[英]Command Timeout | Discord.js
目前我有這個:
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
var bot = new Discord.Client();
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not command ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array
switch (args[0].toLowerCase()) { //not case-sensitive anymore
case "hello":
message.channel.send("hello");
break;
//rest of the commands
我想限制命令“,你好”的使用。 我希望每次用戶輸入“,你好”時都有 10 秒的超時時間。 如果用戶在此冷卻之前輸入命令,它將發送一條消息,說明誰最后使用了該命令以及冷卻時間還剩多長時間。
這就是我想要的結果:
User1: ,hello
Bot: hello
(After 1 second)
User2: ,hello
Bot: User1 has already used this command, please wait another 9 seconds to use it again
(After 9 seconds)
User 2: ,hello
Bot: hello
感謝所有幫助。 謝謝,
您需要存儲使用該命令的最后日期,然后相應地分叉流程。 要同時顯示誰最后使用了該命令,您需要將該信息與時間戳一起存儲。
這是一個基於您的示例:
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
const bot = new Discord.Client();
let lastHelloCommandDate, lastHelloCommandUser;
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not command ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array
switch (args[0].toLowerCase()) { //not case-sensitive anymore
case "hello":
hello(message);
break;
//rest of the commands
}}})
})
function hello(message) {
const now = new Date();
if (now - lastHelloCommandDate > 10 * 60 * 1000) {
// It's been more than 10 mins
message.channel.send("hello");
lastHelloCommandDate = now;
lastHelloCommandUser = message.sender;
} else {
// It's been less than 10 mins
// send a direct message to the user
// i don't know if message.sender exists, check the api
message.sender.send(`Command last used by ${lastHelloCommandUser}`);
}
}
此示例經過重新設計,以便將命令存儲在單個對象中並進行動態檢查。 這消除了對 switch 語句的需要。
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
const bot = new Discord.Client();
let lastHelloCommandDate, lastHelloCommandUser;
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not command ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array
const command = args[0].toLowerCase();
if (!commands[command]) {
throw new Error(`Unknown command supplied: ${command}`);
}
commands[command](message);
}}})
})
const commands = {
hello: message => {
const now = new Date();
if (now - lastHelloCommandDate > 10 * 60 * 1000) {
// It's been more than 10 mins
message.channel.send("hello");
lastHelloCommandDate = now;
lastHelloCommandUser = message.sender;
} else {
// It's been less than 10 mins
// send a direct message to the user
// i don't know if message.sender exists, check the api
message.sender.send(`Command last used by ${lastHelloCommandUser}`);
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.