我目前正在尝试制作一个不和谐的机器人,该机器人将倒计时直到特定日期。现在,我将参数传递到.json文件的文件夹中,其中仅存储事件名称和发布日期/事件日期。
bot.on('ready', () => {
var sendChannel = bot.channels.find(channel => channel.id === '553846848578912257')
setInterval(() =>{
fs.readdir(folder, (err, files) => {
files.forEach(file => {
});
for(i = 0; i < files.length; i++){
var input = require(folder+files[i]);
if(((new Date(input.releasedate) - currentdate) / days) > 0)
{
var embed = new Discord.RichEmbed()
.addField(input.eventName, Math.ceil((new Date(input.releasedate) - currentdate) / days) + ' days until ')
//sendChannel.send(Math.ceil((new Date(input.releasedate) - currentdate) / days) + ' days until ' + input.eventName);
}
else
{
console.log("arg not passed, invalid or past: "+input.eventName)
}
}
sendChannel.send(embed);
});
}, 10000);
console.log("Ready")
console.log(`Logged in as ${bot.user.tag} (${bot.user.id}) on ${bot.guilds.size} servers`);
bot.user.setActivity(`Counting things down | ${bot.guilds.size} servers`);
});
现在,它可以与sendChannel.send命令一起使用,并且可以发送文件夹中的许多文件,但一次只能发送一个。我正在尝试将它们全部嵌入消息中。问题是.addField将仅将最后一个元素存储在“输入”中。有没有办法让我的程序在每次通过之后附加.addField而不是改写?
答案 0 :(得分:0)
addField
存储最后一个输入,因为您在每个循环中创建了一个新的Discord.RichEmbed()
,从而创建了一个新实例并重写了前一个实例。
您应该在循环之前初始化嵌入消息,然后在每个循环中添加如下字段:
bot.on('ready', () => {
var sendChannel = bot.channels.find(channel => channel.id === '553846848578912257')
setInterval(() =>{
fs.readdir(folder, (err, files) => {
// initialize the embed message here
var embed = new Discord.RichEmbed();
files.forEach(file => {
});
for(i = 0; i < files.length; i++){
var input = require(folder+files[i]);
if(((new Date(input.releasedate) - currentdate) / days) > 0)
{
embed.addField(input.eventName, Math.ceil((new Date(input.releasedate) - currentdate) / days) + ' days until ');
}
else
{
console.log("arg not passed, invalid or past: "+input.eventName)
}
}
sendChannel.send(embed);
});
}, 10000);
console.log("Ready")
console.log(`Logged in as ${bot.user.tag} (${bot.user.id}) on ${bot.guilds.size} servers`);
bot.user.setActivity(`Counting things down | ${bot.guilds.size} servers`);
});