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");
...
Related
I need to check if a message sent by user contains emojis because my database can't store this type of data. So I thought that I'll use a message.content.match() or message.content.includes() but when I use it, it still is not enough. I was thinking about making something like blacklist but for emojis and then I realized that I need to save a blacklist of all emojis so I gave up on that. My question for you is, do you know any easier way to make this? I was searching for solution to my problem but I didn't find anything.
Thank you a lot for any help.
if(message.author.id!='botid' && message.author.id===userdbId && message.content.match(/<a?:.+?:\d+>/)){
const name = args.join(" ");
const username = name.slice(0);
conn.query(`UPDATE users SET ignick='`+username+`' WHERE userID='${message.author.id}'`);
console.log(username);
message.channel.send("success message");
conn.end(err => {
if(err){
throw error;
}
console.log('Disconnected from database');
})
}
else{
console.log('bot has been stopped from adding his message to database');
}```
At top of this code i made a connect function and two constructors to pull from database userId
Whenever an emote is used in a message, it follows this format: <:OmegaStonks:723370807308582943>, where the name of the emote is "OmegaStonks" and the id links to the link to the image, like so: https://cdn.discordapp.com/emojis/723370807308582943.png
Detecting this pattern is pretty easy using regex.
<a?:.+?:\d+>
which takes any character from the first : to the second : (and I used a ? to make the wildcard . stop as soon as possible). You also can't have colons in emote names, so it won't abruptly stop there.
Source
Here is how you could do it
client.on('message', msg => {
if(msg.content.match(/<a?:.+?:\d+>/)) return; //or whatever action(s) you want to do
})
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 make a command won't work if it's not in a certain channel. When I run my code, I get this error: SyntaxError: Unexpected token '!'
module.exports = {
name: "kill",
desciprtion: "idk",
if (!message.channel.id === '794303555975643136') return;
const { member } = message;
member.roles.add('794308638125981726')
}
In JavaScript and most other languages, you could refer to ! in functions as not.
For example, let's take message.member.hasPermission().
If we add ! at the start, giving us:
if (!message.member.hasPermission('ADMINISTRATOR') return
We're basically telling the client, if the message member does **not** have the administrator permission, return the command.
Now, let's take your if statement, saying if (!message.channel.id === 'id') return, you're basically telling the client if not message id equals to id return the command, which let's face it makes totally no sense.
When we want to compare a variable with a certain value, we would want to tell the client if the variable is **not** equal to value, return the command.
Hence why, you should fix your if statement into saying:
if (message.channel.id !== '794303555975643136') return;
Sorry for the pretty long answer, felt like giving a little lesson to someone :p
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.
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