1
# SPDX-License-Identifier: MIT
3
"""A simple example using custom a command context type."""
9
from disnake.ext import commands
12
class MyContext(commands.Context):
13
async def tick(self, value: bool):
14
# reacts to the message with an emoji
15
# depending on whether value is True or False
16
# if its True, it'll add a green check mark
17
# otherwise, it'll add a red cross mark
18
emoji = "\N{WHITE HEAVY CHECK MARK}" if value else "\N{CROSS MARK}"
21
# this will react to the command author's message
22
await self.message.add_reaction(emoji)
23
except disnake.HTTPException:
24
# sometimes errors occur during this, for example
25
# maybe you don't have permission to do that
26
# we don't mind, so we can just ignore them
30
class MyBot(commands.Bot):
31
async def get_context(self, message, *, cls=MyContext):
32
# when you override this method, you pass your new Context
33
# subclass to the super() method, which tells the bot to
34
# use the new MyContext class
35
return await super().get_context(message, cls=cls)
37
async def on_ready(self):
38
print(f"Logged in as {self.user} (ID: {self.user.id})\n------")
41
bot = MyBot(command_prefix=commands.when_mentioned)
45
async def guess(ctx: MyContext, number: int):
46
"""Guess a random number from 1 to 6."""
47
value = random.randint(1, 6)
48
# with your new helper function, you can add a
49
# green check mark if the guess was correct,
50
# or a red cross mark if it wasn't
51
await ctx.tick(number == value)
54
if __name__ == "__main__":
55
bot.run(os.getenv("BOT_TOKEN"))