jack h
jack h

Reputation: 51

Sending messages from only 1 channel in Discord (discord.py bot)

For my Discord bot, I am trying to have it only respond to messages in one channel. I have the following code:

@client.event
async def send(message):
  if message.channel.id == 897042290487492638:
    await message.channel.send('Hi!')

The idea is it takes the channel id of a sent message, and if it matches the designated channel, it will respond with 'Hi!'. My problem is when I send a message from the designated channel, the bot will not do anything. I checked the permissions of the bot, and it had all permissions necessary for something like this. To double-check, I added a command earlier in my code to ensure the bot was working, and I could execute said command just fine. Does anyone know what I am missing here?

I do know that I can change the bot's permissions to only read the designated channel, but I want it to be able to read other channels for certain commands. I only want to read this channel for this particular instance, and I figured this would be the best way to do it. Any help is greatly appreciated!

EDIT: After some research, I discovered that commands and events don't mix well unless the following line is added:

await client.process_commands(message)

However, this seemed to fix the error of bots not executing commands, but still executing events. My problem is the opposite - the bot is reading commands just fine, but it is not executing events. I added this line to my code anyways, but still was unable to solve this problem.

Upvotes: 3

Views: 2049

Answers (1)

RiveN
RiveN

Reputation: 2663

It's happening because there isn't anything like a "send" event. I think you want to use the on_message event:

@client.event
async def on_message(message):
  if message.channel.id == 897042290487492638:
    await message.channel.send('Hi!')

  await client.process_commands(message)

Remember that it requires intents.messages.

Upvotes: 1

Related Questions