• <i id='cYt4c'><tr id='cYt4c'><dt id='cYt4c'><q id='cYt4c'><span id='cYt4c'><b id='cYt4c'><form id='cYt4c'><ins id='cYt4c'></ins><ul id='cYt4c'></ul><sub id='cYt4c'></sub></form><legend id='cYt4c'></legend><bdo id='cYt4c'><pre id='cYt4c'><center id='cYt4c'></center></pre></bdo></b><th id='cYt4c'></th></span></q></dt></tr></i><div id='cYt4c'><tfoot id='cYt4c'></tfoot><dl id='cYt4c'><fieldset id='cYt4c'></fieldset></dl></div>

        <bdo id='cYt4c'></bdo><ul id='cYt4c'></ul>

      <legend id='cYt4c'><style id='cYt4c'><dir id='cYt4c'><q id='cYt4c'></q></dir></style></legend>

        <tfoot id='cYt4c'></tfoot>

        <small id='cYt4c'></small><noframes id='cYt4c'>

        @client.event 到底是什么?不和谐.py

        时间:2023-09-09

            <tbody id='TDizp'></tbody>
        • <tfoot id='TDizp'></tfoot>
            • <bdo id='TDizp'></bdo><ul id='TDizp'></ul>
              <i id='TDizp'><tr id='TDizp'><dt id='TDizp'><q id='TDizp'><span id='TDizp'><b id='TDizp'><form id='TDizp'><ins id='TDizp'></ins><ul id='TDizp'></ul><sub id='TDizp'></sub></form><legend id='TDizp'></legend><bdo id='TDizp'><pre id='TDizp'><center id='TDizp'></center></pre></bdo></b><th id='TDizp'></th></span></q></dt></tr></i><div id='TDizp'><tfoot id='TDizp'></tfoot><dl id='TDizp'><fieldset id='TDizp'></fieldset></dl></div>

              <small id='TDizp'></small><noframes id='TDizp'>

              1. <legend id='TDizp'><style id='TDizp'><dir id='TDizp'><q id='TDizp'></q></dir></style></legend>

                  本文介绍了@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)
                  

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

                    • <small id='vqtfg'></small><noframes id='vqtfg'>

                          <tbody id='vqtfg'></tbody>
                      1. <tfoot id='vqtfg'></tfoot>
                        <i id='vqtfg'><tr id='vqtfg'><dt id='vqtfg'><q id='vqtfg'><span id='vqtfg'><b id='vqtfg'><form id='vqtfg'><ins id='vqtfg'></ins><ul id='vqtfg'></ul><sub id='vqtfg'></sub></form><legend id='vqtfg'></legend><bdo id='vqtfg'><pre id='vqtfg'><center id='vqtfg'></center></pre></bdo></b><th id='vqtfg'></th></span></q></dt></tr></i><div id='vqtfg'><tfoot id='vqtfg'></tfoot><dl id='vqtfg'><fieldset id='vqtfg'></fieldset></dl></div>
                          <bdo id='vqtfg'></bdo><ul id='vqtfg'></ul>
                          • <legend id='vqtfg'><style id='vqtfg'><dir id='vqtfg'><q id='vqtfg'></q></dir></style></legend>