File: label.py

package info (click to toggle)
python-discord 2.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,476 kB
  • sloc: python: 49,910; javascript: 363; makefile: 154
file content (98 lines) | stat: -rw-r--r-- 3,825 bytes parent folder | download
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
92
93
94
95
96
97
98
import datetime
import discord
from discord import app_commands

import traceback

# The guild in which this slash command will be registered.
# It is recommended to have a test guild to separate from your "production" bot
TEST_GUILD = discord.Object(0)


class MyClient(discord.Client):
    # Suppress error on the User attribute being None since it fills up later
    user: discord.ClientUser

    def __init__(self) -> None:
        # Just default intents and a `discord.Client` instance
        # We don't need a `commands.Bot` instance because we are not
        # creating text-based commands.
        intents = discord.Intents.default()
        super().__init__(intents=intents)

        # We need an `discord.app_commands.CommandTree` instance
        # to register application commands (slash commands in this case)
        self.tree = app_commands.CommandTree(self)

    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {self.user.id})')
        print('------')

    async def setup_hook(self) -> None:
        # Sync the application command with Discord.
        await self.tree.sync(guild=TEST_GUILD)


class TimeoutModal(discord.ui.Modal, title='Timeout Member'):
    # We can use a Label to attach a rich label and description to our item.
    duration = discord.ui.Label(
        text='Duration',
        description='How long to timeout the member for.',
        component=discord.ui.Select(
            options=[
                discord.SelectOption(label='1 minute', value='60'),
                discord.SelectOption(label='5 minutes', value='300'),
                discord.SelectOption(label='10 minutes', value='600'),
                discord.SelectOption(label='30 minutes', value='1800'),
                discord.SelectOption(label='1 hour', value='3600'),
            ],
        ),
    )

    reason = discord.ui.Label(
        text='Reason',
        description='The reason for the timeout.',
        component=discord.ui.TextInput(
            style=discord.TextStyle.short,
            max_length=256,
        ),
    )

    def __init__(self, member: discord.Member) -> None:
        self.member = member
        super().__init__()

    async def on_submit(self, interaction: discord.Interaction):
        # Tell the type checker what our components are...
        assert isinstance(self.duration.component, discord.ui.Select)
        assert isinstance(self.reason.component, discord.ui.TextInput)

        until = discord.utils.utcnow() + datetime.timedelta(seconds=int(self.duration.component.values[0]))
        await self.member.timeout(until, reason=self.reason.component.value)
        await interaction.response.send_message(
            f'Timeout {self.member.mention} until {discord.utils.format_dt(until)} with reason: {self.reason.component.value}',
            ephemeral=True,
        )

    async def on_error(self, interaction: discord.Interaction, error: Exception) -> None:
        await interaction.response.send_message('Oops! Something went wrong.', ephemeral=True)

        # Make sure we know what the error actually is
        traceback.print_exception(type(error), error, error.__traceback__)


client = MyClient()


@client.tree.command(guild=TEST_GUILD, description="Timeout a member")
async def timeout(interaction: discord.Interaction, member: discord.Member):
    # Send the modal with an instance of our `TimeoutModal` class
    # Since modals require an interaction, they cannot be done as a response to a text command.
    # They can only be done as a response to either an application command or a button press.

    # Do note that this example is illustrative, Discord comes with this timeout feature natively
    # and does not need this command or modal.
    await interaction.response.send_modal(TimeoutModal(member))


client.run('token')