Thomas Mushroom
Thomas Mushroom

Reputation: 1

Bot won't detect message using on_message in discord.py

Starting on line 35, the bot is supposed to detect the message and print it in the console, but it does not print the message in the console.

I've tried looking for a solution but every solution I try does not work. Am I doing something wrong?

import discord
from discord import channel
from discord import message
from discord.colour import Color
from discord.errors import PrivilegedIntentsRequired
from discord.ext import commands

token = token
client = commands.Bot(command_prefix="<", case_insensitive=True)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.command()
async def clear(ctx, amount=100):
    await ctx.channel.purge(limit=amount)

@client.command()
async def move(ctx,member: discord.Member=None, VoiceChannel=None):
    try:
        channel = discord.utils.get(ctx.guild.channels, id=int(VoiceChannel))
        if member == None:
            await ctx.message.author.move_to(channel)
        else:
            await member.move_to(channel)
    except Exception as e:
        embed = discord.Embed(
            title = '**ERROR**',
            description = 'e',
            colour = discord.Color.red()
        )
    await ctx.send(embed=embed)

@client.event
async def on_message(message):
     print(message.content)

@client.event
async def on_message(message):
    ctx = await client.get_context(message)
    if ctx.valid:
        await client.invoke(ctx)

client.run(token)

Upvotes: 0

Views: 1931

Answers (4)

Harry
Harry

Reputation: 66

As RiveN said, use one on_message. This is why your code isn't working as you've repurposed the on_message function. Using more than one on_message function(or any event listener) in the same cog/file would not work for this reason.

Upvotes: 0

superhackedmonkey
superhackedmonkey

Reputation: 21

It's extremely hard to get the message content of a message, well for me it was. You can use something like this that prints message author and guild name.

@bot.listen()
async def on_message(message: discord.Message):
    channel = bot.get_channel(message.channel.id)
    messages = await channel.history(limit=1).flatten()
    for message in messages:
        if not message.attachments:
            print(f"{message.guild.name}:", message.author, message.content)
        else:
            print(message.author, message.attachments)

Upvotes: 0

Jirka
Jirka

Reputation: 11

It didn't work for me either. I have another solution:

@client.event
async def on_message(message):
    message = await message.channel.fetch_message(message.id) # gets the message with id
    messageContent = message.content

Upvotes: 1

RiveN
RiveN

Reputation: 2653

Why are you using multiple on_message events? Use one:

@client.event
async def on_message(message):
    print(message.content)

    ctx = await client.get_context(message)
    if ctx.valid:
        await client.invoke(ctx)

And also remember that you have to enable intents.messages.

Upvotes: 4

Related Questions