为什么我的Discord机器人程序返回message.content为空?

huangapple go评论71阅读模式
英文:

Why is my Discord Bot program returning message.content as empty?

问题

以下是代码的翻译部分:

import os
import discord
from discord import Intents
from dotenv import load_dotenv
from chatbot import ChatBot

load_dotenv()
discord_token = os.getenv('DISCORD_TOKEN')

chatbot = ChatBot()

intents = Intents.default()
intents.typing = False
intents.presences = False
intents.messages = True  # 启用消息意图

client = discord.Client(intents=intents)

chat_history = []

@client.event
async def on_ready():
    print(f'已登录为 {client.user}')

@client.event
async def on_message(message):
    print("收到消息")  # 添加此行
    if message.author == client.user:  # 忽略来自机器人自身的消息
        return
    print('收到消息:', message)
    print(f"收到消息内容: {message.clean_content}")
    if message.content.startswith('/ask'):
        user_input = message.content[5:].strip()
        print('用户输入: ', repr(user_input))
        print('收到用户输入: {}'.format(user_input))  # 打印用户的问题到控制台
        response = chatbot.get_response(user_input, chat_history)
        await message.channel.send(response)

client.run(discord_token)

控制台输出:

已登录为 ask-Bot#1060
收到消息
收到消息: <Message id=1117888736563306637 channel= type=<MessageType.default: 0> author=<Member id=940462143654998096 name='Dane W' discriminator='4497' bot=False nick=None guild=> flags=>

我在Discord中输入“/ask tell me about the striking offense of brandon moreno”。我期望我的控制台返回该值作为message.content。

Discord机器人已被授予所有必要的权限,并存在于我要发送消息的频道中。

英文:
import os
import discord
from discord import Intents
from dotenv import load_dotenv
from chatbot import ChatBot

load_dotenv()
discord_token = os.getenv(&#39;DISCORD_TOKEN&#39;)

chatbot = ChatBot()

intents = Intents.default()
intents.typing = False
intents.presences = False
intents.messages = True  # Enable the messages intent

client = discord.Client(intents=intents)


chat_history = []

@client.event
async def on_ready():
    print(f&#39;We have logged in as {client.user}&#39;)

@client.event
async def on_message(message):
    print(&quot;Received a message&quot;)  # Add this line
    if message.author == client.user:  # Ignore messages from the bot itself
        return
    print(&#39;Received message:&#39;, message)
    print(f&quot;Received message content: {message.clean_content}&quot;)
    if message.content.startswith(&#39;/ask&#39;):
        user_input = message.content[5:].strip()
        print(&#39;User input: &#39;, repr(user_input))
        print(&#39;Received user input: {}&#39;.format(user_input)) # Print the user&#39;s question to the console
        response = chatbot.get_response(user_input, chat_history)
        await message.channel.send(response)




client.run(discord_token)

Console:

We have logged in as ask-Bot#1060
Received a message
Received message: <Message id=1117888736563306637 channel=<TextChannel id=1117859481070616641 name='general' position=0 nsfw=False news=False category_id=1117859481070616638> type=<MessageType.default: 0> author=<Member id=940462143654998096 name='Dane W' discriminator='4497' bot=False nick=None guild=<Guild id=1117859480428871700 name="Dane W's Server" shard_id=0 chunked=False member_count=2>> flags=<MessageFlags value=0>>
Received message content:

I typed "/ask tell me about the striking offense of brandon moreno" into Discord. I expected my console to return that value as message.content.

The Discord bot has been given all necessary permissions and exists in the channel I am typing to message into.

答案1

得分: 1

你的代码中缺少 intents.message_content = True
intents.messages 不再足够。

在这里了解更多信息:message_content

英文:

You are missing intents.message_content = True in your code.
intents.messages is not enough anymore.

Find out more about it here: message_content

huangapple
  • 本文由 发表于 2023年6月13日 02:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定