forked from DisnakeDev/disnake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverters.py
More file actions
75 lines (57 loc) · 3.03 KB
/
Copy pathconverters.py
File metadata and controls
75 lines (57 loc) · 3.03 KB
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
# This example requires the 'members' privileged intent to use the Member converter.
import typing
import disnake
from disnake.ext import commands
intents = disnake.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(commands.when_mentioned_or("!"), intents=intents)
@bot.command()
async def userinfo(ctx: commands.Context, user: disnake.User):
# In the command signature above, you can see that the `user`
# parameter is typehinted to `disnake.User`. This means that
# during command invocation we will attempt to convert
# the value passed as `user` to a `disnake.User` instance.
# The documentation notes what can be converted, in the case of `disnake.User`
# you pass an ID, mention or username (discrim optional)
# E.g. 80088516616269824, @Danny or Danny#0007
# NOTE: typehinting acts as a converter within the `commands` framework only.
# In standard Python, it is use for documentation and IDE assistance purposes.
# If the conversion is successful, we will have a `disnake.User` instance
# and can do the following:
user_id = user.id
username = user.name
avatar = user.display_avatar.url
await ctx.send(f"User found: {user_id} -- {username}\n{avatar}")
@userinfo.error
async def userinfo_error(ctx: commands.Context, error: commands.CommandError):
# if the conversion above fails for any reason, it will raise `commands.BadArgument`
# so we handle this in this error handler:
if isinstance(error, commands.BadArgument):
return await ctx.send("Couldn't find that user.")
@bot.command()
async def ignore(ctx: commands.Context, target: typing.Union[disnake.Member, disnake.TextChannel]):
# This command signature utilises the `typing.Union` typehint.
# The `commands` framework attempts a conversion of each type in this Union *in order*.
# So, it will attempt to convert whatever is passed to `target` to a `disnake.Member` instance.
# If that fails, it will attempt to convert it to a `disnake.TextChannel` instance.
# See: https://docs.disnake.dev/en/latest/ext/commands/commands.html#typing-union
# NOTE: If a Union typehint converter fails it will raise `commands.BadUnionArgument`
# instead of `commands.BadArgument`.
# To check the resulting type, `isinstance` is used
if isinstance(target, disnake.Member):
await ctx.send(f"Member found: {target.mention}, adding them to the ignore list.")
elif isinstance(
target, disnake.TextChannel
): # this could be an `else` but for completeness' sake.
await ctx.send(f"Channel found: {target.mention}, adding it to the ignore list.")
# Built-in type converters.
@bot.command()
async def multiply(ctx: commands.Context, number: int, maybe: bool):
# We want an `int` and a `bool` parameter here.
# `bool` is a slightly special case, as shown here:
# See: https://docs.disnake.dev/en/latest/ext/commands/commands.html#bool
if maybe is True:
return await ctx.send(number * 2)
await ctx.send(number * 5)
bot.run("token")