#πŸ”’ Can't recieve last message from an user discord bot

32 messages Β· Page 1 of 1 (latest)

bright coral
#

Hi I'm doing a simple discord bot which will retrieve the last message send on a channel then will do a command x based on it but when I try to print the last message made by an user Y it show me nothing I don't know why code: ```py
import discord
import subprocess
import os

Your bot token (replace 'YOUR_BOT_TOKEN' with the actual token)

TOKEN = 'd'

Initialize the bot

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
print(f'Logged in as {client.user}')

@client.event
async def on_message(message):
# Ignore messages from the bot itself

if message.author == client.user:
    return

# Print the last message sent by any user to the console
print(f"Last message from {message.author}: {message.content}")

# Check if the message content is "dodo"
# print("test " , message.content, " toto " , message.author)
# if message.content.strip().lower() == 'dodo':
#     # Run the dodo.py script
#     try:
#         # Run dodo.py using subprocess
#         result = subprocess.run(['python', 'dodo.py'], capture_output=True, text=True)

#         # Send the output back to the channel
#         if result.stdout:
#             await message.channel.send(f"Output:\n{result.stdout}")
#         if result.stderr:
#             await message.channel.send(f"Error:\n{result.stderr}")

#     except Exception as e:
#         await message.channel.send(f"An error occurred: {e}")

Run the bot

client.run(TOKEN)

glass zealotBOT
#

@bright coral

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

unique ravine
#

Show the console output and what you have done during the bot is online

#

Also

bright coral
unique ravine
#

!d discord.Intents.default

glass zealotBOT
#

classmethod default()```
A factory method that creates a [`Intents`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents) with everything enabled except [`presences`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.presences), [`members`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.members), and [`message_content`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.message_content).
bright coral
#

Don't get it

unique ravine
#

In this mode, the bot cannot see the content you sent unless you mention the bot or send it in the direct message to the bot

bright coral
#

wait

#

so i need to dm the bot ?

unique ravine
#

You can use

bright coral
#

Or my params are bad ?

unique ravine
#

!d discord.Intents.all

glass zealotBOT
#

classmethod all()```
A factory method that creates a [`Intents`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents) with everything enabled.
bright coral
#

Where do I put this in my code ?

unique ravine
#

Replace the default to all in your code

#

This include additional configuration on the bot in discord developer portal

bright coral
#

ok wait

#

is this fine ?

unique ravine
#

Yes

#

For the intents

bright coral
#

doesn't seems to work

bright coral
#

Well did the exact same thing but I used telegram instead

#

Instead

#

Worked well

#

Discord seems weird idk + it wasn't the first time I did discord bot just copy and pasted a code from two years ago which was working fine

bright coral
#

!close

glass zealotBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.