@client.event 到底是什么?不和谐.py
问题描述
几天前,我开始对编写不和谐机器人有点兴趣.在这些程序的语法中,我注意到很多我无法找到答案的难以理解的问题.这就是为什么我要求您帮助理解它们.
A few days ago I became interested in programming discord bots a bit. In the syntax of these programs I noticed a lot of unintelligible issues that I can not find an answer to. That's why I am asking you for help in understanding them.
所有问题都基于此代码:
All questions are based on this code:
import discord
import asyncio
from discord.ext import commands
botToken = '***'
client = commands.Bot(command_prefix = '.')
@client.event
async def on_ready():
print('Bot is ready!')
@client.event
async def on_message(message):
author = message.author
if message.content =='Hello':
await client.send_message(message.channel, 'Welcome again {}!'.format(author))
client.run(botToken)
什么是@client.event?我发现这是一个事件处理程序,但它是如何工作的?为什么需要运行程序?它是否以某种方式连接到 asyncio?
What is @client.event? I found that is a event handler, but how is it worki? Why is it needed to run program? Is it somehow connected to a asyncio?
解决方案
当 Client
接收到来自 Discord 的事件时,它会计算出该事件是什么,并生成或定位之前的对象由事件发送,例如任何 MESSAGE_RECEIVE 事件的 discord.Message
或 REACTION_ADD 等的 discord.Reaction
.
然后客户端将对象发送到处理这些事件的方法中,但您首先需要告诉客户端这些方法是什么.这就是事件装饰器的用武之地.
When a Client
receives an event from Discord, It works out what that event is and generates, or locates, the objects that were sent by the event, such as a discord.Message
for any MESSAGE_RECEIVE events, or a discord.Reaction
for REACTION_ADD etc.
The client then sends the objects into the method that handles those events, but you first need to tell the client what those methods are. This is where the event decorators come in.
装饰器本质上是将其他函数作为参数的函数.您将看到的最常见的是 @property
.这表示您定义的函数应该传递给 property()
函数
Decorators are, in essence, functions that take other functions as arguments. The most common one you'll see is @property
. This says that the function you define should be passed into the property()
function
@property
def name(self):
return self._name
和
def name(self):
return self._name
name = property(name)
这可能会让您感到困惑,但这就是 discord.py 处理其事件的方式.
This may be a bit confusing to wrap your head around, but this is how discord.py handles its events.
当您在 on_message
上使用 @client.event
装饰器时,您实际上是在说 on_message = client.event(on_message)代码>
When you use the @client.event
decorator on your on_message
, what you are actually doing is saying on_message = client.event(on_message)
on_event 的 discord.py 的内部代码是 这个
The internal code of discord.py for on_event is this
def event(self, coro):
# Validation we don't need to worry about
setattr(self, coro.__name__, coro)
return coro
这意味着它将函数作为其参数,并在客户端本身上设置一个新属性.对于我们的 on_message
示例,我们将 on_message
函数传递给 client.event()
并让客户端定义一个新的 client.on_message
方法与我们的 on_message
方法相同.
Which means that it takes the function as its parameter, and sets a new attribute on the client itself. For our on_message
example, we pass our on_message
function into client.event()
and it makes the client define a new client.on_message
method that is the same method as our on_message
.
注意:func.__name__
返回该函数的名称.on_message.__name__
将返回 "on_message"
.setattr(obj, name, value)
设置对象的属性,所以 setattr(self, "foo", 100)
表示 self.foo代码> 将是 100.
Note: func.__name__
returns the name of that function. on_message.__name__
will return "on_message"
.
setattr(obj, name, value)
sets an attribute on an object, so setattr(self, "foo", 100)
means that self.foo
will be 100.
现在客户端知道我们的 on_message
,当它接收到一个表明消息已发送的事件时,它会创建 discord.Message
对象并将其传递给 client.on_message
,正如我们已经建立的,和我们自己的on_message
Now that the client knows our on_message
, when it receives an event saying that a message was sent, it creates the discord.Message
object and passes that into client.on_message
, which as we already established, is the same as our own on_message
如果你愿意,你甚至可以跳过装饰器并在你的函数之后执行此操作,但它不如装饰器优雅:
If you wanted, you could even just skip the decorator and do this after your function, but it is less elegant than a decorator is:
on_message = client.event(on_message)
相关文章