【问题标题】:problem regarding my discord bot coded through jda关于我通过 jda 编码的不和谐机器人的问题
【发布时间】:2021-07-20 16:57:32
【问题描述】:

我的机器人在打印输出时卡住了。我检查了一下,逻辑部分没有问题,因为我使用了理智的逻辑来制作一个普通的 java 程序。请帮忙,因为它卡在不和谐的打印输出中,我不知道如何解决它。 我还添加了一些不必要的打印功能来找出错误所在。令我惊讶的是,这只是打印消息,这在我之前制作过机器人时并不常见,而且它们都没有任何错误来“打印”消息。

import javax.security.auth.login.LoginException;

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;

public class rpsidnfp {
    public static JDA jda;
    
    public static void main(String args[]) throws LoginException {
        jda = JDABuilder.createDefault("(my token here)").build();
        core2 core2obj = new core2();
        jda.addEventListener(core2obj);
    }
}


前者是我的主要课程。 下面是包含所有功能的核心类。

package pack.rpsidnfp;

import java.util.Random;

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


public class core2 extends ListenerAdapter {
    public static String prefix = "!";
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        String input = event.getMessage().getContentRaw();
        String[] options = {"rock", "paper", "scissors"};
        Random robj = new Random();
        int rnum = robj.nextInt(options.length);
        System.out.println(rnum);
        String conf = options[rnum];
        event.getChannel().sendMessage(conf);
        System.out.println(conf);
        String same = prefix + conf;
        String win = "congrats, you won!";
        String lose = "lmao, you lost";
        if(input.equals(same)) {
            event.getChannel().sendMessage("we both kept the same thing");
        }
        else if(input.equals(prefix + options[0])) {
            if(conf.equals(options[1])) {
                event.getChannel().sendMessage(lose);
            }
            else if(conf.equals(prefix + options[2])) {
                event.getChannel().sendMessage(win);
            }
        }
        else if(input.equals(prefix + options[1])) {
            if(conf.equals(options[0])) {
                event.getChannel().sendMessage(win);
            }
            else if(conf.equals(options[2])) {
                event.getChannel().sendMessage(lose);
            }
        }
        else if(input.equals(prefix + options[2])) {
            if(conf.equals(options[0])) {
                event.getChannel().sendMessage(lose);
            }
            else if(conf.equals(options[2])) {
                event.getChannel().sendMessage(win);
            }
        }

    }
    
}

【问题讨论】:

    标签: java eclipse discord discord-jda


    【解决方案1】:

    sendMessage 方法返回一个MessageAction。您需要在该 RestAction 实例上调用 queue()

    此外,请记住,您的机器人会收到自己的消息,因此您应该确保它会忽略这些消息。您可以在侦听器方法的开头添加if (event.getAuthor().isBot()) return;

    另见:

    【讨论】:

    • 非常感谢您的帮助!我忘了编辑我添加了 queue();命令在最后,但之后它不断循环。但是后来,我找到了你的回复,它已经解决了。再次感谢您帮助我解决这个问题:)。
    猜你喜欢
    • 2020-07-23
    • 2021-08-06
    • 2021-11-18
    • 2022-11-10
    • 2021-11-14
    • 2020-06-08
    • 2020-12-25
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多