我希望我的机器人处理其他机器人发送的命令

时间:2021-07-29 19:06:38

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

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

代码:

#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 可以听到机器人,但命令不能。有什么办法可以解决这个问题?

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

使用 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