So I've just recently decided to try creating a Discord bot. I am only a beginner at the moment so I've been having some difficulty with creating one command for my discord bot. I want to create a command that can kill any previously running command, such as a //say or a //spam (This is a fun orientated bot that I'm playing around with, so it allows some "spammy" exploits such as the bot constantly replying to itself (ie. //say //say //say //say etc. causes the bot to run the //say command until all the //says have been executed.)
Because of this, I want there to be a way to kill the command if it gets out of hand with another command. I've tried a bunch of stuff, but nothing seems to work.
Here is what I tried first: (note: task is defined, so that is not the issue, it is just not visible in this codeblock)
//stuff up to this point works fine
if (msg.content.startsWith(`${prefix}say`)) {
task = 1;
var text = msg.content.split(' ').slice(1).join(' ');
if(!text) return msg.reply('hello?');
if (task === 1) {
msg.channel.send(text);
} else if (task === 0) {
return;
}
if (msg.content.startsWith(`${prefix}cease`)) {
task = 0;
msg.channel.send('Task terminated successfully.');
}
});
However, the bot completely ignores this and continues running the //say command even if the user has attempted to execute a //cease command.
I decided to try and play around with while loops after that, like this:
//stuff up to this point work fine
if (msg.content.startsWith(`${prefix}say`)) {
task = 1;
var text = msg.content.split(' ').slice(1).join(' ');
if(!text) return msg.reply('hello?');
while (task === 1) {
msg.channel.send(text);
}
}
if (msg.content.startsWith(`${prefix}cease`)) {
task = 0;
msg.channel.send('Task terminated successfully.');
}
});
However, this just makes the command prompt completely freak out with random stuff that I don't have the slightest clue about what it means. All I can understand is that the process ran out of memory...I think.
Here's what happens.
<--- Last few GCs --->
[18668:000002D05FC41820] 31983 ms: Mark-sweep 1397.8 (1425.2) -> 1397.3 (1424.7) MB, 892.1 / 0.0 ms (average mu = 0.089, current mu = 0.015) allocation failure scavenge might not succeed
[18668:000002D05FC41820] 31990 ms: Scavenge 1398.1 (1424.7) -> 1397.6 (1425.2) MB, 5.5 / 0.0 ms (average mu = 0.089, current mu = 0.015) allocation failure
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 000003DFF175C5C1]
1: StubFrame [pc: 000003DFF175D9BF]
Security context: 0x00069561e6e9 <JSObject>
2: Channel [00000127FFD14059] [C:\Users\[removed]\Documents\epic-bot\node_modules\discord.js\src\util\Constants.js:~165] [pc=000003DFF1A16AA6](this=0x0127ffd12799 <Object map = 0000023F67AEB559>,channelID=0x0199da820629 <Channel map = 000001B00E5314E1>)
3: /* anonymous */(aka /* anonymous */) [000000DACB142C...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 00007FF6F1A9F04A v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+5114
2: 00007FF6F1A7A0C6 node::MakeCallback+4518
3: 00007FF6F1A7AA30 node_module_register+2032
4: 00007FF6F1D020EE v8::internal::FatalProcessOutOfMemory+846
5: 00007FF6F1D0201F v8::internal::FatalProcessOutOfMemory+639
6: 00007FF6F2222BC4 v8::internal::Heap::MaxHeapGrowingFactor+9556
7: 00007FF6F2219C46 v8::internal::ScavengeJob::operator=+24310
8: 00007FF6F221829C v8::internal::ScavengeJob::operator=+17740
9: 00007FF6F2220F87 v8::internal::Heap::MaxHeapGrowingFactor+2327
10: 00007FF6F2221006 v8::internal::Heap::MaxHeapGrowingFactor+2454
11: 00007FF6F1DDCDB7 v8::internal::Factory::NewFillerObject+55
12: 00007FF6F1E72CC6 v8::internal::WasmJs::Install+29414
13: 000003DFF175C5C1
The answer is probably super basic but as I said, I'm just starting out.
Any help would be appreciated.
Thanks!