如何在 discord.py 重写中定义对象
How to define objects in discord.py rewrite
如题所述,如何?我真的很难理解文档,因为没有任何示例。如何定义 VoiceState、Member、VoiceChannel 等...
运行 在教程中很好,直到您必须开始查阅特定内容的文档,此时当您不知道如何定义对象或使用某些命令时,它会变得非常令人沮丧...
其中很多都来自经验,所以如果需要一段时间才能掌握窍门,请不要气馁!我知道文档可能有点令人生畏,但我会尽力提供一些示例来提供帮助。
在制作 discord 机器人时,通常您会根据参数和 ctx
(context).
获取信息
首先,我将从一些一般示例开始,然后我将介绍如何在使用 d.py 时使用这些示例。
参数和参数
在python中创建函数时,可以定义参数类型:
def my_func(a: int):
return a + 5
这样做是假设传递给 my_func
的参数是整数,因此也表现为 int
s:
my_func(1)
将 return,如您所料,6
。
但是,当您尝试传递类似这样的内容时:
my_func("1")
你会得到一个 TypeError
,它抱怨连接 str 和 int。此外,当您像这样列出对象的属性时,您可以看到对象的差异:dir(obj)
将相同的概念应用于命令时:
@bot.command()
async def cmd(ctx, member: discord.Member):
await ctx.send(f"Hello, {member.mention}!")
但是当使用命令时,它所做的是能够根据您输入的属性(例如成员的 ID 或名称)获取成员。它找到的成员将具有 discord.Member
对象的所有属性。
这意味着您将能够访问诸如成员的 roles, which return a list containing each role as a discord.Role
对象之类的东西,您可以从那里获取角色的属性,等等。
旁注:
在 d.py 中获取对象的另一种方法是使用 discord.utils
. It takes in an iterable, e.g. a list, as the first argument, and then after that it takes keyword arguments, such as name
, id
, colour
, or any attributes of an abc
到 return 一个对象。
示例:
@bot.command()
async def getrole(ctx):
role = discord.utils.get(ctx.author.roles, name="Very Special Role!", mentionable=True)
await ctx.send(f"Look at you with your {role.mention} How classy!")
这将遍历每个消息发送者的角色,寻找可提及的名称(区分大小写)Very Special Role!
,并且它将使用角色的属性之一发送消息; mention
.
上下文
对于命令,您要传递的第一个参数是 Context,按照惯例表示为 ctx
.
如 link 所示,它具有一系列属性,主要围绕命令的来源、作者和所有其他相关详细信息等。
@bot.command()
async def hello(ctx):
await ctx.send(f"Hello, {ctx.author.name}!")
在那里的命令中,我使用了 coroutine from context called send()
,用于发送消息。
并且在命令的 上下文 中,它会将其发送到同一频道,这就是它的工作原理。
另外,一些常用的多余代码的例子:
ctx.message.channel.send("some message")
ctx.message.author.send("some dm")
可以分别变成:
ctx.send("some message")
ctx.author.send("some dm")
但是您如何知道可以send()
向何处发送消息?好吧,这让我们进入下一部分:
抽象基础类
这些是我假设您在谈论定义对象时所谈论的内容。它们是对象继承的基本模板 - 例如,TextChannel
has inherited all of Messageable
's attributes (except connectable
, which is only inherited by discord.VoiceChannel.channel
.
我将使用 abc.Messageable
as an example. In the link, it gives us examples of some Messageable objects, i.e. where you can send messages to, and these are objects such as TextChannel
s, Member
s 等
偶尔我会看到有人漏掉 await
或者不知道什么时候添加它们,或者添加它们太频繁了。你会知道什么时候添加一个,因为文档会说明函数是否是协程,例如:
这就是您知道可以对每个对象做什么的方式 - 将这些视为您在 discord 中获得的对象的模板,而不是您定义的东西!
如果您需要任何进一步的说明,或者如果您发现我的回答有任何错误,请告诉我。
参考文献:
如题所述,如何?我真的很难理解文档,因为没有任何示例。如何定义 VoiceState、Member、VoiceChannel 等...
运行 在教程中很好,直到您必须开始查阅特定内容的文档,此时当您不知道如何定义对象或使用某些命令时,它会变得非常令人沮丧...
其中很多都来自经验,所以如果需要一段时间才能掌握窍门,请不要气馁!我知道文档可能有点令人生畏,但我会尽力提供一些示例来提供帮助。
在制作 discord 机器人时,通常您会根据参数和 ctx
(context).
首先,我将从一些一般示例开始,然后我将介绍如何在使用 d.py 时使用这些示例。
参数和参数
在python中创建函数时,可以定义参数类型:
def my_func(a: int):
return a + 5
这样做是假设传递给 my_func
的参数是整数,因此也表现为 int
s:
my_func(1)
将 return,如您所料,6
。
但是,当您尝试传递类似这样的内容时:
my_func("1")
你会得到一个 TypeError
,它抱怨连接 str 和 int。此外,当您像这样列出对象的属性时,您可以看到对象的差异:dir(obj)
将相同的概念应用于命令时:
@bot.command()
async def cmd(ctx, member: discord.Member):
await ctx.send(f"Hello, {member.mention}!")
但是当使用命令时,它所做的是能够根据您输入的属性(例如成员的 ID 或名称)获取成员。它找到的成员将具有 discord.Member
对象的所有属性。
这意味着您将能够访问诸如成员的 roles, which return a list containing each role as a discord.Role
对象之类的东西,您可以从那里获取角色的属性,等等。
旁注:
在 d.py 中获取对象的另一种方法是使用 discord.utils
. It takes in an iterable, e.g. a list, as the first argument, and then after that it takes keyword arguments, such as name
, id
, colour
, or any attributes of an abc
到 return 一个对象。
示例:
@bot.command()
async def getrole(ctx):
role = discord.utils.get(ctx.author.roles, name="Very Special Role!", mentionable=True)
await ctx.send(f"Look at you with your {role.mention} How classy!")
这将遍历每个消息发送者的角色,寻找可提及的名称(区分大小写)Very Special Role!
,并且它将使用角色的属性之一发送消息; mention
.
上下文
对于命令,您要传递的第一个参数是 Context,按照惯例表示为 ctx
.
如 link 所示,它具有一系列属性,主要围绕命令的来源、作者和所有其他相关详细信息等。
@bot.command()
async def hello(ctx):
await ctx.send(f"Hello, {ctx.author.name}!")
在那里的命令中,我使用了 coroutine from context called send()
,用于发送消息。
并且在命令的 上下文 中,它会将其发送到同一频道,这就是它的工作原理。
另外,一些常用的多余代码的例子:
ctx.message.channel.send("some message")
ctx.message.author.send("some dm")
可以分别变成:
ctx.send("some message")
ctx.author.send("some dm")
但是您如何知道可以send()
向何处发送消息?好吧,这让我们进入下一部分:
抽象基础类
这些是我假设您在谈论定义对象时所谈论的内容。它们是对象继承的基本模板 - 例如,TextChannel
has inherited all of Messageable
's attributes (except connectable
, which is only inherited by discord.VoiceChannel.channel
.
我将使用 abc.Messageable
as an example. In the link, it gives us examples of some Messageable objects, i.e. where you can send messages to, and these are objects such as TextChannel
s, Member
s 等
偶尔我会看到有人漏掉 await
或者不知道什么时候添加它们,或者添加它们太频繁了。你会知道什么时候添加一个,因为文档会说明函数是否是协程,例如:
这就是您知道可以对每个对象做什么的方式 - 将这些视为您在 discord 中获得的对象的模板,而不是您定义的东西!
如果您需要任何进一步的说明,或者如果您发现我的回答有任何错误,请告诉我。
参考文献: