disnake

Форк
0
128 строк · 4.6 Кб
1
# SPDX-License-Identifier: MIT
2

3
"""An example demonstrating two methods of sending modals and handling modal responses."""
4

5
# pyright: reportUnknownLambdaType=false
6

7
import asyncio
8
import os
9

10
import disnake
11
from disnake.ext import commands
12

13
bot = commands.Bot(command_prefix=commands.when_mentioned)
14

15

16
# One way of sending modals is using a "high-level" implementation similar to views,
17
# in which a class representing the modal is defined, complete with a callback and error handler.
18

19
# Sent modals are stored internally for a certain amount of time, taking up some amount of memory.
20
# Since there is no way of knowing whether the user closed the modal without submitting it,
21
# they time out after 10 minutes by default, at which point they will be removed
22
# from the internal storage, and any submission by the user will fail.
23
# This timeout can be adjusted through the use of the `timeout` parameter of the modal class.
24

25

26
class MyModal(disnake.ui.Modal):
27
    def __init__(self) -> None:
28
        components = [
29
            disnake.ui.TextInput(
30
                label="Name",
31
                placeholder="The name of the tag",
32
                custom_id="name",
33
                style=disnake.TextInputStyle.short,
34
                min_length=5,
35
                max_length=50,
36
            ),
37
            disnake.ui.TextInput(
38
                label="Content",
39
                placeholder="The content of the tag",
40
                custom_id="content",
41
                style=disnake.TextInputStyle.paragraph,
42
                min_length=5,
43
                max_length=1024,
44
            ),
45
        ]
46
        super().__init__(title="Create Tag", custom_id="create_tag", components=components)
47

48
    async def callback(self, inter: disnake.ModalInteraction) -> None:
49
        tag_name = inter.text_values["name"]
50
        tag_content = inter.text_values["content"]
51

52
        embed = disnake.Embed(title=f"Tag created: `{tag_name}`")
53
        embed.add_field(name="Content", value=tag_content)
54
        await inter.response.send_message(embed=embed)
55

56
    async def on_error(self, error: Exception, inter: disnake.ModalInteraction) -> None:
57
        await inter.response.send_message("Oops, something went wrong.", ephemeral=True)
58

59

60
@bot.slash_command()
61
async def create_tag(inter: disnake.CommandInteraction):
62
    await inter.response.send_modal(modal=MyModal())
63

64

65
# Similar to the views and low-level components duality,
66
# you can also send modals using a more "low-level" implementation
67
# without creating a custom modal class, and instead using event listeners.
68

69
# Naturally, these are persistent, unlike modal classes which don't persist
70
# over bot restarts and generally time out after a certain period of time.
71
# Similarly, the listener approach doesn't impact memory usage for every sent modal
72
# as much as the method shown above.
73

74

75
@bot.slash_command()
76
async def create_tag_low(inter: disnake.CommandInteraction):
77
    # Works same as the above code but using a low level interface.
78
    # It's recommended to use this if you don't want to increase cache usage.
79
    await inter.response.send_modal(
80
        title="Create Tag",
81
        custom_id="create_tag_low",
82
        components=[
83
            disnake.ui.TextInput(
84
                label="Name",
85
                placeholder="The name of the tag",
86
                custom_id="name",
87
                style=disnake.TextInputStyle.short,
88
                min_length=5,
89
                max_length=50,
90
            ),
91
            disnake.ui.TextInput(
92
                label="Content",
93
                placeholder="The content of the tag",
94
                custom_id="content",
95
                style=disnake.TextInputStyle.paragraph,
96
                min_length=5,
97
                max_length=1024,
98
            ),
99
        ],
100
    )
101

102
    # Waits until the user submits the modal.
103
    try:
104
        modal_inter: disnake.ModalInteraction = await bot.wait_for(
105
            "modal_submit",
106
            check=lambda i: i.custom_id == "create_tag_low" and i.author.id == inter.author.id,
107
            timeout=600,
108
        )
109
    except asyncio.TimeoutError:
110
        # The user didn't submit the modal in the specified period of time.
111
        # This is done since Discord doesn't dispatch any event for when a modal is closed/dismissed.
112
        return
113

114
    tag_name = modal_inter.text_values["name"]
115
    tag_content = modal_inter.text_values["content"]
116

117
    embed = disnake.Embed(title=f"Tag created: `{tag_name}`")
118
    embed.add_field(name="Content", value=tag_content)
119
    await modal_inter.response.send_message(embed=embed)
120

121

122
@bot.event
123
async def on_ready():
124
    print(f"Logged in as {bot.user} (ID: {bot.user.id})\n------")
125

126

127
if __name__ == "__main__":
128
    bot.run(os.getenv("BOT_TOKEN"))
129

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

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

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

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