-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathparam.py
109 lines (85 loc) · 3.18 KB
/
param.py
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
99
100
101
102
103
104
105
106
107
108
109
# SPDX-License-Identifier: MIT
"""Some examples showing how to customize slash command options."""
import os
from typing import Union
import disnake
from disnake.ext import commands
bot = commands.Bot(command_prefix=commands.when_mentioned)
# disnake will parse the signatures of commands,
# using the annotations for determining option types,
# and automatically handling default values.
# On top of that, using `Param` adds support for many other features.
# We create a new command named "simple" with two options:
# "required" (a required string) and "optional" (an optional integer).
@bot.slash_command()
async def simple(
inter: disnake.CommandInteraction,
required: str,
optional: int = 0,
):
...
# builtins are not the only types supported.
# You can also use various other types like User, Member, Role, TextChannel, Emoji, ...,
# as well as `Union`s of those types in many cases (see the documentation for more details).
@bot.slash_command()
async def other_types(
inter: disnake.CommandInteraction,
user: disnake.User,
emoji: disnake.Emoji,
member_or_role: Union[disnake.Member, disnake.Role],
):
...
# Adding descriptions to the command itself and to its options is
# easy through the use of docstrings, which will be parsed automatically.
@bot.slash_command()
async def description(
inter: disnake.CommandInteraction,
user: disnake.User,
channel: disnake.TextChannel,
):
"""A random command
Parameters
----------
user: A random user
channel: A random channel
"""
# To make an option optional, you can simply give it a default value.
# If you'd like to dynamically compute a default value, you can do so
# by using `Param` and passing a callable.
@bot.slash_command()
async def defaults(
inter: disnake.CommandInteraction[commands.Bot],
string: str = "this is a default value",
user: disnake.User = commands.Param(lambda inter: inter.author),
):
...
# You may limit numbers to a certain range using `commands.Range`.
# "..." is impicitly infinity. Range[int, 0, ...] therefore means any integer from 0 to infinity,
# and Range[int, ..., 0] means anything from -inf to 0.
# All of these bounds are inclusive, meaning `Range[int, 1, 4]` would allow any of 1, 2, 3, or 4.
@bot.slash_command()
async def ranges(
inter: disnake.CommandInteraction,
ranking: commands.Range[int, 1, 10],
negative: commands.Range[int, ..., 0],
fraction: commands.Range[float, 0, 1.0],
):
"""Command with limited ranges
Parameters
----------
ranking: An integer between 1 and 10
negative: An integer lower than 0
fraction: A floating point number between 0 and 1
"""
# Since Discord only allows numbers between -2^53 and 2^53,
# using `commands.LargeInt` allows you to use larger numbers.
# This results in the slash command using a string option, which will be
# converted to an integer locally, allowing for a wider range of numbers.
@bot.slash_command()
async def large(inter: disnake.CommandInteraction, largenumber: commands.LargeInt):
...
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})\n------")
if __name__ == "__main__":
bot.run(os.getenv("BOT_TOKEN"))