Server Mute Music Bot? .setDeaf is not a function? - javascript

Well so I am trying to server mute my bot when it joins a VC but all I get is
TypeError: bot.setDeaf is not a function
I've tried the same with
let bot = message.guild.members.cache.get('ID');
But that didn't work either.
What am I doing wrong? I couldn't really find anything anywhere that would help me.
Here's the rest. Thanks in advance!
let bot = client.users.cache.get('ID');
bot.setDeaf(true);

Note 1. You can only call .setDeaf() from a VoiceState. Even though you can only have one VoiceState per user, the VoiceState is scoped to the instantiating GuildMember instance.
Note 2. You can access the VoiceState property via GuildMember.voice, and then call the relevant methods.
Note 3. Contrary to what others suggested, there is a fine difference between .setDeaf() and .setSelfDeaf(). Firstly, .setDeaf() server-deafens the member of that voice state. That means there will be a nice red icon next to that member's voice presence. . An entry to the audit log will also pop up, and you can specify the reason in .setDeaf().
On the other hand, .setSelfDeaf() can only be called on the bot's VoiceState. The library internally checks if the VoiceState user ID matches the client user ID. (see https://github.com/discordjs/discord.js/blob/master/src/structures/VoiceState.js#L192)
Self-deafening will not generate an audit log entry, and the icon next to the bot will be gray, not red.
So your code would be as follows:
//exit if the voice state property does not exist
if(!message.guild.me.voice) return message.channel.send("I'm not in a voice channel...");
//deafen
message.guild.me.voice.setDeaf(true, "Yeah...this is a deafening test.").then(() => {
message.channel.send("I have successfully server deafened myself.");
});
You can view the documentation for .setDeaf() here

Related

Disconnect users from a voice channel for being defeaned for 10 minutes

So my problem is I want to create a bot that disconnects people from a VC on Discord server if they are being defeaned for 10 minutes.
I know a thing or two about programming because I learnt it in school, but I never used python.
I have a working music bot, with index.js and everything. So I was wondering if you could help me write a code for that I can paste into it so it'll work?
Thanks in advance!
I found this code:
client.on('voiceStateUpdate', (oldState, newState) => {
if (newState.deafened && newState.member.manageable) {
newState.kick();
}
});
But it didn't work for some reason, and it wouldn't even let me set user afk time.
You don't have to fix this, you can write a new code instead for the whole concept :)
First of all, VoiceState doesn't have a property called 'deafened'. You can use VoiceState.selfDeaf instead, which returns a boolean.
Second of all, VoiceState.kick() wont do anything, as it's not a method of VoiceState. Assuming you want to kick the member from the vc, you can use VoiceState.disconnect().
You can read the VoiceState documentation here

List all users with a specific role with DiscordJS

I already checked here but this unfortunately doesn't seem to help in my case. I am trying to list all members in a server that have the role with id 'roleID'. I get that role like so:
const guild = await client.guilds.fetch(guildID);
const role = await guild.roles.fetch(roleID);
However, whenever I try to print the roles.member property, I get an empty collection, and trying to map it out returns an empty array
console.log(role.members);
>>Collection(0) [Map] {}
console.log(role.members.map(users => users.user.tag));
>>[]
And I am absolutely stumped as to why. I know the ID is definitely correct since printing out the role lists all the correct information, and I used roleID to successfully assign the role to a few users in the server.
And yes, I triple checked, there are definitely users in the server that have the role
Weirdly enough what seems to fix this issue on my side is not adding the GuildMembers intent, but adding the GuildPresences intent.
I don't know why that happens since this intent is unrelated, but you could try it as a solution to your problem.
You also need to make sure that you enabled this intent at https://discord.com/developers/applications -> Bot -> Enable Presence Intent
Image for reference: https://i.imgur.com/oepA04b.png

Remove a specific user reaction from a specific message - Discord.js

I hope you can help. This has been driving me crazy. I am, new to programming and JS, but I am making a discord bot as a hobby project. Please forgive me if I don't use the correct terminology but I think my question should actually be quite simple.
I have a bot that builds a message with an embed. The bot listens with a collector and adds players to fields in the embed depending on which reaction they react so. See screenshot for example. Players can add themselves to "Farming" "Not Farming" or "Starter"
Screenshot of bot's post
When I post the initial embed, I am clearing all the pinned messages in the channel and pining this message, so the post with my status embed will always be the only pinned post in the channel.
What I would like to do is type a command like "!placed #user" and the user which was #mentioned should have thier reaction removed from the origional post. I have no problem getting the message ID and user ID, but I cannot seem to combine the two to remove thier reaction. Here is an extract from my code:
message.channel.messages.fetchPinned().then(messages => {
console.log(`Received ${messages.size} messages`);
var testuserid = message.mentions.users.first().id;
messages.forEach(message => {
message.reactions.resolve("👍").users.remove(testuserid);
})
})
The problem is that last line message.reactions.resolve. I have tried every combination of using the emoji character or code, hardcoding the user ID, etc etc
I always seem to end up with an error similar to:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property
'users' of null
message.reactions.removeAll() does work, but removes all reactions including the bot's.
I got this code from here but have tried many other combinations of code including this one which I cant seem to get to work at all (something to do with not being in an async function).
Please tell me I am missing something simple!
I don't know if you've taken a look at the docs for the message.reactions.resolve() method, but the way you're trying to use it isn't quite the right way to use it (and isn't how it is intended to be used). message.reactions.resolve() only takes in two different types of parameters: either a MessageReaction object or a Snowflake (the specific message reaction's ID -- note that this does not refer to the ID or value of the emoji, but of the specific reaction itself).
So basically, the .resolve() method is supposed to convert an ID into an object. So if you have a message with ID 811386028425609247 and you do messages.resolve(811386028425609247), it will return the specific message whose ID that is. (Of course, that's an example with messages and not message reactions). In your case, that's not what you want to do either. .resolve() would be used to get a single, specific reaction using the ID of that reaction, which you don't have.
And note that it is the ID of the reaction itself that I am referring to, and not the ID of the emoji being reacted. If I were to react with a 👍 on two different messages, the emoji ID would be the same for both (because they are the same emoji), but the reaction ID would be different (because they are two separate reactions).
What you really want to do is find a specific emote that has been reacted on a message, and remove the user that reacted that. So what you're actually looking for is something like:
message.reactions.cache.find(reaction => reaction.emoji.name == "👍").users.remove(testuserid);
What this new line of code does is it looks through the reactions on the message, finds the reaction with the 👍 emoji, and then removes the user from that reaction. And just for fun, I'll add what your old line of code was doing: it was looking for a specific reaction with ID "👍", which of course doesn't exist, and attempting to remove the user from that nonexistent reaction (hence the error you were getting: Cannot read property 'users' of null, because the reaction itself was nonexistent and therefore null).

I cant get an item from an array for my Discord bot to use to take action

I am trying to program a bot for a server, and I am currently working on making a 'Mute' command. My idea was to make it so when the command is called, the bot must first check if the person has the role which allows them to mute other members, if that condition is met, then the bot would take the second argument, aka the Discord id of the member which must be muted, and give them a role which inhibits them from speaking in the server.
For some reason when I was testing the code, I wasn't able to get past the bot checking whether the second argument was a valid ID for a member of the server.
Here is the mute command code:
if (message.member.roles.cache.has('765334017011613717')) {
console.log('Oh oh');
const person = message.guild.member(
message.mentions.users.first() || message.guild.members.get(args[1])
);
if (!person) {
console.log('Oh oh 2');
}
} else message.channel.send('You do not have permission to use this command.');
Note: The console.log() functions were added so I could see where the problem was happening when running the code.
There are two things that you might do in order to make this work with the first being more of a suggestion.
First, ideally you always want to check if the role exists and return if it doesn't. This prevents you from having to put your entire logic to mute the person into the if statement. You do that like this:
if (!message.member.roles.cache.has('your role ID')) {
return message.channel.send('You do not have permission to use this command.');
}
The actual problem in your code stems from your way of finding the member. The right way to do that is, either checking the first mention of a member or taking a provided ID from your first argument. Your first argument being not args[1] but args[0]. If the ID is being used you need to use the cache property. Source
let muteMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
You should then check if a member has actually been found and return if not.
if (!muteMember) {
return message.channel.send("That member doesn't exist!")
}
Note: You already have this in your code but I wanted to include it for completeness.
After that you can continue with your code to mute someone.

How do I remove a reaction from a message by a specific user in Discord.js?

I'd like to remove a reaction from a specific user for a message I've fetched. However I couldn't get it to work (probably because I'm a weeb who can't do Javascript), so it'd be nice if I could get some help :)
I tried looking around such as [this]Remove a users reaction from fetchMessage? - Discord JS thread, however it returned as map was undefined.
The most recent I came up with was this:
var messageEmbed = message.channel.fetchMessage('637189841418846208')
.catch(console.error);
messageEmbed.reactions.remove(message.author.id)
I want it to remove the reaction but instead I got the error "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'remove' of undefined"
"reactions" is a collection of reactions, therefore you need to first find the specific reaction you are looking for, this example fetches the first reaction on the message and attempts to remove a user from the reaction.
let messageEmbed = message.channel.fetchMessage('637189841418846208')
.catch(console.error);
messageEmbed.reactions.first().remove(message.author.id)
There are many other ways to search within a collection for a specific reaction, a general one is the .get() method witch in this case will search for the reaction based on it's ID
messageEmbed.reactions.get("REACTION ID")
To see all of the methods available see, Collection, Reaction.

Categories

Resources