Skip to content
Advertisement

How to receive and send message to specific channel in Discord.py?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement