4
4
from discord .ext import commands
5
5
6
6
7
- class MyContext (commands .Context ):
7
+ class MyContext (commands .Context ): # custom context
8
8
async def tick (self , value ):
9
9
# reacts to the message with an emoji
10
10
# depending on whether value is True or False
@@ -21,13 +21,30 @@ async def tick(self, value):
21
21
pass
22
22
23
23
24
+ # you can subclass discord.ApplicationContext to create custom application context if needed
25
+ class MyApplicationContext (discord .ApplicationContext ): # custom application context
26
+ async def success (self , message ):
27
+ try :
28
+ await self .respond (embed = discord .Embed ( # respond with a green embed with "Success" title
29
+ title = "Success" ,
30
+ description = message ,
31
+ colour = discord .Colour .green ()
32
+ ))
33
+ except discord .HTTPException : # ignore exceptions
34
+ pass
35
+
36
+
24
37
class MyBot (commands .Bot ):
25
38
async def get_context (self , message , * , cls = MyContext ):
26
39
# when you override this method, you pass your new Context
27
40
# subclass to the super() method, which tells the bot to
28
41
# use the new MyContext class
29
42
return await super ().get_context (message , cls = cls )
30
43
44
+ async def get_application_context (self , interaction , cls = MyApplicationContext ):
45
+ # the same stuff for custom application context
46
+ return await super ().get_application_context (interaction , cls = cls )
47
+
31
48
32
49
bot = MyBot (command_prefix = "!" )
33
50
@@ -44,10 +61,19 @@ async def guess(ctx, number: int):
44
61
await ctx .tick (number == value )
45
62
46
63
64
+ @bot .slash_command (guild_ids = [...])
65
+ async def slash_guess (ctx , number : int ):
66
+ """Guess a random number from 1 to 6."""
67
+ value = random .randint (1 , 6 )
68
+ if number == value :
69
+ await ctx .success ("Congratulations! You guessed the number." ) # use the new helper function
70
+ else :
71
+ await ctx .respond ("You are wrong! Try again." )
72
+
73
+
47
74
# IMPORTANT: You shouldn't hard code your token
48
75
# these are very important, and leaking them can
49
76
# let people do very malicious things with your
50
77
# bot. Try to use a file or something to keep
51
78
# them private, and don't commit it to GitHub
52
- token = "your token here"
53
- bot .run (token )
79
+ bot .run ("TOKEN" )
0 commit comments