Nightly
Nightly

Reputation: 27

I want my bot to process commands sent by other bots

This isn't something most people want, but I do.

Code:

#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)

I figured adding "await client.process_commands(message)" to the bottom of on_message would process commands sent by other bots such as MEE6 but no luck. It appears by default on_message can hear bots but commands can not. Any way to get around this?

Any help would be greatly appreciated!

Upvotes: 0

Views: 435

Answers (2)

user15308374
user15308374

Reputation:

Use ID

for commands:

# 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 ...

for events:

# 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

Upvotes: 0

Blupper
Blupper

Reputation: 408

It is a feature of the Bot class to ignore other bot's messages, but @Daniel O'Brien solved it in this thread. The solution is to subclass the bot and override the function which ignores other bots, like 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)

Upvotes: 2

Related Questions