英文:
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=
我在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('DISCORD_TOKEN')
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'We have logged in as {client.user}')
@client.event
async def on_message(message):
print("Received a message") # Add this line
if message.author == client.user: # Ignore messages from the bot itself
return
print('Received message:', message)
print(f"Received message content: {message.clean_content}")
if message.content.startswith('/ask'):
user_input = message.content[5:].strip()
print('User input: ', repr(user_input))
print('Received user input: {}'.format(user_input)) # Print the user'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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论