Creating a Python Discord bot that echoes all messages in a channel

Hey everyone! I’m working on a Discord bot using Python 3.6.1. I want it to copy all messages sent in a channel. Right now, I’ve got this code:

@bot.event
async def on_message(message):
    await bot.send(message.content)

But it’s not working as expected. I’m getting an error about the destination being NoneType. Any ideas on how to fix this? I’m pretty new to Discord bot development, so any help would be awesome! Thanks a bunch!

I’ve encountered this issue before. The problem lies in the bot.send() method you’re using. That’s not the correct way to send messages in discord.py. Instead, you should use message.channel.send(). This will send the message back to the channel it originated from.

Here’s the corrected code:

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    await message.channel.send(message.content)

This code checks if the message is from the bot itself to avoid an infinite loop. Then it sends the message content back to the same channel. Make sure you have the necessary intents enabled in your bot initialization as well. Let me know if you need any further clarification!

As someone who’s been tinkering with Discord bots for a while, I can shed some light on your issue. The problem lies in how you’re trying to send messages. Instead of bot.send(), you should use message.channel.send(). This ensures the bot responds in the same channel where the message was received.

Here’s a revised version of your code that should work:

@bot.event
async def on_message(message):
if message.author == bot.user:
return
await message.channel.send(message.content)

This code first checks if the message is from the bot itself to prevent an infinite loop. Then it echoes the message content back to the channel.

One thing to keep in mind: this will echo ALL messages in ALL channels the bot has access to. If you want to limit it to a specific channel, you’ll need to add a check for the channel ID. Also, make sure you’ve enabled the necessary intents when initializing your bot. Happy coding!

hey mate, i think ur issue is with the bot.send() method. that’s not how ya send messages in discord.py. Try using message.channel.send() instead. That’ll send the message back to the same channel it came from. Hope this helps ya out!