【问题标题】:Discord bot - JDAs Event Listener sending mutiple messagesDiscord bot - 发送多条消息的 Js 事件监听器
【发布时间】:2022-01-06 06:32:06
【问题描述】:

我正在使用 JDA 创建一个不和谐的机器人,但我是一个新手,现在我的机器人正在工作,但它正在发送多条消息,我认为每次运行代码时它都会发送一条,我认为这是关于事件侦听器,但不确定也不知道如何解决,有人可以帮我解决这个问题吗?谢谢。

这是我的主文件:

package en.devck.dc;

import javax.security.auth.login.LoginException;

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Activity.ActivityType;

public class devck {
    public static JDA jda;
    public static String prefix = "*";
    
    // Main method
    public static void main(String[] args) throws LoginException {
        jda = JDABuilder.createDefault("my token").build();
        jda.getPresence().setStatus(OnlineStatus.ONLINE);
        Activity act = Activity.of(ActivityType.WATCHING, "Cowboy Bebop");
        jda.getPresence().setActivity(act);
        
        Commands cmd = new Commands();
        jda.addEventListener(cmd);
        
    }
            
    
}

这是我的命令文件:

package en.devck.dc;

import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class Commands extends ListenerAdapter{
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String[] args = event.getMessage().getContentRaw().split("\\s+"); 
        
        if(args[0].startsWith(devck.prefix)) {
            args[0] = args[0].substring(1);
            switch (args[0]) {
            case "info":
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("Hey there! There is a new bot over here").queue();
                break;
            case "greet":
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage(event.getAuthor().getName()+" is sending greetings to "+args[1]).queue();
                break;
            default:
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("I did not understand your command... but I'm learning!").queue();
                break;
            }
            
        }
    }

}

当我输入“*info”时,它会说:

  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”
  • “嘿!这里有一个新机器人”

【问题讨论】:

  • 您需要先停止机器人,然后再重新启动(否则您会多次运行机器人)。如果您使用的是 Eclipse,控制台中有一个红色方块用于停止应用程序,两个X-按钮用于清除已停止应用程序的输出。
  • 是的!现在它工作正常!谢谢!

标签: java discord bots chatbot discord-jda


【解决方案1】:

根据您的 IDE,您可能需要停止运行 Java 应用程序。

Eclipse: 顶栏 -> 调试/运行工具 -> Red Square(这可能有一个数字显示同时运行的实例数量)

Intellij

如何防止多个机器人同时运行(仅限 Intellij)

  • 在您的运行选项中禁用“允许多个实例”。

【讨论】:

  • 哇,好用,谢谢大佬
  • 没问题,如果这解决了问题,或者其他人的评论,不要忘记点击复选标记使其成为解决方案!
猜你喜欢
  • 2021-01-12
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2023-03-22
  • 2020-12-23
  • 2020-12-01
  • 2021-03-02
  • 2021-06-05
相关资源
最近更新 更多