真正的@ client.event是什么?不和谐 [英] What it really is @client.event? discord.py

查看:39
本文介绍了真正的@ client.event是什么?不和谐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我开始对不和谐的机器人编程感兴趣.在这些程序的语法中,我注意到了许多无法解决的难以理解的问题.这就是为什么我要您帮助您了解它们.

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?我发现这是一个事件处理程序,但是它如何工作?为什么需要运行程序?它以某种方式连接到异步吗?

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)

这篇关于真正的@ client.event是什么?不和谐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆