I am currently working on a !mute command (or any other prefix) and since I am very new to JavaScript I am getting stuck. I have some of it down but can't get any father then that. I have a role set called "Muted". My code:
bot.on('message', message =>{
let args = message.content.substring(config.prefix.length).split(" ");
switch (args[0]) {
case 'mute':
let person = message.guild.member(message.mentions.first() || message.guild.members.get(args[1]))
if(!person) return message.reply("I could not find the person you are looking for... :thinking:")
}
})
According to the docs, muting a guild member is as simple as calling member.setMute(true or false, reason), as the example points out:
member.setMute(true, "It needed to be done") mutes the targeted member with "It needed to be done" provided as a reason.
Related
Hey so I am making a discord bot and when I use this to check for mention: message.mentions.members.first(); it will look for mention in whole message user has sent. I am trying to work this out because if user send message ?ban bla bla bla #user it will work. I want to check for mention only in args[0]. Is that possible? I am using discord v12. Thanks!
this is what i found to work...
if(args[0].slice(2).slice(0, -1) !== message.mentions.users.first()?.id) {
return message.reply("Please start with a user...")
}
the args[0].slice(2).slice(0, -1) if a mention... will be the id of the first mention... and if the mention is the first arg, it will also be the first mention. So what I did was took ID of the first mention and compared it to the sliced args[0] to see if they match, else it will return telling them to please start with a user... Make sure to keep the ? in message.mentions.users.first()?.id just in the case of no mention in the message, it will not cause an error to the process and will also return the please start with a user message.
Mentions
USERS_PATTERN uses RegEx for checking if string mentions about a User. If string matches with the pattern it returns true, otherwise returns false.
const { MessageMentions: { USERS_PATTERN } } = require('discord.js');
...
if (!args[0].match(USERS_PATTERN)) return message.channel.send("Please mention a user");
...
I have a message edit log but I want to stop sending the log if a mobs message was updated, I tried a few codes like
if(bot.oldMessage.content.edit()){
return;
}
It showed and error
cannot read property 'edit' of undefined
I then removed edit then content was undefined. The code for the message update is below.
The Code
module.exports = async (bot, oldMessage, newMessage) => {
let channels = JSON.parse(
fs.readFileSync('././database/messageChannel.json', 'utf8')
);
let channelId = channels[oldMessage.guild.id].channel;
let msgChannel = bot.channels.cache.get(channelId);
if (!msgChannel) {
return console.log(`No message channel found with ID ${channelId}`);
}
if (oldMessage.content === newMessage.content){
return;
}
let mEmbed = new MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL({dynamic: true}))
.setColor(cyan)
.setDescription(`**Message Editied in <#${oldMessage.channel.id}>**`)
.addField(`Before`, `${oldMessage.content}`)
.addField(`After`, `${newMessage.content}`)
.setFooter(`UserID: ${oldMessage.author.id}`)
.setTimestamp()
msgChannel.send(mEmbed)
}
How would I stop it from sending the embed if a bots message was updated.
Making a really simple check will resolve this issue. In Discord.js there is a user field that tells you if the user is a bot or not.
In fact, it is really recommended you add this in the "onMessage" part of your code as it stops other bots from using your bot, this is to make sure things are safe and no loopbacks/feedbacks happen, either way, you don't want a malicious bot taking advantage of your bot, which can get your bot in trouble too.
Here is what you want to do;
if (message.author.bot) return;
What this code specifically does is check if the message's author is a bot, if it returns true, it will break the code from running, if it returns a false, the code continues running.
You can do the same if you want to listen to bots ONLY by simply adding a exclamation mark before the message.author.bot like this;
if (!message.author.bot) return;
It is also possible to see what other kinds of information something holds, you can print anything to your console. For example, if you want to view what a message object contains, you can print it into your console with;
console.log(message) // This will show everything within that object.
console.log(message.author) // This will show everything within the author object (like ID's, name, discriminators, avatars, etc.)
Go ahead and explore what you can do!
Happy developing! ^ -^
That is really easy to do. All you need to do is check if the author of the message ist a bot and then return if true. You do that like this
if (oldMessage.author.bot) return;
I'm trying to create a bot that kicks someone after they say a specific word, but I don't want to have to mention the user after I say the word. This is my code so far:
case 'ok':
const user = message.member
if (user) {
const member = message.guild.member(user);
if (member){
member.kick('Banned').then(() =>{
message.reply(`Banned`)
})
}
}
break;
}})const PREFIX = '';
Is there some way to make the bot automatically kick the user after they say the case word "ok"?
You could simply use String.prototype.includes(), then kick them using GuildMember.kick() as you are now.
// in your existing message event
client.on('message', (message) => {
if (message.content.includes('ok')) // if the content includes 'ok'
message.member.kick().catch(console.error); // kick the member
});
I would recommend reading the full discord.js guide, as it can help you find a greater understanding of the discord.js library, and how to use it with javascript.
I am trying to create a bot that server-mutes everyone if a specific person mutes themself.
I can get the user ID or other details via message.member.
Is there any way to find out if that user is muted or even if the person is in a voice channel?
This is what I found so far:
client.on('voiceStateUpdate', (oldmember, newmember) => {
let oldvoice = oldmember.voiceChannel;
let newvoice = newmember.voiceChannel;
if (oldvoice && newvoice && oldvoice.id != newvoice.id)
var channelStatus = 'Moved'
console.log(oldvoice)
})
But I don't understand it and it just returns undefined.
Edit: Found a way easier solution:
msg.member.voice.selfMute
Old one:
client.channels.cache.get(msg.member.voice.channel.id).guild.voiceStates.cache.get(msg.author.id).selfMute
let's break it down:
client.channels.cache gives you of all text and voice channels in your discord server
get(msg.member.voice.channel.id) gives you the id of the voice channel, the user that wrote the bot command (considering, you have stored the message in a variable e.g.: msg)
.guild.voiceStates.cache gives you a list of everyone in the channel
.get(msg.author.id) takes the user that wrote the last message
.selfMute returns the mute state
btw there are other pretty useful things too like selfDeafen, streaming and more
My question may sound odd I apologize. But im working on a command for my bot that changes your own nickname when you type '>callme newNickName'.
case 'callme':
let nick = args[1];
setNickname(nick); //this is where i am stuck
break;
I'm not sure how to define the person who sent the command so that their own nickname gets changed accordingly.
Be sure that your bot has the appropriate permissions to manage and change nicknames otherwise this will not work.
You should be able to do it like this:
if (message.content.includes('callme')) {
if (!message.guild.me.hasPermission('MANAGE_NICKNAMES')) {
return message.channel.send('Not allowed');
}
message.member.setNickname(message.content.replace('changeNick ', ''));
}
Please see the GuildMember documentation for more detail: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=setNickname