更改Java中Discord机器人的前缀

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

Change prefix for Discord bot in Java

问题

如何在Java中更改我的Discord机器人的前缀?以下是我的代码:

package Rekt.YourAssistant;

import javax.security.auth.login.LoginException;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;

public class Main {
    public static JDA jda;
    
    // 主方法
    public static void main(String[] args) throws LoginException {
        jda = JDABuilder.createDefault("NzU4MTIxMTkwMjExNDUyOTQ4.X2qVYA.aW6C3UrekBZQrmA-AmMxYEMyibI").build();
    }
}

注意:上述代码中的令牌("NzU4MTIxMTkwMjExNDUyOTQ4.X2qVYA.aW6C3UrekBZQrmA-AmMxYEMyibI")是机密令牌,如果您在公开场合使用,请确保不要泄露此令牌。

英文:

How do I change my Discord bot's prefix in Java? Here is my code:

package Rekt.YourAssistant;
 
import javax.security.auth.login.LoginException;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;

public class Main {
	public static JDA jda;
	
	//Main Method
	public static void main(String[] args) throws LoginException {
		jda = JDABuilder.createDefault("NzU4MTIxMTkwMjExNDUyOTQ4.X2qVYA.aW6C3UrekBZQrmA-AmMxYEMyibI").build();
	}
}

答案1

得分: 1

我对JDA一无所知,但似乎它默认没有命令处理程序 - 你需要找到并安装一个,或者自己编写一个。

因此,前缀不是预先管理的概念。以下是JDA Github示例展示如何创建一个命令(我进行了简化以获取相关信息):

public class MessageListenerExample extends ListenerAdapter
{
    public static void main(String[] args)
    {
       //启动机器人
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event)
    {
        //这些是JDA中的每个事件提供的
        JDA jda = event.getJDA();

        Message message = event.getMessage();           //收到的消息。

        String msg = message.getContentDisplay();              //这返回消息的人类可读版本。类似于客户端中看到的内容。

        if (event.isFromType(ChannelType.TEXT))         //如果此消息已发送到服务器文本频道
        {
           if (msg.equals("!ping"))
           {
             //做一些事情
           }
           if (msg.startsWith("!alias"))
           {
             //做另一件事情
           }
        }
     }
}

请注意,msg.equalsmsg.startsWith 方法针对具有其自身前缀的字符串进行操作:!

我强烈建议您阅读整个示例,因为我只剪切出了前缀部分。

附言:您应该重新生成您在问题中发布的机器人令牌。它现在在互联网上,有了它,人们可以控制您的机器人。当您发布另一个问题时,您只需编写“my-token-goes-here”,人们就会知道您的意思。

英文:

I do not know anything about the JDA but it appears not to have a command handler out of the box - you'd need to find and install one or write your own.
Therefore, prefixes are not a pre-managed concept. Here's is how the JDA Github example shows making one command (simplified by me to get to the relevant information):

public class MessageListenerExample extends ListenerAdapter
{
    public static void main(String[] args)
    {
       //start bot
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event)
    {
        //These are provided with every event in JDA
        JDA jda = event.getJDA();

        Message message = event.getMessage();           //The message that was received.

        String msg = message.getContentDisplay();              //This returns a human readable version of the Message. Similar to
                                                        // what you would see in the client.

        if (event.isFromType(ChannelType.TEXT))         //If this message was sent to a Guild TextChannel
        {
           if (msg.equals("!ping"))
           {
             //do something
           }
           if (msg.startsWith("!alias"))
           {
             //do something else
           }
        }
     }
}

Notice that the if msg.equals or msg.startsWith methods act on strings with their own prefix: !

I highly recommend you read the entire example as there is a LOT I cut out to just show the prefix part.

P.S. You should regenerate the Bot Token you posted in your question. It's on the internet now, and with it people can take control of you bot. When you post another question you can just write "my-token-goes-here" and people will know what you mean.

答案2

得分: 0

可以使用GuildMessageReceivedEvent,然后使用以下代码:

// 将此前缀字符串设置为您想要的前缀
String prefix = "/";
if (event.getMessage().getContentRaw.equals(prefix + "help")) {
   event.getMessage().getChannel().sendMessage("您使用了 /help 命令");
}
英文:

You can use a GuildMessageReceivedEvent and then use

// Set This Prefix String To Whatever You want As Your Prefix
String prefix = "/"
if(event.getMessage().getContentRaw.equals(prefix + "help"){
   event.getMessage().getChannel().sendMessage("You used /help command");
}

huangapple
  • 本文由 发表于 2020年9月28日 05:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093472.html
匿名

发表评论

匿名网友

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

确定