为什么多个on_message事件不起作用? [英] Why doesn't multiple on_message events work?

查看:99
本文介绍了为什么多个on_message事件不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能有多个 on_message 事件?

Why can't I have multiple on_message events?

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('in on_ready')

@client.event
async def on_message(message):
    print("in on_message #1")

@client.event
async def on_message(message):
    print("in on_message #2")

@client.event
async def on_message(message):
    print("in on_message #3")

client.run("TOKEN")

例如,如果我输入不一致的内容,则只会触发最后一个 on_message 。我怎样才能使这三个都正常工作?

For example, if I typed anything in discord, it's always only the last on_message that gets triggered. How can I get all three to work?

推荐答案

原生 Client



您只能有一个 on_message ,如果有多个,则只需要最后一个 on_message 事件。您只需要结合三个 on_message

It's not possible with the native Client

You can only have one on_message, if you have multiple, only the last one will be called for the on_message event. You'll just need to combine your three on_message.

import discord

client = discord.Client()

@client.event
async def on_message(message):
    print("in on_message #1")
    print("in on_message #2")
    print("in on_message #3")

client.run("TOKEN")

就像任何Python变量/函数一样(除非装饰器存储您的函数 @ client.event 通过仅保留最近的回调来完成此操作),如果多个名称相同,则将保留最新的名称,其他所有名称都将被覆盖。

Like any Python variable/function (unless the decorator stores your function, @client.event does it by keeping only the most recent callback), if multiple names are the same, the most recently will be kept, and all others will get overwritten.

这是我写的一个简单示例,旨在使您广泛了解discord.py中的事件如何工作(请注意:实际代码并不完全像这样,因为它已被重写并大大减少了)。

This is a simple example I wrote to give you a broad understanding of how events in discord.py work (note: the actual code isn't exactly like this, as it's rewritten and significantly reduced).

class Client:
    def event(self, func):               
        if func.__name__ == "on_message":
            self.on_message_handle = func
            return func

    def receive_message(self, msg):
        func = getattr(self, "on_message_handle", None)
        if func is not None:
            func(msg)
        else:
            self.process_commands(msg)

client = Client()

@client.event
def on_message(msg):
    print("in on_message #1")

@client.event
def on_message(msg):
    print("in on_message #2")

client.receive_message("hello")
# "in on_message #2"

您可以看到 client.event 仅保留一个 on_message

As you can see client.event only keep one instance of on_message.

或者,如果您使用discord.py的 ext.commands 扩展名,则有一种本机的获取方法多个 on_message 回调。通过将它们定义为监听器来实现。您最多可以有一个 on_message 事件,以及无限数量的 on_message 侦听器。

Alternatively, if you're using the ext.commands extension of discord.py, there is a native way to have multiple on_message callbacks. You do so by using defining them as a listener. You can have at most one on_message event, and infinite amounts of on_message listeners.

from discord.ext import commands

bot = commands.Bot('.')

@bot.event
async def on_message(msg):
    print("in on_message #1")
    await bot.process_commands(msg)  # so `Command` instances will still get called


@bot.listen()
async def on_message(msg):
    print("in on_message #2")


@bot.listen()
async def on_message(msg):
    print("in on_message #3")

bot.run("TOKEN")

收到消息后,所有 on_message#1-3 将全部打印出来。

When a message is received, all on_message #1-3 will all get printed.

这篇关于为什么多个on_message事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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