disnake

Форк
0
/
custom_context.py 
55 строк · 1.7 Кб
1
# SPDX-License-Identifier: MIT
2

3
"""A simple example using custom a command context type."""
4

5
import os
6
import random
7

8
import disnake
9
from disnake.ext import commands
10

11

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}"
19

20
        try:
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
27
            pass
28

29

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)
36

37
    async def on_ready(self):
38
        print(f"Logged in as {self.user} (ID: {self.user.id})\n------")
39

40

41
bot = MyBot(command_prefix=commands.when_mentioned)
42

43

44
@bot.command()
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)
52

53

54
if __name__ == "__main__":
55
    bot.run(os.getenv("BOT_TOKEN"))
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.