I want my bot to give coins to members that spent 1 minute in any voice channel. When I'm sitting in voice channel it's working good, but if I am quitting a channel, the bot still gives me 1 coin per minute. What did I do wrong?
I tried to stop a function when the user leaves a channel, tried to clear a timeout function, but it's still working that way:
bot.on("voiceStateUpdate",(oldMember,newMember)=>{
let nuc = newMember.voiceChannel
if(nuc !== undefined){
function smth() {
setTimeout(function coin() {
db.add(`money_${newMember.id}`, 1)
setTimeout(coin, 60000);
}, 60000)}
smth()
newMember.send('You're in voicechannel')
} else {
return newMember.send('You're out of voicechannel')
}
})
Related
I have the problem that the bot the member count updates only once and does nothing else after. Does anyone know how to solve it?
Heres my current code:
bot.on("ready", () => {
const guild = bot.guilds.cache.get('779790603131158559');
setInterval(() => {
const memberCount = guild.memberCount;
const channel = guild.channels.cache.get('802083835092795442')
channel.setName(`DC︱Member: ${memberCount.toLocaleString()}`)
}, 5000);
});
If I am understanding you correctly, you want to rename a VC to the member count. The Discord API only lets you rename a channel 2 times every 10 minutes. You are trying to run that code every 5 seconds.
Try setting your timeout delay to 600000 instead of 5000.
You could try to use voiceStateUpdate, it's fired everytime a user leaves, enters, mutes mic or unmutes mic. Here's a link to it: voiceStatusUpdate
You can also use voiceChannelID if you want to get the ID of the channel. Here a link: voiceChannelID
Here's a basic idea of the code you can use:
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
} else if(newUserChannel === undefined){
// User leaves a voice channel
}
})
So basically I have a question now. And I made a logs for showing people who join/leave/move between the voice channel and now I want to expand the function of it. So I've decided to show if somebody disconnect a user from a channel. But I have a problem while fetching audit logs into this. If I disconnected somebody once and someone leave on its own after, it still shows up that I disconnected the users. So I am thinking is it possible to fix this issue by fetching a specific time of audit log but I don't know how to do this. Here is a part of code which shows the disconnect and leave part.
else if (newUserChannel === null) {
// User leaves a voice channel
const fetchedLogs = await (oldMember, newMember).guild.fetchAuditLogs({
limit: 1,
type: 'MEMBER_DISCONNECT',
});
const disconnectLog = fetchedLogs.entries.first();
// console.log(disconnectLog)
const { executor } = disconnectLog;
const Disconnected = new Discord.MessageEmbed()
.setColor('#555555')
.setAuthor(`${executor.username}#${executor.discriminator}`, executor.displayAvatarURL())
.setDescription(`<#${executor.id}> **has disconnected user** <#${oldMember.id}>`)
.setTimestamp()
.setFooter(F1, F2)
const VCLeave = new Discord.MessageEmbed()
.setColor('#55FFFF')
.setAuthor(`${oldMember.member.user.tag}`, oldMember.member.user.displayAvatarURL({ dynamic: true }))
.setDescription(`<#${oldMember.id}> **has left voice channel \`${oldUserChannel.name}\`**`)
.setTimestamp()
.setFooter(F1, F2)
LogsChannel.send(Disconnected);
LogsChannel.send(VCLeave);
}
Comparing the entry's time is a good idea to fix this issue, in my opinion.
disconnectLog has a property called createdAt (Date) and createdTimestamp (Number).
This is how you get the entry's time and compare it later to the current date.
const Diff = Math.abs((new Date().getTime() - disconnectLog.createdAt.getTime()) / 1000);
if (Diff <= 5) {console.log("The entry was created less than 5 seconds ago, so the user was disconnected by an admin.")};
I am new in this world and I am trying things.
I was trying to build a timer bot... And now I have a little problem, I would like to stop my timer whenever I want.
bot.on('message', message => {
let timer = setInterval(() => {
if (message.content.startsWith('!start'))
message.channel.send("I print something");
}, 10 * 1000)
if (message.content.startsWith('!stop')) {
message.channel.send("Interaval Cleared");
clearInterval(timer);
}})
When I type !stop in my discord channel, it displays me the message but it doesn't stop my timer... I tried with a return; but it didn't work.
Could you help me please ?
Thanks,
Have a good day !
You're starting a new interval each time you run the function. Also, your timer value is scoped to each function call, and not "shared" between calls, you need to make it available between them. The behavior you want should look something like this instead:
bot = {}
bot.timer = 0; // ensures clearInterval doesn't throw an error if bot.timer hasn't been set
bot.listen = message =>{
if(message == 'start'){
clearInterval(bot.timer); // stops the interval if it been previously started
bot.timer = setInterval(()=>console.log('I print something'),1000)
}
if(message == 'stop') clearInterval(bot.timer);
}
I have a command that creates a voice channel, and if this channel is empty, it will be deleted after 3 seconds. So if I created the channel and nobody join it everything will be fine, but if someone is in the channel longer than 3s and after leaves, then it won't be deleted. I think the problem is somewhere here (in the code below), but I don't know how to fix it, is there any possible ways how to fix it?
bot.on(`voiceStateUpdate`, oldMember => {
deleteEmptyChannelAfterDelay(oldMember.voiceChannel);
});
function deleteEmptyChannelAfterDelay(voiceChannel, delayMS = 3000){
if(!voiceChannel) return;
if(voiceChannel.members.first()) return;
if(!voiceChannel.health) voiceChannel.health = 0;
voiceChannel.health += 1;
setTimeout(function(){
if(!voiceChannel) return;
if(voiceChannel.members.first()) return;
voiceChannel.health -= 1;
if(voiceChannel.health > 0) return;
voiceChannel.delete()
.catch(error => console.log(error));
}, delayMS);
}
I have a problem I almost resolved but i'm now stuck.
I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this:
At the top of my code I have var currentdate = new Date();
And then, later in my source code:
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.
And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.
Thank you for the help and sorry if my english is bad !
You need to be checking at some interval whether or not to send a message.
Something like setInterval would work.
setInterval(function(){
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
}, MIN_INTERVAL)
You want MIN_INTERVAL to be the minimum amount of time in milliseconds to check for sending messages.
If you want to check every minute
const MIN_INTERVAL = 1000 * 60
You have to use if statement and you didn't specified hour or minutes, so the bot can't send message.
async function verifyTime() {
var d = new Date();
if (d.getHours() == d.getMinutes()) {
//your code goes here
} catch (error) {
console.log(error);
}
setTimeout(() => {
verifyTime();
}, 61 * 1000);
} else {
setTimeout(() => {
verifyTime();
}, 1000);
}
}
client.login(token);
//⬇ remember to place this under your client.login
setTimeout(() => {
verifyTime();
}, 5000);