Javascript 使命令重复Discord.js
我有一个discord.js代码,当我运行“!cat”时,它会从r/cats发送一个随机图像,代码如下:Javascript 使命令重复Discord.js,javascript,discord.js,Javascript,Discord.js,我有一个discord.js代码,当我运行“!cat”时,它会从r/cats发送一个随机图像,代码如下: var Discord = require('discord.js'); var bot = new Discord.Client() randomPuppy = require('random-puppy') bot.on('ready', function() { console.log(bot.user.username); }); bot.on('message', a
var Discord = require('discord.js');
var bot = new Discord.Client()
randomPuppy = require('random-puppy')
bot.on('ready', function() {
console.log(bot.user.username);
});
bot.on('message', async message => {
if (message.content === "!cat") {
const img = await randomPuppy('cats')
message.channel.send(img);
}
});
我希望它每20分钟发送一次,而不是用户使用命令时发送一次。我已经找到了一些方法,但它们不能同步工作。提前谢谢 可以尝试使用setInterval(),如下所示: 它每1200000毫秒或20分钟执行一次内部操作。我会把它放在
客户机.once(“ready”)
位。请注意,您将无法访问该消息,只需将该消息发送到特定的频道即可
下面是完整的工作实现
const Discord = require("discord.js");
const client = new Discord.Client();
randomPuppy = require("random-puppy");
//Place your channelID here. You should be able to find it by:
//Enabling Developer mode in your discord app(User Settings > Advanced> Developer mode)
//Right click the channel you want the messages to send to and tap "copy id"
channelID = "channelID";
prefix = "!";
//This fires as soon as your bot is "ready"
client.once('ready', ()=>{
console.log("Bot is Active!");
//Here's the meat of it. setInterval repeats the function every "interval" milliseconds.
//Note that it doesn't fire the first time
//Set the interval to something smaller, like 10000, to see if it works
setInterval(async function(){
//The code you want to run
const img = await randomPuppy('cats');
client.channels.cache.get(channelID).send(img);
}, 1200000);
//Further reassurance
console.log("This should print");
});
//Added this bit for further context, nothing useful in here
client.on("message", async message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === "test"){
message.channel.send("Despite your best efforts, I am still alive...");
}
});
client.login('BOT_TOKEN_HERE');
你能不能只使用
setInterval()
?很抱歉,我对这一点很陌生,你能展示更多的代码和放在哪里吗。这对我不起作用,并给我一个错误SyntaxError:await仅在异步函数客户端中有效。一旦('ready',异步消息=>{setInterval(function(){const img=await randomPuppy('cats')message.channel.send(img);},10000})
您的setInterval()
函数不是异步的setInterval(async function(){…},10000)
好的,我添加的代码至少在我这方面是有效的。还添加了async关键字以使内部异步。我在第一次安排任务时也遇到了很多麻烦,但在这方面还是有点初学者。非常感谢你的帮助!我非常感激你的工作!我认为你的答案也正确
const Discord = require("discord.js");
const client = new Discord.Client();
randomPuppy = require("random-puppy");
//Place your channelID here. You should be able to find it by:
//Enabling Developer mode in your discord app(User Settings > Advanced> Developer mode)
//Right click the channel you want the messages to send to and tap "copy id"
channelID = "channelID";
prefix = "!";
//This fires as soon as your bot is "ready"
client.once('ready', ()=>{
console.log("Bot is Active!");
//Here's the meat of it. setInterval repeats the function every "interval" milliseconds.
//Note that it doesn't fire the first time
//Set the interval to something smaller, like 10000, to see if it works
setInterval(async function(){
//The code you want to run
const img = await randomPuppy('cats');
client.channels.cache.get(channelID).send(img);
}, 1200000);
//Further reassurance
console.log("This should print");
});
//Added this bit for further context, nothing useful in here
client.on("message", async message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === "test"){
message.channel.send("Despite your best efforts, I am still alive...");
}
});
client.login('BOT_TOKEN_HERE');