为什么我不能有多个 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
.
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
回调.您可以通过将它们定义为 listener
来实现.您最多可以有一个 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 事件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!