I need example to stopping loop in discord.js - discord.js

I want to finish the loop or limit messages.
I dont know what to do to stop the loop.
please help
For example I want to do
let timer = setInterval(function(){
if(command = "loop")
message.channel.send("test")
}, sec * 1000)
if(command = "stoploop")
//i need example to stopping the loop

Use clearInterval() to stop a running interval.
let timer = setInterval(function() {
if(command === "loop")
message.channel.send("test")
}, sec * 1000)
if(command === "stoploop") {
clearInterval(timer);
}
Please keep in mind that sending a message to a channel every second is every abusive to Discord's API. Doing such behavior for too long will probably make Discord ban your bot because of API abuse.

Related

Discord Distube no_up_next error shows and then crashes

Whenever I use the skip command on the last song of the queue, it skips the song but the bot will crash showing this error in the terminal (CMD). Here is the code:
else if (cmd === "skip") {
let queue = distube.getQueue(message.guild.id);
let channel = message.member.voice.channel;
if (!channel) {
return message.channel.send(`** You need to Join Voice Channel **`)
}
if (!queue) {
return message.channel.send(`** Nothing Playing **`)
}
queue.skip();}
I found out the answer by myself. Here is to anyone who needs it
else if (cmd === "skip") {
let queue = distube.getQueue(message.guild.id);
let channel = message.member.voice.channel;
if (!channel) {
return message.channel.send(`** You need to Join Voice Channel **`)
}
if (!queue) {
return message.channel.send(`** Nothing Playing **`)
}
if (queue.autoplay || queue.songs.length > 1) distube.skip(message)
else distube.stop(message) }
Basically the code is the same but I added an if statement in the end. What it does is that the bot checks wether autoplay is enabled or the amount of songs in queue is more than one. Then it will skip if either of those are true. If it is not the bot will stop the queue and leave the voice channel if you have leaveOnStop enabled in options.

How to make my bot leave a voice channel if there is no one in the channel for longer than 5 minutes

So recently I have been making a Music bot for discord, but I would like to have my bot leave a channel if no one is in the voice channel for longer than 5 minutes. For now my bot disconnects by the stop command only
if (!queue) return message.reply("There is nothing playing.").catch(console.error);
if (!canModifyQueue(message.member)) return;
queue.songs = [];
queue.connection.dispatcher.end();
queue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error);
}
};
Can someone help me with this? I would greatly appreciate it.
You can use the voiceStateUpdate event, which emits when somebody joins or leaves a voice channel (among other things).
client.on('voiceStateUpdate', (oldState, newState) => {
// if nobody left the channel in question, return.
if (oldState.channelID !== oldState.guild.me.voice.channelID || newState.channel)
return;
// otherwise, check how many people are in the channel now
if (!oldState.channel.members.size - 1)
setTimeout(() => { // if 1 (you), wait five minutes
if (!oldState.channel.members.size - 1) // if there's still 1 member,
oldState.channel.leave(); // leave
}, 300000); // (5 min in ms)
});

Timer command not functioning

So basically I made a timer command using ms.js, and when i use the command /timer 5s, it responds with the timer has ended directly after without actually waiting the 5 seconds, any help?
case 'timer':
let time = ms(args[1])
setTimeout(() => {
message.channel.send('Your timer has now ended.')
}, ms(time));
break;
first of all check the value of time variable. if that is ok then modify your code like below. and be careful that the setTimeout use millisecond as time parameter
case 'timer':
let time = ms(args[1])
setTimeout(() => {
message.channel.send('Your timer has now ended.')
},time);
break;

Getting count of a specific reaction on a message

I'm trying to make a bot that will delete messages with a certain amount of thumbsdown reactions. I'm having trouble identifying the count of a specific reaction on a message.
Basically, I've created a command that waits for messages and adds them to my msgarray. After each message, I want to go through the array and delete any messages with the specified amount of reactions.
This is what I have so far:
var msgarray = [];
const msgs = await message.channel.awaitMessages(msg => {
msgarray.push(msg);
for (i = 0; i < msgarray.length; i++) {
// I'm not sure where to go from here, I want to make an if statement that checks
// for a certain amount of thumbsdown reactions on the message
if (msgarray[i].reactions) {
// incomplete
}
}
});
This is my first time programming in javascript, so I apologize if this code doesn't make much sense.
TextChannel.awaitMessages() resolves with a Collection, so the msg parameter you're using is not a single message, but a Collection of multiple messages.
Also, it would be better to check messages only when they get a reaction, using the messageReactionAdd event, that fires every time someone adds a reaction.
It should look like this:
// this code is executed every time they add a reaction
client.on('messageReactionAdd', (reaction, user) => {
let limit = 10; // number of thumbsdown reactions you need
if (reaction.emoji.name == '👎' && reaction.count >= limit) reaction.message.delete();
});

Deleting my messages in a server using bulkDelete (discord.js)

I am new to discord.js but learned that I can delete my messages using bulkDelete and it will delete them all, even if they are older than 2 weeks. I clear my messages in a server I moderate manually once a month and needless to say it takes forever. I was wondering if anyone would be able to help me make a command that will do this automatically whenever I call it?
Thanks,
K
I would set up a recursive function that checks if there are messages in the channel (100 max every time): if there are no messages it stops otherwise it deletes them and restarts.
function clean(channel, limit = 100) {
return channel.fetchMessages({limit}).then(async collected => {
let mine = collected.filter(m => m.author.id == 'your_id_here'); // this gets only your messages
if (mine.size > 0) {
await channel.bulkDelete(mine, true);
clean(channel);
} else channel.send("The channel is now empty!").delete(5000); // this message is deleted after 5 s
});
}
You can adapt this idea to your existing command parser or, if you don't know how to implement this, try:
client.on('message', msg => {
if (msg.author.bot || msg.author != YOU) return;
// with YOU i mean your User object, to check permissions
let command = 'clean', // the name of your command
args = msg.content.split(' ');
if (args[0].toLowerCase() == command)
clean(msg.channel, !isNaN(args[1]) ? args[1] : undefined); //<-- THIS is how to use the function
// used a ternary operator to check if the other arg is a number
});
This is just a very basic implementation, there are a lot of better ways to detect commands.
I've just found a way to filter messages.
You can fetch messages and then check for each message if its yours
await message.channel.fetchMessages({
limit: 100
}).then((msgCollection) => {
msgCollection.forEach((msg) => {
if(msg.author.id == message.author.id) {
msg.delete();
}
})
});

Resources