NameError Discord.py文件+日志记录

时间:2020-11-05 23:28:11

标签: python discord.py

我正在尝试使用on_message事件将消息和有关消息的信息写入我的文件logs.txt中,但是出现错误;

回溯(最近通话最近): _run_event中的第333行“ C:\ Users \ arsto \ AppData \ Local \ Packages \ PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0 \ LocalCache \ local-packages \ Python38 \ site-packages \ discord \ client.py” 等待科罗(* args,** kwargs) 文件“ C:\ Users \ arsto \ Desktop \ Discord Bot Coding \ DBL-Bot \ bot.py”,第39行,位于on_message中 user_id = message.author.id NameError:名称“ message”未定义

如果我在Self之前加上Message,则会遇到相同的错误,但提示为Self。

我的代码;

@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 个答案:

答案 0 :(得分:0)

@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!")