【问题标题】:I want my bot to process commands sent by other bots我希望我的机器人处理其他机器人发送的命令
【发布时间】:2021-10-05 10:47:49
【问题描述】:

这不是大多数人想要的,但我愿意。

代码:

#imports
import discord
import os
from keep_alive import keep_alive
from discord.ext.commands import has_permissions, MissingPermissions
from discord.ext import commands
from discord.utils import get

#client name
client = discord.Client()

#log-in msg
@client.event
async def on_ready():
    print("Successfully logged in as")
    print(client.user)

#prefix and remove default help cmd
client = commands.Bot(command_prefix='H')
client.remove_command("help")

@client.command(pass_context=True)
async def ere(ctx, *, args=None):
  await ctx.send("hi")
  if discord.utils.get(ctx.message.author.roles, name="MEE6") != None:
    if args != None:
      await ctx.send("mee6 just spoke!")
  else:
    await ctx.send("nope")

@client.event
async def on_message(message):
  print(message.author)
  if "Here" in message.content:
    if discord.utils.get(message.author.roles, name="MEE6") != None:
      channel = await client.fetch_channel(870023245892575282)
      await channel.send("yes")
  await client.process_commands(message)

我认为在 on_message 的底部添加“await client.process_commands(message)”可以处理其他机器人(如 MEE6)发送的命令,但没有运气。默认情况下,on_message 可以听到机器人,但命令不能。有什么办法可以解决这个问题?

任何帮助将不胜感激!

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    忽略其他机器人的消息是 Bot 类的一个特性,但 @Daniel O'Brien 在 this 线程中解决了这个问题。解决方案是对机器人进行子类化并覆盖忽略其他机器人的函数,如下所示:

    class UnfilteredBot(commands.Bot):
        """An overridden version of the Bot class that will listen to other bots."""
    
        async def process_commands(self, message):
            """Override process_commands to listen to bots."""
            ctx = await self.get_context(message)
            await self.invoke(ctx)
    

    【讨论】:

    • 我愿意接受这个答案,但它并不完全适合我。你建议我把这段代码放在哪里?
    • client = commands.Bot(command_prefix='H') 替换为client = UnfilteredBot(command_prefix='H') 并将自定义类放在代码之前。
    • 谢谢,成功了。我不得不使用 client = UnfilteredBot(command_prefix='H')
    • 对,是我的错字。编辑了我的评论
    【解决方案2】:

    使用 ID

    对于命令:

    # from discord.ext import commands as cmds
    
    def is_mee6():
        def predicate(ctx: cmds.Context):
            return ctx.message.author.id == 159985870458322944: 
            # 159985870458322944 is ID of MEE6
        return cmds.check(predicate)
    
    # Use like it:
    
    # @cmds.command()
    # @is_mee6()
    # async def ...
    

    对于事件:

    # from discord.ext import commands as cmds
    
    mee6_id = 159985870458322944
    
    # and use it for checks. for Example
    
    @cmds.event
    async def on_message(message):
        if message.author.id == mee6_id:
            # ANY
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2021-04-14
      • 2023-03-12
      • 1970-01-01
      • 2021-05-11
      • 2010-09-29
      • 2019-06-29
      • 2022-10-13
      • 2021-10-30
      相关资源
      最近更新 更多