AaravM4
AaravM4

Reputation: 410

Discord.py making bot respond in certain channels

I want the users with my bot to be able to configure the bot to only run in certain channels. I was able to achieve this when I was using the on_message function by checking which channel the message was coming from. Now I'm using Cogs and I'm not sure how I would approach this problem.

My Old Code was something like this:

val_channels = ['a', 'b', 'c']
if message.channel in val_channels:
  # Do Something
else:
  print('The bot was configured to: ' + ', '.join(val_channels))

Upvotes: 0

Views: 1795

Answers (1)

Pármenas Haniel
Pármenas Haniel

Reputation: 86

You can verify if the ctx.message.channel.id matches with your list.

class Greetings(commands.Cog):

    @commands.command()
    async def hello(self, ctx):
        """Says hello"""
        if ctx.message.channel.id in val_channels:
            await ctx.send('Hello!')
        else:
            await ctx.send('You can not use this command here.')

But if you still want to use a on_message event inside a cog, you will need do modify the decorator. Link to doc

class Greetings(commands.Cog):

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.channel.id in val_channels:
            # Do something
        else:
            print('The bot was configured to: ' + ', '.join(val_channels))

Upvotes: 1

Related Questions