So, basically I have some code setup and I don't see the issue with the code. But basically what I want it to do is if the previous voice channel of the member that switched channels was "DO NOT DISTURB" it will move them back to the "DO NOT DISTURB" voice channel. What have I done wrong? I get 0 errors in console.
client.on('voiceStateUpdate', async (oldState, newState) => {
let newUserChannel = newState.channel;
let oldUserChannel = oldState.channel;
if(oldUserChannel.id === "894024223088050176") {
var dndChannel = oldState.guild.channels.cache.find(ch => ch.type === "voice" && ch.name === "DO NOT DISTURB")
newState.member.voice.setChannel(dndChannel)
}
});
Double check if your channelid in the if condition is right and use
Guild.channels.fetch(channelId)
insted of Guild.channels.cache.find().
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
}
})
I am trying to log presence for my bot called Timer Bot. I want it to alert everyone when he goes online and when he goes offline. Here is the script I'm using -
client.on('presenceUpdate', (oldPresence, newPresence) => {
let member = newPresence.member;
// User id of the user you're tracking status.
if (member.id === '603517534720753686') {
if (oldPresence.status !== newPresence.status) {
// Your specific channel to send a message in.
let channel = member.guild.channels.cache.get('788547135234375712');
// You can also use member.guild.channels.resolve('<channelId>');
let text = "";
if (newPresence.status === "online") {
text = "**Hello #everyone, Timer Bot is now online! Thank you for your patience.**";
} else if (newPresence.status === "offline") {
text = "**#everyone Due to issues, Timer Bot is currently offline. We apologize for the inconvenience.**";
}
// etc...
channel.send(text);
}
}
});
For some reason, it doesn't work. Anyone know why?
Thanks,
Brian.#1111
You need to enable presence intent and then pass it in to the client when logging in.
new Discord.Client({ws:{intents: [Intents.FLAGS.GUILD_PRESENCES]}});
Source: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
https://discord.com/developers/applications/
I am working on a discord bot that would need to detect when a user joins & leaves a channel. I have tried this on line 4 because if the user's connection doesn't change, it won't run.
But this does not work. Here is my entire section of code.
client.on('voiceStateUpdate', (oldState, newState) => {
// check for bot
if (oldState.member.user.bot) return;
if (oldState.member.voice === newState.member.voice) return;//<- here
client.channels.cache.get('777782275004825640').send(`${oldState.member} joined`);
});
I've also tried .connection but it doesn't work with my current setup. Any help would be great!
I found it!
After some testing, I found out that when a channel was updated one of four things would happen.
if the user joined the oldState.channelID returned null
if the user left the newState.channelID returned null
if the user muted themselves or were deafened the oldState.channelID was equal to newState.channelID
if the user moved channels oldState.channelID did not equal newState.channelID
my Discord.Client() is set as client, so if yours is different change that
client.on('voiceStateUpdate', (oldState, newState) => {
if(oldState.channelID === newState.channelID) {
console.log('a user has not moved!')
}
if(oldState.channelID != null && newState.channelID != null && newState.channelID != oldState.channelID) {
console.log('a user switched channels')
}
if(oldState.channelID === null) {
console.log('a user joined!')
}
if (newState.channelID === null) {
console.log('a user left!')
}
});
Try this snippet and please let me know how it worked for you.
let oldChannel = oldState.voiceChannel;
let newChannel = newState.voiceChannel;
if(oldChannel === undefined && newChannel !== undefined) {
// User has joined a channel
} else if(newChannel === undefined) {
// User has left a channel
}
I am making a Discord.js Bot on v12 that includes a mute command, that mutes the whole voice channel you are in. The problem is when somebody leaves the channel they stay muted. I am trying to fix that with a simple event to unmute the person, but I don't understand the VoiceStateUpdate and the OldState and NewState. I've searched widely, but I can only find one for joining a vc, not leaving. Here is what I got so far:
Mute command:
else if (command === 'mute') {
message.delete()
if (!message.member.roles.cache.has('') && !message.member.roles.cache.has('')) {
message.reply('You don\'t have permission to use this commmand!')
.then(message => {
message.delete({ timeout: 5000 })
}).catch();
return;
}
if (message.member.voice.channel) {
let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
for (const [memberID, member] of channel.members) {
member.voice.setMute(true);
}
} else {
message.reply('You need to join a voice channel first!')
.then(message => {
message.delete({ timeout: 5000 })
}).catch();
}
}
Unmute event:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.member.user.bot) return;
if (oldState.member.user !== newState.member.user) member.voice.setMute(false);
});
Thanks for taking your time to help me! :)
Well, you are already on the right track. What you should do is check if someone is muted when they leave a voice channel and then remove that mute.
So lets get to that.
First we check if the person is leaving the voice channel. Thats important because the voiceStateUpdate event is triggered every time some does anything to their voice, i.e. mute or joining. We do that by checking if oldState.channelID is either null or undefined as that indicates a joining.
if (oldState.channelID === null || typeof oldState.channelID == 'undefined') return;
Next we need to check if the person leaving is muted and return if not.
if (!oldState.member.voice.mute) return;
And lastly we remove the mute.
oldState.member.voice.setMute(false);
So your entire voiceStateUpdate should look a little something like this.
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelID === null || typeof oldState.channelID == 'undefined') return;
if (!oldState.member.voice.mute) return;
oldState.member.voice.setMute(false);
});
You can't manipulate the voice state of a person that isn't on a voice channel, it would cause a DiscordAPIError: Target user is not connected to voice., and even if you could, you probably would still not want to, because even if you don't get another unhandled promise error of some sort, you would probably still get an infinite loop by checking if the New State's channel is null, because it would fire another event with null channel as soon as you unmute the person and so on.
So, the way I see it right now, a possible solution to your problem, is to unmute whenever the user joins a channel. It might not be necessary to be this way if someone can figure out a better way to check if it is a channel leaving that is happening other than just using newState.channel === null. Whatever, if it's okay for you to unmute on join, you could do it this way:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channel === null && newState.channel !== null) {
newState.setMute(false);
}
});
Notice though that this could bug if someone joins the channel exactly after leaving, otherwise, you should have no problems.
according to this link (and I havent tested this) you need something like this:
const Discord = require("discord.js")
const bot = new Discord.Client()
bot.login('*token*')
bot.on('voiceStateUpdate', (oldState, newState) => {
let newUserChannel = newState.voiceChannel
let oldUserChannel = oldState.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
} else if(newUserChannel === undefined){
// User leaves a voice channel
}
})
client.on('voiceStateUpdate', (oldMember, newMember) => {
const oldUserChannel = oldMember.voice.channelID
const newUserChannel = newMember.voice.channelID
if (oldUserChannel === 'MUTED CHANNEL ID' && newUserChannel !== 'MUTED CHANNEL ID') {
newMember.voice.setMute(false);
}
});
You can write in a json file the id of the muted channel and take it from there, I can't think of another way, sorry :(
I'm programming a discord bot and I'm having a problem in one of the main functionalities - the welcome message.
I've tried several methods but none of them worked, except for one: the one that needs a channel ID.
I dont want to use that method because I want to use my bot on multiple servers, and that would mean that I have to change the code for each one of them - and I really dont want to.
client.on('guildMemberAdd', function(message) {
member.guild.channels.cache.get('MY CHANNEL ID').send('welcome dadadadada');
});
This code worked (actually, I just tried it and it doesn't - but it did. It was something like that). Now I'm trying more 'advanced' coding to tell the bot to automatically get the channel ID from #general (for this I did npm install long)
const getDefaultChannel = (guild) => {
if(guild.channel.has(guild.id))
return guild.channels.get(guild.id)
const generalChannel = guild.channels.find(channel => channel.name === 'general');
if (generalChannel)
return generalChannel;
return guild.channels
.filter(c => c.type === 'text' &&
c.permissionsFor(guild.client.user).has('SEND_MESSAGES'))
.sort((a, b) => a.position - b.position) ||
Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber()
.first();
}
client.on("guildMemberAdd", member => {
const channel = getDefaultChannel(member.guild);
channel.send(`Welcome ${member} to the server, wooh!`);
});
I didn't find anything useful and I don't know what to do. Also, english is not my first language.
What else could I try? Thank you.
I think this could help you!
The guildMemberAdd listener can send messages
client.on('guildMemberAdd', function(member, message) => {
let generalChannelName = message.guild.channels.find(channel => channel.name === 'general');
let generalChannel = generalChannel.id;
generalChannel.send(`Welcome ${member.tag} to the server!`);
});
It's hard to insert your filtering here, may this help you?