event.getMessage().getContentDisplay() 为什么没有返回任何内容?

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

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):

event.getMessage().getContentDisplay() 为什么没有返回任何内容?

并且您还需要在您的代码中启用该意图:

JDA bot = JDABuilder.createDefault("TOKEN")
            .setActivity(Activity.playing("正在开发中的机器人"))
            .enableIntents(GatewayIntent.MESSAGE_CONTENT).build();

查看JDA文档

正如您可以在这篇文章中所阅读的,您需要启用消息内容意图才能接收消息的内容。

英文:

Be sure that you have the Message Content Intent enabled in your Bot settings (in the Discord Portal):

event.getMessage().getContentDisplay() 为什么没有返回任何内容?

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();

Link to JDA Docs

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.

huangapple
  • 本文由 发表于 2023年4月17日 22:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036514.html
匿名

发表评论

匿名网友

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

确定