特定不和谐音频道的OnMessage例程



我正在尝试使用不和机器人开发一款游戏。我在处理onmessage例程时遇到了麻烦。我只需要它来"聆听"。一个特定的通道,而不是所有的服务器…到目前为止,我做了以下操作:

@client.event
async def on_message(message):
global rojo
global IDS
canal = IDS['C_Juego']
if message.author == client.user or str(message.channel) != IDS['C_Juego']:
return
else:
if(rojo == 1):
autor = message.author
await message.add_reaction("🔴")
await message.channel.send("Player: " + str(autor) + " removed!")
role = get(message.guild.roles, name="Jugador")
await message.author.remove_roles(role)
elif(str(message.channel) == IDS['C_Juego']):
await message.add_reaction("🟢")
print("verde")

怎么回事?当我启用这个功能…除了该函数被发送的每个消息调用....

之外,我的其余命令不再起作用(在服务器的任何通道中)我解释了上下文:这是一个游戏,在听一首歌的时候,玩家必须把不同的单词放在一个主题下,当音乐停止时,如果有人写,他们就被淘汰了。

命令定义:我有很多命令定义……它工作得很好,直到我添加这个有问题的函数…我添加其中两个作为示例:

@client.command()
@commands.has_role("Owner")
async def clear(ctx):
await ctx.channel.purge()
@client.command()
@commands.has_role("Owner")
async def swipe(ctx, role: discord.Role):
print(role.members)
for member in role.members:
await member.remove_roles(role)
await ctx.send(f"Successfully removed all members from {role.mention}.")

覆盖缺省提供的on_message将禁止运行任何额外的命令。要解决这个问题,在on_message的末尾添加一个bot.process_commands(message)行。

@client.event
async def on_message(message):
# do what you want to do here
await client.process_commands(message)

https://discordpy.readthedocs.io/en/latest/faq.html why-does-on-message-make-my-commands-stop-working