Discord Bot(JDA)并未修改我的角色

时间:2020-07-15 14:39:17

标签: discord discord-jda

我对Discord Bot进行了编程,以使其成为时间角色(该角色每秒将其名称更改为:“ {UserName} --D --H --min --sec)。

它工作正常,但是一个小时后它不再工作,但是我没有更改代码中的任何内容。

我有一个命令(“ {prefix} shutdown”),它基本上执行ShardManager.shutdown();。然后我得到一个错误。

命令代码:

package Discord.Commands.TimeRank;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;

import Discord.Bot;
import Discord.Commands.Command;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.RateLimitedException;

public class startTimeRole extends Command {
    public static Server server;

    public Timer timer;
    public static boolean isRunning = false;

    @Override
    public void set(String prefix, String help) {

        prefix = "timeRole";
        help = Bot.prefix + " timeRole <STATE> -> STATE = start, pause";

        super.set(prefix, help);
    }

    @Override
    public void Execute(MessageReceivedEvent event, String[] args) {
        super.Execute(event, args);

        server = server.get(event.getGuild());

        if (args.length <= 1) {
            event.getTextChannel().sendMessage("Please give at least one **Argument**!").queue();
            help(event);
            return;
        } else {
            if (args[1].equals("start")) {
                if (!isRunning) {
                    timer = new Timer();
                    event.getTextChannel().sendMessage("Activated timeRole!").queue();
                    isRunning = true;
                    timer.scheduleAtFixedRate(new TimerTask() {
                        public void run() {

                            for (Client client : server.onlineClients) {
                                Role timeRole = null;

                                for (Role role : client.member.getRoles()) {
                                    if (timeRole != null) {
                                        break;
                                    }
                                    if (role.getName().startsWith(client.member.getUser().getName())) {
                                        
                                        client.onlineTime.setTime(role.getName().substring(client.member.getUser().getName().length() + 1));
                                        
                                        client.onlineTime.plus(10);

                                        timeRole = role;
                                    }
                                }
                                if (timeRole != null) {
                                    timeRole.getManager()
                                            .setName(client.member.getUser().getName() + " " + client.onlineTime.get())
                                            .queue();
                                } else {
                                    try {
                                        server.guild
                                                .addRoleToMember(client.member,
                                                        server.guild.createRole()
                                                                .setName(client.member.getUser().getName() + " "
                                                                        + client.onlineTime.get())
                                                                .complete(true))
                                                .queue();
                                    } catch (RateLimitedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }, 0, 10000);
                    return;
                } else {
                    event.getTextChannel().sendMessage("timeRole is Active!").queue();
                    help(event);
                    return;
                }
            } else if (args[1].equals("pause")) {
                if (isRunning) {
                    timer.cancel();
                    event.getTextChannel().sendMessage("Paused timeRole").queue();
                    ;
                    isRunning = false;
                } else {
                    event.getTextChannel().sendMessage("timeRole is Paused!").queue();
                    help(event);
                    return;
                }
                return;
            } else {
                event.getTextChannel().sendMessage("Please give one **Argument**").queue();
                help(event);
                return;
            }
        }
    }
}

“服务器”类的代码:

package Discord.Commands.TimeRank;

import java.util.ArrayList;

import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;

public class Server {
    public ArrayList<Client> clients = new ArrayList<Client>();
    public ArrayList<Client> onlineClients = new ArrayList<Client>();

    public Guild guild;

    private Server(Guild guild) {
        this.guild = guild;
    }

    public static Server get(Guild guild) {
        Server server = new Server(guild);

        server.clients = server.getMembersAsClients();
        server.onlineClients = server.getOnlineMembersAsClients();
        
        return server;
    }

    public ArrayList<Client> getMembersAsClients() {
        ArrayList<Client> clients = new ArrayList<Client>();

        System.out.println(guild.getMembers().size());

        for (Member member : guild.getMembers()) {
            if (!member.getUser().isBot()) {
                clients.add(new Client(member));
            }
        }

        return clients;
    }

    public ArrayList<Client> getOnlineMembersAsClients() {
        ArrayList<Client> onlineClients = new ArrayList<Client>();

        for (Client client : clients) {
            if (!client.member.getUser().isBot()) {
                if (client.member.getOnlineStatus() == OnlineStatus.ONLINE) {
                    onlineClients.add(client);
                }
            }
        }
        return onlineClients;
    }
}

和时间类别:

package Discord.Commands.TimeRank;

public class Time {
    public int Days, Hours, Minutes, Seconds;

    public Time() {
    }

    public String get() {
        return Days + "D " + Hours + "H " + Minutes + "min " + Seconds + "sec ";
    }

    public void plus(int plus) {
        Seconds += plus;
        if (Seconds >= 60) {
            Seconds = 0;
            Minutes++;
            if (Minutes >= 60) {
                Minutes = 0;
                Hours++;
                if (Hours >= 24) {
                    Hours = 0;
                    Days++;
                    return;
                }
            }
        }
    }

    public void setTime(String Time) {
        String[] Time_ = Time.split(" ");

        int Days = Integer.parseInt(Time_[0].split("D")[0]);
        int Hours = Integer.parseInt(Time_[1].split("H")[0]);
        int Minutes = Integer.parseInt(Time_[2].split("min")[0]);
        int Seconds = Integer.parseInt(Time_[3].split("sec")[0]);

        this.Days = Days;
        this.Hours = Hours;
        this.Minutes = Minutes;
        this.Seconds = Seconds;
    }
}

错误:

[DefaultShardManager] INFO RateLimiter - Waiting for 1 bucket(s) to finish. Average queue size of 2 requests
Exception in thread "Timer-0" java.util.concurrent.RejectedExecutionException: The Requester has been stopped! No new requests can be requested!
    at net.dv8tion.jda.internal.requests.Requester.request(Requester.java:107)
    at net.dv8tion.jda.internal.requests.RestActionImpl.queue(RestActionImpl.java:189)
    at net.dv8tion.jda.internal.managers.ManagerBase.queue(ManagerBase.java:121)
    at net.dv8tion.jda.api.requests.RestAction.queue(RestAction.java:411)
    at net.dv8tion.jda.api.requests.RestAction.queue(RestAction.java:377)
    at Discord.Commands.TimeRank.startTimeRole$1.run(startTimeRole.java:67)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

很抱歉有这么多代码,但我认为您可能需要它来知道什么,什么是什么。

如果您有任何问题,请问他们。 :D

1 个答案:

答案 0 :(得分:0)

您受到速率限制。对api的不同请求对允许执行它们的频率有不同的限制。

您只是经常做。之所以切换机器人,是因为每个新机器人都从零开始,直到再次达到极限为止。

您可以减少请求的数量,或者必须使用一种可靠的切换机器人解决方案(不建议使用)。