0
votes

I am trying to use the on_message event to write messages and information about the messages into my file logs.txt however I am getting the error;

Traceback (most recent call last): File "C:\Users\arsto\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) File "C:\Users\arsto\Desktop\Discord Bot Coding\DBL-Bot\bot.py", line 39, in on_message user_id = message.author.id NameError: name 'message' is not defined

and if I put Message before Self I get the same error but it says Self.

My code;

@bot.event
async def on_message(self, message):
    user_id = message.author.id
    message_id = message.id
    content = message.content
    message_channel = message.channel.id
    guildid = message.guild.id
    attachment = message.attachments
    if user.bot:
        return
    with open('logs.txt','w') as file:
        file.write(f"New Message > Server: {guildid} | User: {user_id} | Message: {messageid} / {content} | Channel: {message_channel} | Attachment: {attachment}")
1

1 Answers

0
votes
@bot.event
async def on_message(message):
    user_id = message.author.id
    message_id = message.id
    content = message.content
    message_channel = message.channel.id
    guildid = message.guild.id
    attachment = message.attachments
    with open('logs.txt','w+') as file:
        file.write(f"New Message > Server: {guildid} | User: {user_id} | Message: {message_id} / {content} | Channel: {message_channel} | Attachment: {attachment}")
        print("Message logged!")