Discord js detect other bot's message - javascript

I'm trying to detect a message that comes from another discord bot. Like for example, if the other discord bot says the word "captcha", my discord bot will detect it and ping me. Hopefully, there's also a way to detect another bot's embed too, not only messages.

You can detect if a user is a bot using the bot property on a User.
// create a message event
client.on('message', (message) => {
if (message.author.bot) {
// if the message is a bot
console.log(`${message.author.username} sent: '${message.content}'`); // you can fetch the message text through `message.content`
if (message.embeds[0])
// if the message includes an embed
console.log(
`${message.author.username} sent an embed with the title ${message.embeds[0].title}`
); // you can fetch the embed content through `message.embeds[0]`
}
});

You can get the bot property from author (user object)
message.author.bot will return true if whoever sent the message is a bot, otherwise false.
For your "captcha" detection you would just need to check message.content

Related

Discord.js || Can bot track ban list?

There are a lot of to ban a user in a server, you can do it manually or with another bot. I want my bot to scan a banlist, and if someone will get banned, it will send a message "user (id) has been banned
I started writing smt but now, I had no idea what can I do to check it.
EDIT: It's working now, but it still need to find this user id.
client.on('guildBanAdd', message => {
client.user.cache.find() //idk how to define it
const channel1 = client.channels.cache.find(channel => channel.id === "158751278127895");
channel1.send('12512');
})
You don't have to fetch bans because the guildBanAdd event parses a GuildBan object,which als includes a User object.
client.on('guildBanAdd', ban => {
console.log(ban.user.username); // Username
})
message.channel won't work because there isn't a message object.
If you want to get the executor (moderator) who performed the ban, kick etc.
There is a good guide for this Working with Audit Logs

how to make a bot Private message on command?

Wanted to make the bot dm a person who uses the command to get a hint based on the puzzle role they had.
did watch a YT vid that showed to dm a person based on a trigger. That didn't help as it needs too many triggers.
the bot should dm a person on what role they have
so I do know we need to use if (message.member.roles.cache.some(role => role.name === 'Puzzle 1')
Not completely sure on how to make it :'(
You can use message.author.send("YOUR MESSAGE HERE") for sending a private message to the user.
If you want to check user role by name before sending DM, you can make something like that:
if (message.member.roles.cache.some(role => role.name === 'Puzzle 1')){
message.author.send("YOUR MESSAGE HERE")
}
IMPORTANT NOTE: If a user disabled Allow direct messages from server members on privacy & safety settings, the bot can't send a message to a user. So I recommend you to use try/catch.

How can I create a "media only" channel on Discord using a bot? (JS)

I'm trying to code my Discord Bot (Js) in order to delete every type of text message in a specific channel while keeping images and videos. Anyone has any idea how can I do it?
You can use the property attachments of Message. It returns a Collection of MessageAttachments, which you can use to determine if there are attachments in the message. (links do not count as an attachment)
client.on("message", message => {
// Checking if the author is a bot.
if (message.author.bot) return false;
// Deleting the message if there are 0 attachments in the message.
if (message.attachments.size == 0) message.delete()
});

Bot Sends Multiple Times The Content Of The Message | Discord.js

I am trying to make a dark net bot for a roleplay discord server (FiveM), but when i send a message at the specific channel it send the message back multiple times
bot.on('message', message=>{
if(message.channel.id == "ID") {
if(isNaN(message.content)) {
message.channel.send(message.content)
}
}
});
The problem is that your bot is registering it's own message and, because it's the same as it's supposed to react on, it responds with the message content which in turn is the same as it's supposed to react on and so on and so on...
The good news is that you can fix this with a simple line of code.
if (message.author.bot) return;
Put that above your if(message.channel.id == "ID") and all will be fine. It checks if the author of a message is a bot and if so returns, meaning it doesn't execute the rest of the code.

Problems with messageDelete in Discord js

I try to make my bot on Discord server. Want to make a function, which will copy all deleted message in text channel, but, messageDelete hear only deleted message which was writing after bot start. When I delete message which make earlier bot start, its not work.
{
client.on ("messageDelete", messageDelete =>{
let channel = client.channels.find(channel => channel.name === 'log-deleted-message')
console.log(`Deleted :${messageDelete.content}`)
channel.send(`${messageDelete.author.username} write : ${messageDelete.content}`
})
}
The above answer is now outdated.
With Discord.js V12 you can now Cache messages on the messageDelete event, You would need to enable Partials. Once this is enabled, you can fetch the message beforehand like this:
if(message.partial){
let msg = await message.fetch()
console.log(msg.content)
}
That will then log the content of the previously uncached message.
messageDelete is an event that is called when a message is deleted while the bot is on. If a message is deleted before the bot is turned on there is no way to recover it, which is why it's known as deleted. The only way to accomplish the goal that you want is to leave the bot on permanently. Read more in the docs if you want more information.

Categories