Yesterday I was working on something simple where a bot on command of !name Barty
would print back Hello Barty
JavaScript
x
4
1
@bot.command()
2
async def name(ctx, args):
3
await ctx.send("hello {}".format(args)
4
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:
JavaScript
1
9
1
@bot.event
2
async def on_message(message):
3
4
if message.channel.id == 1234567:
5
6
@bot.command()
7
async def name(ctx, args):
8
await ctx.send("hello {}".format(args)
9
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?
Advertisement
Answer
Move the define code out of the IF statement:
JavaScript
1
4
1
@bot.command()
2
async def name(ctx, args):
3
await ctx.send("hello {}".format(args)
4
When you’ve done that, you should be able to do;
JavaScript
1
6
1
if (message.channel.id == 'channel id'):
2
await message.channel.send('message goes here')
3
4
else:
5
# handle your else here, such as null, or log it to ur terminal
6
Could also check out the docs: https://discordpy.readthedocs.io
Once you make the command, make the IF statement inside of that.