How to detect when bot is mentioned - discord.js

Before you mark this a duplicate, I have already tried this post: How to detect if the bot has been mentioned?
else if (message.mentions.has(client.user)) {
message.channel.send("https://images-ext-2.discordapp.net/external/L-agoqC2Qsf2sdz4tdrcD5hUiJe6moglhwHjXPi8McE/https/i.imgflip.com/3ia3r2.png")
console.log('Bot was mentioned')
}
I have it to log when it is mentioned but doesn't log, so i figure that it doesn't detect it being mentioned. Is there a different way to detect if the bot is mentioned?

The annoying thing with if (message.mentions.has(...) is that it would make the bot react even when #everyone or #here are mentioned, hence why I don't recommend using it.
The best thing yet, unfortunately, the ugliest thing to do, would be to have an if statement checking if the message includes a client mention, that can be done using:
if (message.content.includes(`<#!${client.user.id}>`) console.log('Bot was mentioned!')

In order to check for a mention to a specific Guild#GuildMember, you should write
if (message.mentions.members.has(<Client>.user.id)) {
message.channel.send("https://images-ext-2.discordapp.net/external/L-agoqC2Qsf2sdz4tdrcD5hUiJe6moglhwHjXPi8McE/https/i.imgflip.com/3ia3r2.png");
console.log('Bot was mentioned');
}
This will check for the direct mention to the bot.

Related

Nextcord: I want to mention the sender of the slash command I made. How would I go about that?

I'm trying to make this command notify my admins of a bot test and need it to mention the user who called the command. How would I go about that? I don't fully understand how to get that information with slash commands.
#client.slash_command(name= "test", description="(For Deputies or Leader only) Checks the operational state of the client.", guild_ids=[806043206030589952])
#has_any_role(leader_id, deputy_id)
async def test(interaction:Interaction):
bot_log = channel_up(940125176723554394)
await bot_log.send(f'<#&806045254834847776>,{} has started diagnostics for the bot. Please ignore any possible disturbances for the next minute or so.')
Thanks in advance for the advice, it's the first discord bot I've ever created.
EDIT:
In documentation, I found the solution. I have to use interaction.user.mention to get it to mention the user who sent the command. Or at least in theory, I'm dealing with a different issue now. Hopefully this helps people who also were as confused as me out.
You can use the interaction.user.name function with an # at the beginning.
The code should look something like this:
await interaction.response.send_message("#"+str(interaction.user.name))

Discord.py reacting to most recent message

I need help reacting to the most recent message. This can be in a specified channel or just in the server. The main thing I need help with is getting the message id or info about the most recent message, the reacting part I can do.
Please let me know if there is a solution, as everything I have looked up hasnt produced any results.
Thanks!
There are quite a few methods to get the last message in a channel. Assuming you already have a specific channel in which you want to react. Use
last_message = channel.last_message #channel must be a discord.Channel
The docs specify that this could sometimes get the wrong message, hence we use
messages = await channel.history(limit=1).flatten()[0]
References:
last_message
history
Tip:
Try searching for the relevant class in the docs instead of googling

One time only command possible?

I was wondering if its possible to have a command that can be used only once by a user on discord, if so please can someone give me the code for it I would much appreciate it.
First of all, SO is not a code-as-a-service platform or whatever. You give code, you tell your problem, and we help you. It's how it works. Here's my answer.
Of course, yes, it is possible. Anything that is based on logic can be reproduced by a computer. You just need to learn how to think as a computer would. In your case, it'd look like that in pseudo-code.
when the command is received:
has the user already used this command?
yes:
return "You've already used this command!"
no:
do the work
You would need to save the users who already have used the command. For simplicity reasons, the database you would like to use will be represented as an array.
let blockList = []
client.on('message', msg => {
if (msg.content === "!command") {
if (blockList.includes(msg.author.id)) return msg.reply("You've already used this command!");
msg.reply("This is the first time you're using this command! You won't be able to do it again. BUT since the blocklist is stored as an array, the list will be cleared when the program will stop.")
return blockList.push(msg.author.id)
}
})
Here you have the logic. It's up to you to integrate it into your code, with your database.
I hope I helped, and please, do not post another question that looks like that. Try, search, and ask! It'll be much nicer to help you, and I'm sure you would learn much more.

How to check if a command user has a certain role

I have been trying to make a quick bot for a friend, however I have come across something I cant fix. Whenever I try to access the member from the message it returns null. I am trying to find the users roles so I can check if they have a certain one.
The root of this problem that member is null inside the message. Therefor I cannot read from a null value.
client.on('message', message => {
This is how I am declaring message, and here is a console log of message. https://pastebin.com/vrdg9Wvu
So please can someone help me find a way to compare the command users roles.
Which version of dicord.js are you using?
I don't know where this may come from, maybe if the user who sent the message has left the guild while sending, otherwise I don't know what's wrong. If it's still not working, consider using message.guild.members.cache.get(message.author.id).then(user => {}), that may work.

How do I remove code from my discord bot?

Can someone please explain how to properly remove code from your discord bot?
So, recently, I started coding a discord bot. So far the only command I've made was, when I say "ping", it replies with "pong."
BUT. It would mention you with the "#" symbol. I didn't like that so I removed the function and instead made it just say "pong" without mentioning you. But, when I tested it, it would mention me and say pong. AND it would also just send a message that said pong. So I removed the whole code. NOPE it still kept replying to my message so I even removed the bot login method but it still kept replying with pong.
If there is something I need to fix in my code, then this is the code (and yes I made a variable for "bot"):
bot.on('message', message =>{
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'ping':
message.reply('pong!')
break;
}
})
It's because you're using message.reply(). This will always mention the original author. Use message.channel.send() instead.
Okay. So like, I'm actually dumb. All I had to do was restart visual studio

Resources