英文:
Creating a countdown timer in a Discord message using JDA
问题
我正在尝试在JDA中创建一个倒计时器。不过我认为我做得不对。原始输入的命令将会被不断地编辑以显示进行中的倒计时。倒计时的最长时间为48小时,就像在for循环中看到的那样。以下是我编写的代码:
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
super.onGuildMessageReceived(event);
//!countdown 小时:分钟:秒钟
if (event.getMessage().getContentRaw().startsWith("!countdown")) {
String timebuilder = "";
char[] chararr = event.getMessage().getContentRaw().toCharArray();
for (int i = 11; i < chararr.length; i++) {
timebuilder += chararr[i];
}
int hour = Integer.parseInt(timebuilder.substring(0, 2)); // 更正这里的索引
int minute = Integer.parseInt(timebuilder.substring(3, 5)); // 更正这里的索引
int second = Integer.parseInt(timebuilder.substring(6, 8)); // 更正这里的索引
for (int i = 0; i < 172800; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (hour == 0 && minute == 0 && second == 0) {
event.getMessage().editMessage("00:00:00").queue();
event.getChannel().sendMessage("Countdown complete!").queue();
break;
} else if (hour == 0 && minute > 0 && second > 0) {
minute--;
second--;
event.getMessage().editMessage(String.format("%02d:%02d:%02d", hour, minute, second)).queue();
} else if (hour == 0 && minute > 0 && second == 0) {
minute--;
second = 59;
event.getMessage().editMessage(String.format("%02d:%02d:%02d", hour, minute, second)).queue();
} else if (hour > 0 && minute == 0 && second == 0) {
second = 59;
minute = 59;
hour--;
event.getMessage().editMessage(String.format("%02d:%02d:%02d", hour, minute, second)).queue();
} else if ((hour == 0 && minute == 0 && second > 0) || (hour > 0 && minute == 0 && second > 0) || (hour > 0 && minute > 0 && second > 0)) {
second--;
event.getMessage().editMessage(String.format("%02d:%02d:%02d", hour, minute, second)).queue();
} else if (hour > 0 && minute > 0 && second == 0) {
minute--;
event.getMessage().editMessage(String.format("%02d:%02d:%02d", hour, minute, second)).queue();
}
}
}
}
这个代码似乎有更简单的方式来实现。你可以考虑使用Timer
或SimpleDateFormat
之类的东西,不过我不太清楚具体应该如何操作。出于某些原因,我的消息没有通过.editMessage()
方法进行编辑。
输出:
!countdown 01:02:02 //用户输入的Discord机器人命令
对于预期输出,"!countdown 01:02:02" 的消息应该会不断地被更新的倒计时所编辑,就像下面展示的那样:
预期输出:
!countdown 01:02:02 //用户输入的Discord机器人命令
01:02:01 //原始命令
01:02:00 //原始命令
01:01:59 //原始命令
// 更多类似的倒计时更新...
英文:
I am trying to create a countdown timer in JDA. I do not think I am doing it right though. The original command entered would be constantly edited to show the countdown in progress. The countdown would have a max time of 48 hours as seen in the for loop. Here is the code I have:
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
super.onGuildMessageReceived(event);
//!countdown hours:minutes:seconds
if (event.getMessage().getContentRaw().startsWith("!countdown")) {
String timebuilder = "";
char[] chararr = event.getMessage().getContentRaw().toCharArray();
for (int i = 11; i < chararr.length; i++) {
timebuilder += chararr[i];
}
// event.getChannel().sendMessage(timebuilder).queue();
int hour = Integer.parseInt(timebuilder.substring(0, 1));
int minute = Integer.parseInt(timebuilder.substring(3, 4));
int second = Integer.parseInt(timebuilder.substring(6, 7));
for (int i = 0; i < 172800; i++ ) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 0 0 0
if (hour == 0 && minute == 0 && second == 0) {
event.getMessage().editMessage("00:00:00");
event.getChannel().sendMessage("Countdown complete!").queue();
break;
}
// 0 1 1
else if (hour == 0 && minute > 0 && second > 0) {
minute--;
second--;
event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
}
//0 1 0
else if (hour == 0 && minute > 0 && second == 0) {
minute--;
second = 59;
event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
}
//1 0 0
else if (hour > 0 && minute == 0 && second == 0) {
second = 59;
minute = 59;
hour--;
event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();;
}
//0 0 1 //1 0 1 //1 1 1
else if ((hour == 0 && minute == 0 && second > 0) || (hour > 0 && minute == 0 && second > 0) || (hour > 0 && minute > 0 && second > 0)) {
second--;
event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();
}
//1 1 0
else if (hour > 0 && minute > 0 && second == 0) {
minute--;
event.getMessage().editMessage(hour + ":" + minute + ":" + second).queue();
}
}
}
}
It seems like there would be an easier way to do this. With Timer or SimpleDateFormat or something but I just don't know what that would be. For some reason my message is not being edited with the .editMessage()
method.
Output:
!countdown 01:02:02 //user keyed discord bot command
For the expected output, the "!countdown 01:02:02" message should be continually edited with the updated countdown. As shown below:
Expected output:
!countdown 01:02:02 //user keyed discord bot command
01:02:01 //original command
01:02:00 //original command
01:01:59 //original command
etc...
答案1
得分: 1
你应该能够使用 Timer 和 TimerTask,Timer 用于调度 Timer Task。定时器通常用于在后台线程中调度任务,代码如下:
TimerTask task = new TimerTask(){
public void run(){
System.out.println("要执行的代码");
}
};
Timer timer = new Timer();
// 延迟时间以毫秒为单位进行计算
timer.schedule(task, 5*1000);
英文:
You should be able to use a Timer and a TimerTask, the Timer is used to schedule the Timer Task. Timers are used to schedule tasks in the background of a thread like so:
TimerTask task = new TimerTask(){
public void run(){
System.out.println("Code to be executed");
}
};
Timer timer = new Timer();
// The delay period is calculated in milliseconds iirc
timer.schedule(task, 5*1000);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论