Improve timer

This commit is contained in:
syuilo 2018-08-26 09:12:48 +09:00
parent ac3abc0210
commit 1dc0bf27e9

View file

@ -12,29 +12,31 @@ export default class TimerModule implements IModule {
} }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
const seconds = (msg.text || '').match(/([0-9]+)秒/); const secondsQuery = (msg.text || '').match(/([0-9]+)秒/);
const minutes = (msg.text || '').match(/([0-9]+)分/); const minutesQuery = (msg.text || '').match(/([0-9]+)分/);
const hours = (msg.text || '').match(/([0-9]+)時間/); const hoursQuery = (msg.text || '').match(/([0-9]+)時間/);
const timeStr = seconds || minutes || hours;
if (timeStr) { const seconds = secondsQuery ? parseInt(secondsQuery[1], 10) : 0;
const num = parseInt(timeStr[1], 10); const minutes = minutesQuery ? parseInt(minutesQuery[1], 10) : 0;
const hours = hoursQuery ? parseInt(hoursQuery[1], 10) : 0;
if (num <= 0) { if (secondsQuery || minutesQuery || hoursQuery) {
if ((seconds + minutes + hours) == 0) {
msg.reply(serifs.timer.invalid); msg.reply(serifs.timer.invalid);
return true; return true;
} else { } else {
msg.reply(serifs.timer.set); msg.reply(serifs.timer.set);
const time = const time =
seconds ? 1000 * num : (1000 * seconds) +
minutes ? 1000 * 60 * num : (1000 * 60 * minutes) +
hours ? 1000 * 60 * 60 * num * 1000 : (1000 * 60 * 60 * hours);
null;
const str = `${hours ? hoursQuery[0] : ''}${minutes ? minutesQuery[0] : ''}${seconds ? secondsQuery[0] : ''}`;
setTimeout(() => { setTimeout(() => {
this.ai.sendMessage(msg.userId, { this.ai.sendMessage(msg.userId, {
text: serifs.timer.notify.replace('{time}', timeStr[0]) text: serifs.timer.notify.replace('{time}', str)
}); });
}, time); }, time);