我正在尝试实现一些代码,这些代码将在不一致时开始倒计时。我目前已将其运行到控制台中,但无法正常工作。我设法使它在一个单独的文件中工作,并且已经复制了所有正确的位,但是它不起作用。代码在第59行。如果有帮助,代码为here。
我尝试在Google上四处搜索,并找到了我正在使用的相同代码(我很确定)。所以我不确定为什么它不起作用。
我正在制作一个Night Night Ultimate Werewolf机器人。一个夜晚的终极狼人是一个隐藏的角色游戏,当有人狼人时,必须避免被杀死。最后,您有2分钟半的时间告诉玩家您的角色,并尝试诱使他们杀死/不杀死您(取决于您的角色)。
我试图做的是使这2分半钟倒计时,所以我希望它运行某些代码,等待3秒钟,然后再执行其他代码。稍后我会将这段代码更改为2:30计时器,因此,如果您能告诉我该怎么做,那会更好。但是,至少,我想知道为什么下面的代码不起作用,以及如何解决它。
代码段是
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'countdown') {
console.log('Welcome to My Console,');
setTimeout(function() {
console.log('Blah blah blah blah extra-blah');
}, 3000);
}
})
完整文件为
//required libraries
const Discord = require('discord.js');
const client = new Discord.Client();
//What to do when the bot starts up.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity("with my ding-a-ling")
});
//prefix
var prefix = ("~")
//Find the latancy and API latency.
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'ping') {
message.channel.send(":ping_pong: Pinging...").then((ping) => { ping.edit(`:ping_pong: Pong!\nLatency is ${Math.floor(ping.createdAt - message.createdAt)}\nAPI latency is ${Math.round(client.ping)}ms`); });
}
}
);
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'newgame') {
if (message.member.roles.has("641725420957335562")) {
client.channels.get('642070729805791232').send("Sending out message....").then((newgame) => { newgame.edit(`Hey, <@&641727807663374345>! A new game is starting. Do ~ready to apply`) })
}
client.on('message', message => {
if (message.author.bot) return;
if (message.content !== prefix + 'ready') {
return;
}
if (message.content === prefix + 'ready') {
message.channel.send(`<@${message.author.id}> is ready!`)
var roles = Array("Doppelganger", "Werewolf", "Minion", "Mason", "Seer", "Robber", "Troublemaker", "Drunk", "Insomniac", "Villager", "Tanner", "Hunter")
var role = roles[Math.floor(Math.random() * roles.length)];
}
message.author.send("Your role is..." + role + "!")
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'accept') {
if (message.channel.type === 'dm') {
client.channels.get('642090245122293771').send(`<@${message.author.id}> has accepted their role!`)
}
}
});
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'countdown') {
console.log('Welcome to My Console,');
setTimeout(function() {
console.log('Blah blah blah blah extra-blah');
}, 3000);
}
})
});
}
});
//logs messages into console
client.on("message", async message => {
console.log(`${message.author.username} said: ${message.content}`);
});
//Test command if we need it
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'test') {
message.channel.send("test reply")
}
}
);
client.login(process.env.TOKEN);
我没有得到任何错误,代码只是行不通。
任何帮助将不胜感激。 谢谢。
答案 0 :(得分:0)
首先,您的代码未正确嵌套。您只应该有一个client.on('message'):
//required libraries
const Discord = require('discord.js');
const client = new Discord.Client();
var sleep = require('sleep');
//What to do when the bot starts up.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity("with my ding-a-ling")
});
//prefix
var prefix = ("~")
//Find the latancy and API latency.
client.on('message', message => {
if (message.author.bot) return;
if (message.content === prefix + 'ping') {
message.channel.send(":ping_pong: Pinging...").then((ping) => { ping.edit(`:ping_pong: Pong!\nLatency is ${Math.floor(ping.createdAt - message.createdAt)}\nAPI latency is ${Math.round(client.ping)}ms`); });
}
if (message.content === prefix + 'newgame') {
if (message.member.roles.has("641725420957335562")) {
client.channels.get('642070729805791232').send("Sending out message....").then((newgame) => { newgame.edit(`Hey, <@&641727807663374345>! A new game is starting. Do ~ready to apply`) })
}
if (message.content === prefix + 'ready') {
message.channel.send(`<@${message.author.id}> is ready!`)
var roles = Array("Doppelganger", "Werewolf", "Minion", "Mason", "Seer", "Robber", "Troublemaker", "Drunk", "Insomniac", "Villager", "Tanner", "Hunter")
var role = roles[Math.floor(Math.random() * roles.length)];
message.author.send("Your role is..." + role + "!");
}
if (message.content === prefix + 'accept') {
if (message.channel.type === 'dm') {
client.channels.get('642090245122293771').send(`<@${message.author.id}> has accepted their role!`)
}
}
if (message.content === prefix + 'countdown') {
console.log('Welcome to My Console,');
setTimeout(function() {
console.log('Blah blah blah blah extra-blah');
}, 3000);
}
if (message.content === prefix + 'test') {
message.channel.send("test reply")
}
});
//logs messages into console
client.on("message", async message => {
console.log(`${message.author.username} said: ${message.content}`);
});
client.login(process.env.TOKEN);
这将具有相同的行为,但是您将具有更好的性能,并且代码更具可读性。
以下是计数器的示例:
if (message.content === prefix + 'countdown') {
console.log('Countdown started');
let total = Date.now() + (60000*2+60000/2); // 2 minutes later
let interval = setInterval(function() {
console.log((total-Date.now()/1000)+' seconds remaining');
}, 3000);
setTimeout(function(){
clearInterval(interval);
}, (60000*2+60000/2));
console.log("Timer ended");
}
每3秒将显示“剩余x秒”,直到计时器结束。