This is a bot that plays an audio per command in vc (on Discord server 1). But on Discord server 2, the bot should always be in a vc, and not leaven when no one (except itself) is in the vc.
So the bot should always stay in the talk channel 1073655275141210122 on Discord server 1, even if it is alone in that talk. On server 2, on the other hand, the bot should always stay in the voice channel or VC when no one else is in it (except the bot itself).
My problem: The bot leavens on both channels as soon as there is no one in them.
My current code:
// Voice State Update Event
client.on('voiceStateUpdate', (oldState, newState) => {
const state = newState || oldState
if(state.channelId == '1073655275141210122') {
return;
}
if (oldState.channelID !== oldState.guild.me.voice.channelID || newState.channel){
return;
}
if(oldState.channel.members.size == 1 && oldState.channel.members.get(client.user.id)){
newState.guild.me.voice.disconnect();
}
});
I have already tried:
if(state.channelId == '1073655275141210122') return
&
if (oldState.channelID !== oldState.guild.me.voice.channelID || !newState.channel)
but it doesn't matter.
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().
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 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 trying to make my bot remove all messages from one specific channel if it doesn't begin with 'play!'.
I have tried (message.channel === (channel number) && !message.content.('play!')). This does not seem to work.
bot.on('message', message=>{
//delete all in channel if not beginning with play! starts here
if (message.channel === (the channel) && !message.content.('play!')) {
message.delete(50);
}
// and ends here
})
I expect it to delete all messages that do not begin with play! and are in the channel.
You should use
if (message.channel === (the channel) && !message.content.startsWith('play!')) {
return message.delete(50);
}
So !message.content.startsWith('play!') and return message.delete(50); to delete the message and to stop doing anything else with the message.
I think you could use something like this:
if (message.channel === (the channel) && message.content !== "play!") {
message.delete(50);
}
or if you want to check if the content starts with "play!":
if (message.channel === (the channel) && !message.content.startsWith("play!")) {
message.delete(50);
}