英文:
why is event.getMessage().getContentDisplay() not returning anything?
问题
It seems that you have a code snippet written in Java for a Discord bot. The issue you mentioned is related to the message display. It appears that the message is not being displayed correctly. Here's the corrected message part:
System.out.println("We have received a message from " + event.getAuthor().getName() + ": " +
event.getMessage().getContentDisplay());
You should remove the "
and use regular double quotes to display the message correctly.
英文:
` package com.ahwak;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import javax.security.auth.login.LoginException;
public class DiscordBot extends ListenerAdapter {
@Override
public void onMessageReceived(MessageReceivedEvent event)
{
System.out.println("We have received a message from " + event.getAuthor().getName() + ": " + event.getMessage()
.getContentDisplay());
if(event.getMessage().getContentRaw().equals("!ping")){
event.getChannel().sendMessage("Pong!").queue();
}
}
public static void main(String[] args) throws LoginException {
JDA bot = JDABuilder.createDefault("TOKEN")
.setActivity(Activity.playing("Working on this bot")).build();
bot.addEventListener(new DiscordBot());
}
}`
It is supposed to return Pong! when !ping is typed but it is not working. It works correctly when I enter a message however the message does not get displayed. Meaning it outputs "We have received a message from Rexstech: ".
答案1
得分: 0
请确保在Discord Portal的Bot设置中启用了消息内容意图(Message Content Intent):
并且您还需要在您的代码中启用该意图:
JDA bot = JDABuilder.createDefault("TOKEN")
.setActivity(Activity.playing("正在开发中的机器人"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT).build();
正如您可以在这篇文章中所阅读的,您需要启用消息内容意图才能接收消息的内容。
英文:
Be sure that you have the Message Content Intent enabled in your Bot settings (in the Discord Portal):
And you also want to enable the Intent in your code:
JDA bot = JDABuilder.createDefault("TOKEN")
.setActivity(Activity.playing("Working on this bot"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT).build();
As you can read in this article of Discord, you can see that you need to enable the Message Content Intent to receive the content of messages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论