4
votes

Yesterday I was working on something simple where a bot on command of !name Barty would print back Hello Barty

@bot.command()
async def name(ctx, args):
    await ctx.send("hello {}".format(args)

However the problem I am facing at this moment is that the bot would response to any channel where I do use the !name XXXX and what I am trying to do is that I want only to react to given specific channel in discord.

I tried to do:

@bot.event
async def on_message(message):

    if message.channel.id == 1234567:

        @bot.command()
        async def name(ctx, args):
            await ctx.send("hello {}".format(args)

but that was completely not working and I am out of ideas and here I am.

How can I send command to a given specific channel and get back response from there?

2
What you currently have in your second snippet is: "if channel id equals 1234567, then define the command name". But this doesn't work for two reasons: the function isn't called and commands should be top-level functions (i.e. not nested). Are you intending to apply this "channel restriction" to other commands as well (not just "name")?TrebledJ
@TrebledJ Correct, I was planning to add maybe 3 functions that works the same way as async def name(ctx, args): but some will have more args etc async def birthday(ctx, args, args2): - But yes. I want to apply the "channel restriction" to other commands aswell. :)Thrillofit86

2 Answers

6
votes

Move the define code out of the IF statement:

@bot.command()
async def name(ctx, args):
await ctx.send("hello {}".format(args)

When you've done that, you should be able to do;

if (message.channel.id == 'channel id'): 
  await message.channel.send('message goes here')

else:
  # handle your else here, such as null, or log it to ur terminal

Could also check out the docs: https://discordpy.readthedocs.io

Once you make the command, make the IF statement inside of that.

2
votes

message.channel.id now needs to be an integer, not a string so you don't need to use ''

    if (message.channel.id == 1234567): 
      await message.channel.send('message goes here')

  else:
    # handle your else here, such as null, or log in to ur terminal