Thrillofit86
Thrillofit86

Reputation: 629

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

I am working on a command where a bot responds to !name Barty with Hello Barty.

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

However, the problem I am facing is that the bot responds to !name foo in any channel, and I only want it to respond in certain channels.

I tried:

@bot.event
async def on_message(message):
  if message.channel.id == 1234567:
    @bot.command()
    async def name(ctx, arg):
      await ctx.send(f"hello {arg}")

This isn't working and I'm out of ideas.

How can I send a command to a given channel and get a response from it?

Upvotes: 3

Views: 40251

Answers (2)

Toby
Toby

Reputation: 76

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.

Upvotes: 5

Emil Sležis
Emil Sležis

Reputation: 59

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

Upvotes: 2

Related Questions