我创建了一个Discord机器人,该机器人具有一些生成riddel和riddels答案的命令。
我发现了一个已触发的错误:如果我在两个不同的服务器中邀请bot并尝试在这两个服务器之一中运行一个命令,它将在第二个服务器中运行该命令,这实际上很糟糕,因为我想要每个服务器独立。如果该命令在一台服务器上运行,则不应在另一台服务器上运行。
我试图保存服务器ID,并试图用它编写一些条件,但是没有用。
我想使该命令在每台服务器上都有效,而不是在所有服务器上共享
let CommandRunned = CommandRunnedWithSpace.trim().toLowerCase()
let argsWithOutSpace = receivedMess.content.toLowerCase().slice(mentionNumber).trim().split(' ');
const cmd = argsWithOutSpace.shift().toLowerCase();
const args = argsWithOutSpace.join(' ');
if (CommandRunned === 'startriddle') {
if (RiddleMap.get('check') === true) {
receivedMess.reply("Riddle Already Started (can't start two riddels at the sametime)");
} else {
currentCluesArray = [];
clueI = 0;
getRndNum = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
let randomNum = getRndNum(0, RiddlesApi.Riddles.length - 1)
RiddlesApi.Riddles.filter((Riddel) => {
if (Riddel.id === randomNum) {
RiddleMessage = Riddel.Riddle
answer = Riddel.Answer
clues = Riddel.clues
}
})
StartRiddleModuel.startRiddle(receivedMess, RiddleMessage);
RiddleMap.set('check', true);
}
}
if (cmd === 'answer') {
if (answerCooldown.has(receivedMess.author.id)) {
receivedMess.delete();
receivedMess.reply('You have to wait ' + answercdseconds + ' seconds befor answering again')
} else {
if (RiddleMap.get('check') === true) {
if (args === answer) {
RiddleMap.set('check', false);
receivedMess.reply("Correct :hushed: ")
receivedMess.react('✅');
} else if (args === '') {
receivedMess.reply("Well you didnt enter anything.How you want me to know your answer then.")
receivedMess.react('?');
} else {
receivedMess.reply("That wasnt the right answer try again :smirk: ")
receivedMess.react('❌');
}
answerCooldown.add(receivedMess.author.id);
} else {
receivedMess.reply("No Riddles did started");
}
}
}
答案 0 :(得分:0)
没有完整的代码,很难指出应该更改哪些代码行。但是,通常的想法是每次运行命令时都对公会ID进行比较。在Message
对象下,您可以找到这样的公会ID:
msg.guild.id
当用户请求一个谜语时,您应该保存公会ID,然后将其与Answer命令中的ID进行比较。
但是,还有另一种解决方案可以更好地满足您的需求:awaitMessages
method from TextChannel类。
最重要的好处是每个频道使用了它,因此您无需添加任何上述逻辑。
文档中的示例应帮助您将其正确地合并到脚本中(我的评论和较小的更改):
// Here you need to create a filter to 'catch' only desired messages
// E.g. filter I use to check if my bot was pinged is as below
// message.mentions.users.some(mention => mention.id === client.user.id)
const filter = m => m.content.includes('answer');
// Here you specify max: 1 as you want to catch and respond to each message
channel.awaitMessages(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => console.log('If correct command was sent, program enters here'))
.catch(collected => console.log('In case errors or timeout, put commands here'));
唯一缺少的是循环awaitMessages
(这样它就不会在找到一条消息后结束)并设置单独的计时器,以便您知道答复时间何时结束。