【问题标题】:Timerank command is not working without any errorsTimerank 命令无法正常工作,没有任何错误
【发布时间】:2020-03-21 03:37:29
【问题描述】:

为什么我的命令没有运行?

我的 discord 机器人有一个命令 (~timerank @user)。机器人应该给用户一个角色。但是命令没有运行。编译时我没有收到任何错误。我像所有其他命令一样注册了我的命令:

public class CommandManager {

    public ConcurrentHashMap<String, ServerCommand> commands;
    public CommandManager() {

        this.commands = new ConcurrentHashMap<>();

        this.commands.put("clear", new ClearCommand());
        this.commands.put("preview", new PreviewCommand());
        this.commands.put("client", new ClientInfoCommand());
        this.commands.put("help", new HelpCommand());
        this.commands.put("vote", new VoteCommand());
        this.commands.put("play", new PlayCommand());
        this.commands.put("stop", new StopCommand());
        this.commands.put("trackinfo", new TrackInfoCommand());
        this.commands.put("shuffle", new ShuffleCommand());
        this.commands.put("statchannel", new StatChannelCommand());
        this.commands.put("timerank", new TimeRankCommand());
    }

    public boolean perform(String command, Member m, TextChannel channel, Message message, MessageReceivedEvent event) {

        ServerCommand cmd;

        if((cmd = this.commands.get(command.toLowerCase())) != null) {
            cmd.performCommand(m, channel, message, event);
            return true;
        }
        return false;
    }
}

这是我的命令:

public class TimeRankCommand implements ServerCommand {

    @Override
    public void performCommand(Member m, TextChannel channel, Message message, MessageReceivedEvent event) {

        //~timerank @User
        if (m.hasPermission(Permission.ADMINISTRATOR)) {
            List<Member> members = message.getMentionedMembers();

            if(members.size() >= 1) {
                for(Member memb : members) {

                    Guild guild = channel.getGuild();
                    Role role = guild.getRoleById(648047607486087168l);

                    if(memb.getRoles().contains(role)) {

                        guild.addRoleToMember(memb, role).queue();

                        LiteSQL.onUpdate("INSERT INTO timeranks(userid, guildid) VALUES(" + memb.getIdLong() + ", " + guild.getIdLong() + ")");

                        EmbedBuilder builder = new EmbedBuilder();
                        builder.setTitle("Timerank");
                        builder.setThumbnail("http://i.epvpimg.com/toDBaab.png");
                        builder.setFooter("Powered by Backxtar.");
                        builder.setTimestamp(OffsetDateTime.now());
                        builder.setColor(0xf22613);
                        builder.setDescription(memb.getAsMention() + " is **MUTED** for 15 minutes!");
                        channel.sendMessage(builder.build()).queue();
                    }
                }
            }
        }
    }
}

这是我的 SQLManager:

public class SQLManager {

    public static void onCreate() {

        //TimeRank
        LiteSQL.onUpdate("CREATE TABLE IF NOT EXISTS timeranks(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, userid INTEGER, guildid INTEGER, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
    }

}

有人知道为什么命令没有运行吗?如果它是一个 SQL 问题,我至少会在函数中得到消息..

顺便说一句,这是我在 15 分钟后删除角色的功能。 (该函数总是在 1 分钟后被调用):

public void onCheckTimeRanks() {
        ResultSet set = LiteSQL.onQuery("SELECT userid, guildid FROM timeranks WHERE ((julianday(CURRENT_TIMESTAMP) - julianday(time)) * 1000) >= 15");

        try {
            while(set.next()) {
                long userid = set.getLong("userid");
                long guildid = set.getLong("guildid");

                Guild guild = this.shardMan.getGuildById(guildid);
                guild.removeRoleFromMember(guild.getMemberById(userid), guild.getRoleById(648047607486087168l)).complete();

                LiteSQL.onUpdate("DELETE FROM timeranks WHERE userid = " + userid);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: java discord discord-jda


    【解决方案1】:

    两件事:

    1. 我相信 if 语句中的代码应该是 if(!memb.getRoles().contains(role)) { 而不是 if(memb.getRoles().contains(role)) {,因为您似乎想要搜索用户没有静音角色。 (这似乎是您的命令正在执行的操作)目前,您的命令仅搜索具有该角色的用户。
    2. 我不熟悉 SQLite 如何存储时间戳,但如果它的工作方式与所有其他系统存储时间戳的方式相同(它可能会这样做),那么您应该改为使用 "SELECT userid, guildid FROM timeranks WHERE ((julianday(CURRENT_TIMESTAMP) - julianday(time)) /1000 /60) &gt;= 15" 而不是 "SELECT userid, guildid FROM timeranks WHERE ((julianday(CURRENT_TIMESTAMP) - julianday(time)) * 1000) &gt;= 15"。这将使其仅对时间大于 15 分钟而不是大于 0.015 毫秒的用户激活。

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多