【问题标题】:OnMessage routine for a specific discord channel特定不和谐频道的 OnMessage 例程
【发布时间】:2021-10-10 12:31:40
【问题描述】:

我正在尝试使用不和谐机器人开发游戏。我在处理 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")

发生了什么事?当我启用此功能时.. 除了发送的每条消息都会调用此功能之外,我的其余命令停止生效(在服务器的任何通道中)....

我解释一下背景:这是一个游戏,玩家在听一首歌时必须在一个主题下放置不同的单词,当音乐停止时,如果有人写,他们就会被淘汰。 p>

命令定义: 我有很多命令定义......在我添加这个有问题的功能之前它工作正常......我添加其中两个作为示例:

@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 或 @client.command() 装饰器?
  • 是的!很多,我会将这些信息添加到问题中

标签: python discord discord.py


【解决方案1】:

覆盖默认提供的 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

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2021-09-15
    • 2021-04-15
    • 2021-11-23
    • 2020-07-14
    • 2020-11-06
    相关资源
    最近更新 更多