【问题标题】:Why doesn't anything happen when I try to use this bot?为什么我尝试使用此机器人时没有任何反应?
【发布时间】:2022-10-20 02:55:39
【问题描述】:

我在正确的 Discord 频道和正确的服务器中输入消息。我认为它甚至没有检测到聊天中的消息,它只在名称终端中显示聊天名称。我正在关注这个 YT 教程:https://www.youtube.com/watch?v=fU-kWx-OYvE&ab_channel=Indently

import discord
import random

TOKEN = "MTAyOTc5OTI2MTUzMTAyNTQ4OQ.Gm2ElQ.-JLT11wrzHgXqzPIuCI8jZC3aCyN82KFyxWAQo"

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

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


@client.event
async def on_message(message):
    username = str(message.author).split("#")[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f"{username}: {user_message} ({channel})")

    if message.author == client.user:
        return
    
    if message.channel.name == "general":
        if user_message.lower() == "do it jeff":
            await message.channel.send(f"My name is Jeff")
            return
        elif user_message.lower() == "stop":
            await message.channel.send(f"NEVER {username}!")
            return

client.run(TOKEN)

【问题讨论】:

  • client = client = 看起来像是错字。
  • 刚刚修复了它仍然无法正常工作

标签: discord bots


【解决方案1】:

您的机器人无法读取在公会中发送的消息的内容,因为它缺少 message_content 意图。

您只为您的机器人提供了默认意图,其中不包括 message_content 之一。

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

以下是您修复它的方法:

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

如果您的机器人仍然无法读取公会消息的内容,您还需要在 Discord 开发者门户中打开消息内容意图:

选择您的应用 转到“机器人”选项卡 打开message content intent 现在您的机器人应该能够读取行会中发送的消息的内容。

【讨论】:

    猜你喜欢
    • 2021-05-13
    • 2021-02-05
    • 2021-01-23
    • 2015-04-20
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多