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);
}
Related
So I want that in a specific channel, where advertising is enabled, when someone pastes in a facebook, or instagram or whatever link, then the bot automatically reacts to that message with the specific logo emote, what the server has.
Is there any way to do this?
I'm thinking about something like this:
client.on('message', message => {
if (message.channel.id == channel.id.here && message.author.bot == false && message.content("facebook")) { addreactions1(message); }
});
function addreactions1(message) {
if (message.attachments.size === 1) {
message.react("facebook.logo.emote")
return;
}
Since <Message>.content returns a String, you can use the includes() method which performs a case-sensitive search of one string in another string. Eg:
client.on('message', message => {
if(message.author.bot) return; //returns doesn't respond to bot or webhook messages.
if(message.channel.id === "channelid"){
if(message.content.includes("Facebook"){ //searches for Facebook in the message.
message.channel.send("Message content had Facebook as a word in it"); // do anything here
}
}
}
I want my bot to move a user in 1 of 2 afk channels by using a "<afk [radio]" command.
But i first want to check if the user is allready in an AFK Channel. I tried:
const voiceChannel = message.member.voice.channel;
const voiceChannelID = message.member.voice.channelID;
if (!voiceChannel) {
return message.channel.send('You are not in an voice Channel so why move to AFK?');
} else if (voiceChannelID === '789186504483536937', '791444742042943548') {
return message.channel.send('You are already in an AFK channel!');
} else if (!args.length) {
return member.voice.setChannel('789186504483536937');
}
But when i try t use the "<afk" command the bot send the message:
You are already in an AFK channel!
Any ideas why it does this?
The problem is
if (voiceChannelID === '789186504483536937', '791444742042943548')
This is not how multiple conditionals work. Seperate it with the or operator.
if (voiceChannelID === '789186504483536937' || voiceChannelID === '791444742042943548')
You can think of what you were trying to do as passing in two different conditions, f(x, y). If statements in Javascript will only check for the last one (y), and "791444742042943548" is truthy since it is non-null.
I have a bot that sends a message if you use a command in the non-command channel it tells you to only use commands in the correct channel, but I want it not to affect people with the Staff role here is my code i am getting this error TypeError: Cannot read property 'roles' of null
My code:
client.on('message', message => {
if(!message.member.roles.cache.has(784236433975541771)) {
if (message.content.startsWith("-")) {
if (message.channel.id === '759066524605612108') return;
if (message.channel.id === '775035651640918067') return;
if (message.channel.id === '777287305580511262') return;
message.channel.send('You have been pinged in the <#759066524605612108> channel with the results to your command. Please only use commands there.');
}else
if (message.content.startsWith("!")) {
if (message.channel.id === '759066524605612108') return;
if (message.channel.id === '775035651640918067') return;
if (message.channel.id === '777287305580511262') return;
message.channel.send('Rank has been disabled in this channel. Please only use commands in the <#759066524605612108> channel.');
}
}
});
I am trying to do this is discord.js v12
if someone finds this and is looking for the answer i used
let allowedRole = message.guild.roles.cache.find(r => r.name === "Staff");
and that worked for me.
You can try something like this to allow members with certain roles to use your commands:
let allowedRole = message.guild.roles.find("name", "Staff");
if (message.member.roles.has(allowedRole.id) {
// allowed access to command
} else { //aka members who arent staff
// not allowed access
}
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 :(