是否可以制作一个专用于某个作者的变量Discord.js?
Is it possible to make a variable dedicated to a certain author Discord.js?
我正在制作一个商店机器人,我 运行 遇到了一些 SetInterval 错误。我想让每个变量都有用户的 id,所以当我 运行 一个不同的命令时,它会知道停止哪个间隔。 (如果这有意义的话)。
这是我的代码:
if(message.content.startsWith(`!open`) {
var cashier1[message.author.id] = function () {
BalanceJSON[message.author.id].bal += 10
Fs.writeFileSync(`./DB/balance.json`, JSON.stringify(BalanceJSON));
}
setInterval(cashier1[message.author.id], 5000);
}
所有这些代码都在 bot.on('message', message => { })
我想用 clearInterval(cashier1[message.author.id])
停止某个玩家的间歇
函数setInterval
returns a unique id which can be used to clear the interval again (See the example for more information).
您的问题的解决方案是将间隔的唯一 ID 存储在某个对象或数据库中,并使用它再次清除间隔。请参阅下面的示例代码:
// Create an object to store the intervals.
const cashierIntervals = {};
// Inside your message handler.
// Some dummy if statement for demonstration purpose.
if (message.content === 'setInterval') {
// Create the setInterval and store the unique id in the cashier intervals object.
// You should probably add more checks to see if there is already an interval stored for this author id etc.
cashierIntervals[message.author.id] = setInterval(() => {
BalanceJSON[message.author.id].bal += 10;
Fs.writeFileSync(`./DB/balance.json`, JSON.stringify(BalanceJSON));
}, 5000);
} else if (message.content === 'clearInterval') {
// Clear the interval based on the author id.
// Again, probably add more checks to see if the author id has an interval stored etc.
clearInterval(cashierIntervals[message.author.id]);
// Delete the stored interval entry from the global intervals object.
// Not necessary but it keeps the intervals object small.
delete cashierIntervals[message.author.id];
}
创建一个以 id 作为键的对象。你的值将是你想要间隔的函数
您的主文件:
const cashier1 = {
// Template for your key:values
'999999999': yourRepeatingFunction(),
}
// Lets say message.author.id returns '999999999'
// Doing setInterval(cashier1[message.author.id], 5000) Will call yourRepeatingFunction()
我正在制作一个商店机器人,我 运行 遇到了一些 SetInterval 错误。我想让每个变量都有用户的 id,所以当我 运行 一个不同的命令时,它会知道停止哪个间隔。 (如果这有意义的话)。 这是我的代码:
if(message.content.startsWith(`!open`) {
var cashier1[message.author.id] = function () {
BalanceJSON[message.author.id].bal += 10
Fs.writeFileSync(`./DB/balance.json`, JSON.stringify(BalanceJSON));
}
setInterval(cashier1[message.author.id], 5000);
}
所有这些代码都在 bot.on('message', message => { })
我想用 clearInterval(cashier1[message.author.id])
函数setInterval
returns a unique id which can be used to clear the interval again (See the example for more information).
您的问题的解决方案是将间隔的唯一 ID 存储在某个对象或数据库中,并使用它再次清除间隔。请参阅下面的示例代码:
// Create an object to store the intervals.
const cashierIntervals = {};
// Inside your message handler.
// Some dummy if statement for demonstration purpose.
if (message.content === 'setInterval') {
// Create the setInterval and store the unique id in the cashier intervals object.
// You should probably add more checks to see if there is already an interval stored for this author id etc.
cashierIntervals[message.author.id] = setInterval(() => {
BalanceJSON[message.author.id].bal += 10;
Fs.writeFileSync(`./DB/balance.json`, JSON.stringify(BalanceJSON));
}, 5000);
} else if (message.content === 'clearInterval') {
// Clear the interval based on the author id.
// Again, probably add more checks to see if the author id has an interval stored etc.
clearInterval(cashierIntervals[message.author.id]);
// Delete the stored interval entry from the global intervals object.
// Not necessary but it keeps the intervals object small.
delete cashierIntervals[message.author.id];
}
创建一个以 id 作为键的对象。你的值将是你想要间隔的函数
您的主文件:
const cashier1 = {
// Template for your key:values
'999999999': yourRepeatingFunction(),
}
// Lets say message.author.id returns '999999999'
// Doing setInterval(cashier1[message.author.id], 5000) Will call yourRepeatingFunction()