该机器人会将数据保存到JSON文件中,但我不知道如何回调该数据,因此该机器人会记住将消息发布到的正确通道。
bot.on("message" , (message) =>{
if (message.content.startsWith ("!config enable")) {
editedmessage = message.content.slice (15);
bot.msgs = {
channelName: editedmessage
}
fs.writeFile ("./storage/clanConfig.json", JSON.stringify (bot.msgs, null, 4), (err) => {
if (err) throw err;})
message.channel.send ("channel saved");
}
let channel = (channelName);
let counter = 0;
if(++counter === 10){
bot.channels.cache.get(channelName).send(`10 messages were sent`)
counter = 0;
}
});
答案 0 :(得分:0)
我会进行这些更改。
counter
移动到事件处理程序之外。message.content.split(/\s+/)[2]
从消息中获取频道名称。"!config enable bla bla"
)宽容,并且只接受“启用”之后的第一个单词。writeFileSync
将配置写入磁盘。.send()
可能会在配置文件完成写入之前发生)。bot.msgs.channelName
,而不是维护单独的channelName
变量。代码:
bot.msgs = {
channelName: "last channel from config file, otherwise default value"
};
var counter = 0;
bot.on("message", message => {
if (message.content.startsWith("!config enable")) {
let newChannel = message.content.split(/\s+/)[2];
if (isValid(newChannel)) {
bot.msgs.channelName = newChannel;
fs.writeFileSync("./storage/clanConfig.json", JSON.stringify(bot.msgs, null, 4)
message.channel.send("channel saved");
counter = 0;
}
}
if (++counter === 10) {
bot.channels.cache.get(bot.msgs.channelName).send(`10 messages were sent to {bot.msgs.channelName}.`)
counter = 0;
}
});