Discord 机器人中的“空消息”问题

0 投票
0 回答
27 浏览
提问于 2025-04-12 03:10

我在写一个Python脚本,做的是一个Discord机器人,但遇到了问题。这个脚本总是检测到“空消息”,可是其实发到服务器的消息是有内容的,里面是一些命令。我想知道有没有办法解决这个问题,但我还没找到。

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if not message.content:
        print(f"Received empty message from user ID: {message.author.id}")
        return

    messages = [message]
    if message.reference is not None:
        referenced_message = await message.channel.fetch_message(message.reference.message_id)
        if referenced_message.author == client.user and referenced_message.content.startswith('User ID: '):
            messages.insert(0, referenced_message)

    for msg in messages:
        print(f"Received message: '{msg.content}' from user ID: {msg.author.id}")

        if msg.content.startswith('!yes'):
            # Move approved data to a separate file (e.g., approved.json)
            with open('approved.json', 'a') as file:
                json.dump(data.pop(0), file, indent=4)
            await send_next_data()
            await msg.channel.send('Data approved.')
            print(f"User Approved the Shown User.")

        elif msg.content.startswith('!no'):
            # Move rejected data to a separate file (e.g., rejected.json)
            with open('rejected.json', 'a') as file:
                json.dump(data.pop(0), file, indent=4)
            await send_next_data()
            await msg.channel.send('Data rejected.')
            print(f"User Rejected the Shown User.")

为了调试这个问题,我加了一些额外的打印信息,比如 print(f"收到来自用户ID: {message.author.id} 的空消息"),还加了 print(f"用户批准了显示的用户。")print(f"用户拒绝了显示的用户。"),还有其他代码的小改动。

0 个回答

暂无回答

撰写回答