Discord bot ignore all webhooks? - javascript

I have a discord bot that uses a leveling system. It listens to message.author.id, logs the latest message, gives the user XP and at certain levels, users are awarded a new role. Have been working fine for months.
Recently I added a few(10 to be exact) webhooks to the server to enable users to send in bug reports.
Problem is that my bot is reading those webhooks messages as well, and webhooks don't have an author ID, hence, bot crash every time a webhook is posting something.
I know we can except certain webhooks like this if (message.webhookID != 'X') return;
but its kinda inconvenient since I might add or delete webhooks in the future. Is there a way to make my bot ignore all webhooks similar to how it can ignore other bots?
EDIT
This is what I did,
client.on('message', (message) => {
//Check if its a webhook.
if (message.webhookID) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
if (message.guild.id != '335008833267040256')return;{
....

Yes, you can do if (message.webhookID) return;, that should return if the message is sent from any webhook.

Related

Discord - Automatically extract a message immediately after it appears in a channel?

I am yet to code this but actually struggling to understand how to do this
There is a private server with paid membership. I am a member. There are many channels on that server. I am particularly interested in a specific channel. Once someone posts a message in that channel, I want to immediately extract that message from a different program automatically and then process it in downstream systems. How to do that?
I know I'm a bit late to this question, but I'm going to tell you the secrets the Discord elites don't want you to know: This is completely doable, and many people do it. It's called a "self-bot".
Disclaimer: As the other answer stated, this is against ToS. You probably won't be caught, but I'd keep this on the down-low. Definitely don't go around telling this to everyone you meet.
Ok, now here's the actual coding part:
Discord.js, the most common library for discord bots in JavaScript, no longer supports self-bots, so you'll need to use an older version of discord.js. The official old versions have some unresolved bugs when used with modern discord, so I like to go with discord.js.v11.patch. Using this patched package will fix any weird bugs you get. Note: this is still discord.js v11, so if you need documentation, be sure to look at the v11 docs.
Ok, so after you run npm install discord.js.v11.patch (or npm install -g discord.js.v11.patch if you want to install it globally), you'll need to start writing code. Everything is basically the same as any old discord.js bot, but this is v11 so some stuff might be different. Here's some code to get you started. It should do everything you want it to do:
const discord = require('discord.js.v11.patch');
const client = new discord.Client();
const USER_TOKEN = 'XXXXXXXXXXXXXX'; // change this to your token
const CHANNEL_ID = 'XXXXXXXXXXXXXX'; // change this to the chanel you want to listen to.
client.on('ready', () => {
console.log('bot is running');
});
client.on('message', msg => {
if (msg.channel.id != CHANNEL_ID) return;
const message_text = msg.content;
console.log(message_text); // just an example
// send message_text somewhere to process it.
});
client.login(USER_TOKEN);
Now, all you need to do is change USER_TOKEN to your discord token and CHANNEL_ID to the ID of the channel you want to listen to.
To get your token, I recommend using this gist. It is safe and minimal. If you are worried about accessing your token, don't be. As long as you don't give it to anyone else you're fine.
To get the channel ID, simply turn on developer mode, then right-click on the channel you want to listen to. You should see a Copy ID menu button at the bottom of the context menu.
You can only do this if you have permissions to add an actual bot to that server, or have a friend that can do this for you. Using your own account as a bot would be possible, but is against discord ToS and would get your account banned.

Renew Bot Cache Discord.js v12

I have simple code, that checks if a user has an specific role:
message.member.roles.cache.some(role => role.id === 'role_id')
The problem is now, that when I change the roles, so remove the specific role while the bot is running, the user still has the rights. So in the Bot Cache, the user still has the specific role.
Is there any way to renew the bot cache when user roles change?
Thanks in advance
GuildMemberManager.fetch updates the cache. Simply do this above that statement:
await message.guild.members.fetch();
//rest of code with cached members

Discord.js bot reply with slash command

I have a working slash command, which is great. I can use the command with one input parameter eg /command string and it will execute the command.
However I am looking to set up a Discord bot that uses that command in a channel every 5 or so minutes. I can't seem to get the bot to use the command, any ideas on how to get it to work?
It just displays the string in the channel but the bot doesn't execute the command.
bot.on('messageCreate', async (msg) => {
if (msg.content === "!loop") {
interval = setInterval (function () {
msg.channel.send("/command string")
}, 3 * 1000);
}
})
Bots can only receive Application Command Interactions, not create them. This means your bot won't be able to run other bot's slash commands, click their buttons or use their dropdown menus.
If you have control over the other bot's code, though, you can set it up to listen for messages (not interactions) from your second bot, and run code accordingly.
But be aware: as MegaMix mentioned in their comment, if the bot you want to control isn't yours, you probably won't be able to do that, as it is a best practice to ignore messages from other bots to prevent abuse and infinite loops.

message.channel.members no longer contains all users in a text channel

I have a simple command that mentions a random user in a text channel. It worked perfectly for weeks, until today.
TL;DR:
channel.members is a map that used to contain all of the users of the text channel, but not anymore. Is there an error on my side or has something changed?
Before today, map would correctly contain all users that are in a text channel where message event happened. Now, for some reason, in this map there is an author and a bot itself + seemingly random people.
At first he seemed to also include people in a voice channel, and now nobody is in any of the voice channels and after the first message there was only me and a bot in the map, and when I asked someone to do a command, then he also has appeared in the map.
So that made me think that the channel property from message event's arg might have a map of people that messaged anything while bot was live, but how would that explain the people from the voice channel? Furthermore, before today, he would randomize anyone, even the people that have never posted a single message in any of the channels, ever.
Has Discord API changed recently? Because I cannot find any announcement or anything like that and I have no idea why it doesn't work anymore. I even checked out to few commits ranging from yesterday to couple days ago and it still behaves this way.
I double checked everything and I'm sure I have people that didn't say anything anywhere, and I see that bot has mentioned these people last time at 28 of October.
main.js
bot.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
require(`./commands/random_member`).execute(message, null);
});
random_member.js
module.exports = {
name: 'random_member',
usage: 'random_member',
description: "random_member",
execute(message, args) {
const member = message.channel.members.random();
message.channel.send(`${member}`);
}
}
Worthy Alpaca helped me with his comment.
"Firstly, you must manually enable the intents from the Discord Developer site. Go to applications, select your app, and find the "bot" tab on the sidebar. Then you can scroll down until you see this:"
I've enabled both of the options and my issues have disappeared.
Source:
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?

Can I somehow make my bot send a message in a specific text channel when it goes offline?

I already have a function which allows the bot to send a message when it is online, but if the bot is offline I want it to send a message saying that it is now offline so that people can check the activity of the bot.
No you cannot because there is no way for the program to know before the bot goes offline, unless you specifically make it go offline yourself, why can they not just check if the bot has an online/invisible status?
Another option is to do a ping command, if the bot responds, its online if not its offline:
client.on("message", msg => {
if(msg.content === "!ping") {
return msg.reply("Online!");
}
});

Categories

Resources