清除命令删除一条消息(Java Discord JDA)
Clear Command Deleting One Less Message(Java Discord JDA)
我正在尝试做到这一点,所以每当有人执行 !clear [Amount] 时,它都会删除 [Amount] 中的消息数量。我遇到的问题是 !clear [Amount] 少删除了 1 条消息。例如,如果有人执行 !clear 3,它会删除“!clear 3”和它上面的 2 条消息。我想做的是,无论何时有人执行 !clear 3,它都会删除“!clear 3”并删除其上方的 3 条消息。有人可以帮我编辑我的代码吗?
这是我的代码:
package Events;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Clear extends ListenerAdapter {
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
String[] args = e.getMessage().getContentRaw().split("\s+");
if (args[0].equalsIgnoreCase("!clear")) {
if (args.length < 2) {
EmbedBuilder usage = new EmbedBuilder();
usage.setColor(0xff3923);
usage.setTitle("Specify amount to delete");
usage.setDescription("Usage: !clear [# of messages]");
e.getChannel().sendMessage(usage.build()).queue();
} else {
List<Message> messages = e.getChannel().getHistory().retrievePast(Integer.parseInt(args[1])).complete();
e.getChannel().deleteMessages(messages).queue();
e.getChannel().sendMessage("Messages has been deleted!").queue(m -> m.delete().queueAfter(5, TimeUnit.SECONDS));
}
}
}
}
编辑自:
.retrievePast(Integer.parseInt(args[1]))
至:
.retrievePast(Integer.parseInt(args[1])+1)
所有功劳归于dan1st(tysm <3)
我正在尝试做到这一点,所以每当有人执行 !clear [Amount] 时,它都会删除 [Amount] 中的消息数量。我遇到的问题是 !clear [Amount] 少删除了 1 条消息。例如,如果有人执行 !clear 3,它会删除“!clear 3”和它上面的 2 条消息。我想做的是,无论何时有人执行 !clear 3,它都会删除“!clear 3”并删除其上方的 3 条消息。有人可以帮我编辑我的代码吗?
这是我的代码:
package Events;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Clear extends ListenerAdapter {
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
String[] args = e.getMessage().getContentRaw().split("\s+");
if (args[0].equalsIgnoreCase("!clear")) {
if (args.length < 2) {
EmbedBuilder usage = new EmbedBuilder();
usage.setColor(0xff3923);
usage.setTitle("Specify amount to delete");
usage.setDescription("Usage: !clear [# of messages]");
e.getChannel().sendMessage(usage.build()).queue();
} else {
List<Message> messages = e.getChannel().getHistory().retrievePast(Integer.parseInt(args[1])).complete();
e.getChannel().deleteMessages(messages).queue();
e.getChannel().sendMessage("Messages has been deleted!").queue(m -> m.delete().queueAfter(5, TimeUnit.SECONDS));
}
}
}
}
编辑自:
.retrievePast(Integer.parseInt(args[1]))
至:
.retrievePast(Integer.parseInt(args[1])+1)
所有功劳归于dan1st(tysm <3)