discord.js 垃圾邮件命令问题
discord.js spam command troubles
bot.on('message', message => {
if (message.content === 'spam') {
message.channel.send('spam');
while (message.channel.send('spam')) {
if (message.content === 'stop spam') {
return message.channel.send('stopped');
}
}
}
});
我对 javascript 还是很陌生,所以我不确定这是否可行,我一直在尝试这样做,我浏览了 w3schools developers.mozilla,甚至还有一些已经存在的问题这里;我试过使用 do while 和 for 循环,我试过多个版本的代码
最终目标是如果用户发送单词 'spam' 机器人应该持续发送单词 'spam' 并一直这样做直到机器人关闭或用户发送单词 'stop spam'
尝试改用变量。您不能将 message.channel.send('spam')
用于 while 循环。
var spam = false;
if (message.content === 'spam') {
if (message.author.id !== bot.user.id) { // Replace bot with the instance of your bot Client.
spam = true;
} else {
if(spam) {
message.channel.send('spam');
}
}
if (message.content === 'stop spam') {
if(spam) {
message.channel.send('stopped');
}
spam = false;
}
}
关于您正在使用的代码,您应该了解以下几点:
message.channel.send
returns a Promise
,所以你不能把它放在 while
循环中,因为你需要有一些东西 true
或 false
(一个 Boolean
)。
- 现在您正在尝试检查消息的内容是否等于
'stop spam'
而您在 if-statement
中检查内容是否等于 'spam'
- 所以你永远进不了里面 if-statement
.
我建议多练习一些基本的 javascript,然后转到 Node.js,然后再回到 Discord.js - 但是,看到垃圾邮件发送者可能会很酷工作,所以我写了一些你可以使用的垃圾邮件代码 - 检查一下:
首先,创建一个名为 spamCtrl.js
的新文件,如下所示(有关发生的事情的描述,请参阅代码中的注释):
let spamming = false;
let spamChannel = undefined;
// spam function repeats until variable spamming is false
function spam() {
return new Promise((resolve, reject) => {
// add check to make sure discord channel exists
if (!spamChannel)
reject('Channel is undefined!');
// send message on spam channel
spamChannel.send('spam')
.then(msg => {
// wait 100 ms until sending next spam message
setTimeout(() => {
// continue spamming if spamming variable is true
if (spamming) {
spam()
.then(resolve) // not entirely necessary, but good practice
.catch(console.log); // log error to console in case one shows up
}
// otherwise, just resolve promise to end this looping
else {
resolve();
}
}, 100)
})
.catch(console.log);
});
}
// public functions that will be used in your index.js file
module.exports = {
// pass in discord.js channel for spam function
setChannel: function(channel) {
spamChannel = channel;
},
// set spam status (true = start spamming, false = stop spamming)
setStatus: function (statusFlag) {
// get current status
let currentStatus = spamming;
// update spamming flag
spamming = statusFlag;
// if spamming should start, and it hasn't started already, call spam()
if (statusFlag && currentStatus != statusFlag) {
spam();
}
},
// not used in my commands, but you may find this useful somewhere
getStatus: function() {
return spamming;
}
};
接下来,将该文件导入您的 index.js
文件(必须与您的 spamCtrl.js
文件位于同一目录中 - 除非您更改下面的 require
语句)。
// in index.js file, get controller for spam messages
let spamCtrl = require('./spamCtrl');
最后一步:在您的 index.js
文件中(或您处理垃圾邮件命令的任何地方)设置您的命令(可以根据需要重命名):
// 2 commands together make spamming work :)
case '?SPAM':
spamCtrl.setChannel(message.channel);
spamCtrl.setStatus(true);
break;
case '?STOP-SPAM':
spamCtrl.setStatus(false);
break;
让我知道您是否需要任何额外的解释,或者如果您想在这里看到一些调整。
bot.on('message', message => {
if (message.content === 'spam') {
message.channel.send('spam');
while (message.channel.send('spam')) {
if (message.content === 'stop spam') {
return message.channel.send('stopped');
}
}
}
});
我对 javascript 还是很陌生,所以我不确定这是否可行,我一直在尝试这样做,我浏览了 w3schools developers.mozilla,甚至还有一些已经存在的问题这里;我试过使用 do while 和 for 循环,我试过多个版本的代码
最终目标是如果用户发送单词 'spam' 机器人应该持续发送单词 'spam' 并一直这样做直到机器人关闭或用户发送单词 'stop spam'
尝试改用变量。您不能将 message.channel.send('spam')
用于 while 循环。
var spam = false;
if (message.content === 'spam') {
if (message.author.id !== bot.user.id) { // Replace bot with the instance of your bot Client.
spam = true;
} else {
if(spam) {
message.channel.send('spam');
}
}
if (message.content === 'stop spam') {
if(spam) {
message.channel.send('stopped');
}
spam = false;
}
}
关于您正在使用的代码,您应该了解以下几点:
message.channel.send
returns aPromise
,所以你不能把它放在while
循环中,因为你需要有一些东西true
或false
(一个Boolean
)。- 现在您正在尝试检查消息的内容是否等于
'stop spam'
而您在if-statement
中检查内容是否等于'spam'
- 所以你永远进不了里面if-statement
.
我建议多练习一些基本的 javascript,然后转到 Node.js,然后再回到 Discord.js - 但是,看到垃圾邮件发送者可能会很酷工作,所以我写了一些你可以使用的垃圾邮件代码 - 检查一下:
首先,创建一个名为 spamCtrl.js
的新文件,如下所示(有关发生的事情的描述,请参阅代码中的注释):
let spamming = false;
let spamChannel = undefined;
// spam function repeats until variable spamming is false
function spam() {
return new Promise((resolve, reject) => {
// add check to make sure discord channel exists
if (!spamChannel)
reject('Channel is undefined!');
// send message on spam channel
spamChannel.send('spam')
.then(msg => {
// wait 100 ms until sending next spam message
setTimeout(() => {
// continue spamming if spamming variable is true
if (spamming) {
spam()
.then(resolve) // not entirely necessary, but good practice
.catch(console.log); // log error to console in case one shows up
}
// otherwise, just resolve promise to end this looping
else {
resolve();
}
}, 100)
})
.catch(console.log);
});
}
// public functions that will be used in your index.js file
module.exports = {
// pass in discord.js channel for spam function
setChannel: function(channel) {
spamChannel = channel;
},
// set spam status (true = start spamming, false = stop spamming)
setStatus: function (statusFlag) {
// get current status
let currentStatus = spamming;
// update spamming flag
spamming = statusFlag;
// if spamming should start, and it hasn't started already, call spam()
if (statusFlag && currentStatus != statusFlag) {
spam();
}
},
// not used in my commands, but you may find this useful somewhere
getStatus: function() {
return spamming;
}
};
接下来,将该文件导入您的 index.js
文件(必须与您的 spamCtrl.js
文件位于同一目录中 - 除非您更改下面的 require
语句)。
// in index.js file, get controller for spam messages
let spamCtrl = require('./spamCtrl');
最后一步:在您的 index.js
文件中(或您处理垃圾邮件命令的任何地方)设置您的命令(可以根据需要重命名):
// 2 commands together make spamming work :)
case '?SPAM':
spamCtrl.setChannel(message.channel);
spamCtrl.setStatus(true);
break;
case '?STOP-SPAM':
spamCtrl.setStatus(false);
break;
让我知道您是否需要任何额外的解释,或者如果您想在这里看到一些调整。