-
Notifications
You must be signed in to change notification settings - Fork 7
/
basic.py
91 lines (65 loc) 路 2.44 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from __future__ import annotations
import hikari
import crescent
bot = hikari.GatewayBot(token="...")
client = crescent.Client(bot)
class RandomError(Exception):
pass
class UnhandledError(Exception):
pass
# error handling
@client.include
@crescent.catch_command(RandomError)
async def on_cmd_random_error(exc: RandomError, ctx: crescent.Context) -> None:
await ctx.respond(f"{exc} raised in {ctx.command}!")
@client.include
@crescent.catch_event(RandomError)
async def on_event_random_error(exc: RandomError, event: hikari.Event) -> None:
print(f"{exc} raised in {event}!")
@client.include
@crescent.catch_autocomplete(RandomError)
async def on_autocomplete_random_error(
exc: RandomError,
ctx: crescent.AutocompleteContext,
inter: hikari.AutocompleteInteractionOption,
) -> None:
print(f"{exc} raised in autocomplete for {ctx.command}!")
# buggy command/event/autocompletes
@client.include
@crescent.command(name="raise-error-cmd")
class RaiseErrorCmd:
unhandled = crescent.option(bool)
def callback(self, ctx: crescent.Context):
if self.unhandled:
raise UnhandledError("Unhandled error!")
raise RandomError("Handled error")
@client.include
@crescent.event
async def raise_error_event(event: hikari.MessageCreateEvent) -> None:
if event.author.is_bot:
return
if event.message.content is None:
return
if event.message.content == "!unhandled":
raise UnhandledError("Unhandled error!")
elif event.message.content == "!error":
raise RandomError("Handled error")
elif event.message.content.startswith("!"):
await event.message.respond("Use !unhandled or !error")
async def autocomplete(
ctx: crescent.AutocompleteContext, option: hikari.AutocompleteInteractionOption
) -> list[tuple[str, str]]:
assert isinstance(option.value, str)
if option.value == "unhandled":
raise UnhandledError("Unhandled error!")
elif option.value == "error":
raise RandomError("Handled error")
# returns a list of tuples of (option name, option value).
return [("error", "error"), ("unhandled", "unhandled")]
@client.include
@crescent.command(name="autocomplete-error")
class AutocompleteError:
option = crescent.option(str, "Type error to error out", autocomplete=autocomplete)
async def callback(self, ctx: crescent.Context):
await ctx.respond(f"{self.option} (type unhandled or error inside option)")
bot.run()