Auto deleting messages in a specific channel - javascript

I have a suggestion channel, and i made it so users are only allowed to post links in it, with the ability for the bot to react on what they post.
I have done the auto react on links, but i could not make the bot deletes what is not a link.
So i want everything else that is not a link to be deleted.
bot.on('message', message => {
// whenever a message is sent
if (bot.id === '514484773171757061') {
return;
}
if (message.channel.id === "508606903740268545" ){
if (message.content.includes('playrust.io/')) {
message.react('✅').then( () =>{
message.react('❌')});
} else if (message.delete()) {
message.channel.sendMessage('Message has been Deleted ' + message.author)
}
}
});
It is kind working but not very well.
It deletes the message that is not a link million times and sends million notifications :/
I guess the problem is with else if part

I think the problem is that you are creating a sort of infinite loop. In your code you first check if the message send is in a specific channel, after which you check if it contains a link or not. If it doesn't you send a message to that same channel saying 'I didn't find a link'.
When you send this, your bot gets triggered again because a new message has been send. It checks whether it's in a specific channel and if it contains a link, which it doesn't, and thus the cycle repeats.
It can be fixed with one simple statement, which also happends to be very good practice when creating bots. To fix it, you need to include some code which checks if a message has been send by a bot or not. Look at the example below:
bot.on('message', message => {
// Ignore messages from all bots
if (message.author.bot) return;
/* All your other code here */

client.id is not a thing so it needs to be
if (message.author.bot) return;
Then instead of
else if (message.delete()) {
message.channel.send('Message has been Deleted ' + message.author)
}
Use
else {
message.delete(5000)
message.channel.send('Message has been Deleted ' + message.author)
}
Wich result in:
bot.on('message', message => {
// whenever a message is sent
if (message.author.bot) return;
if (message.channel.id === "508606903740268545" ){
if (message.content.includes('playrust.io/')) {
message.react('✅').then( () =>{
message.react('❌')});
} else {
message.delete(5000)
message.channel.send('Message has been Deleted ' + message.author)
}
}
});

Related

How do I fix DiscordAPIError: Unknown Message?

I made a bot that deletes text messages in an image channel, but when I use it it's logs this error.
client.on("message", async message => {
if (message.author.bot) return;
const users = ['853248522563485717'];
if (message.channel.id === '841260660960395287') {
if (users.includes(message.author.id)) return;
if (message.attachments.size !== 0) return;
message.member.send("You can't send text in 'images' channel")
await message.delete();
} else return;
});
(this is different from the other questions with the same topic)
how do I fix it?
The most likely problem is that you are running multiple instances of the Discord bot. Check if you have multiple command lines open, if so, check if they are running the bot. Apart from that, if users spam, Discord API tends to get confused about which messages to delete, making it try to delete the same message more than 1 time. You can try adding an if statement checking if the message is undefined.
client.on("message", async (message) => {
if (!message) return; // ADDED THIS LINE OF CODE
if (message.author.bot) return;
const users = ["853248522563485717"];
if (message.channel.id === "841260660960395287") {
if (users.includes(message.author.id)) return;
if (message.attachments.size !== 0) return;
message.member.send("You can't send text in 'images' channel");
await message.delete();
} else {
return;
}
});
From your error above, you are seemingly using a message event listener for every command or function you want your bot to perform. The maximum for this is 11 (of the same type). This is not the correct way to do this.
What is an event listener?
//the 'client.on' function is called every time a 'message' is sent
client.on("message", async (message) => {
//do code
});
How to fix this:
Instead of using 1 event listener per command, use 1 for the whole client. You could solve this a multitude of ways, with command handlers or such, however I will keep it simple.
This is the basic setup you should see in your index.js or main.js file:
// require the discord.js module
const Discord = require('discord.js');
// create a new Discord client
const client = new Discord.Client();
// on message, run this
client.on('message', (message) => {
if (!message) return;
console.log(`message sent in ${message.channel.name}. message content: ${message.content}`);
//this event will fire every time a message is sent, for example.
});
// login to Discord with your app's token
client.login('your-token-goes-here');
So what do you do if you want to do multiple functions with your bot? (for example deleting text in an images only channel and logging messages to console)
Command handling. It is the only way. I linked an official Discord.js guide above, read through it and see what you can do. I would highly recommend trying this with an entirely different bot and clean index file, as it would be easier than trying to fit in code you dont understand between your already problematic code.

Don't delete message if user has admin?

Basically I have a custom bot that deletes invite link when they're sent in the chat by users.
However if a user who has administrator permissions sends an invite link, I want the bot to ignore it and not the delete message.
Here is what I have:
client.on("message", async message => {
if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
message.delete()
});
Try this
client.on("message", async message => {
if (message.content.includes('discord.gg') || message.content.includes('discordapp.com/invite/')) {
if(!message.member.hasPermission("ADMINISTRATOR")){
message.delete();
}
});
Note: On future Discord.js updates member.hasPermission() might be replaced with member.permissions.has()
But till now permissions.has() cheates an error for me. So I suggest using hasPermissions()
Inside the if statement, include a line to check if the user has admin permission. Eg:if(message.member.permissions.has('ADMINISTRATOR')) return; would check if the user has an admin permission, and would return/not delete the message if he does. Or you can do the other way around, deleting the message if the user does not have the perms.
Eg:
if(!message.member.permissions.has('ADMINISTRATOR')){
message.delete();
}
Note: These statements has to inside the if statements that check if the message has an invite link.
if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
message.delete()
As of now, this just deletes every message that comes in.
So, what you want to do is to check if the author of the message has the permissions.
You can do this with:
message.member.hasPermission(//permissions here)
So, to add that to your code, you can do:
if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
if(!message.member.hasPermission("ADMINISTRATOR")){
)message.delete()
}
If they don't have Administrator, it's auto deleted.
You Can Read More Here:
https://discordjs.guide/popular-topics/permissions.html

Problem defining typingStart to a specific channel

I am trying to get my discord bot to send this message only if the user starts typing in the defined channel, and not other text channels. I don't get any errors, the bot just doesn't send a message. What am I doing wrong here? Can typingStart be defined to a specific channel?
const Join2_channel = "972135774921247676"
bot.on("typingStart", (message , channel) => {
if (channel.id === Join2_channel) {
message.send('Type !join');
}
});
The typingStart event takes two parameters; channel (the channel the user started typing in) and user (that started typing), in this order.
In your current code you're checking if the user.id is 972135774921247676 and as that's a channel's snowflake, it won't match. You need to update your callback function:
bot.on("typingStart", (channel, user) => {
if (channel.id === Join2_channel) {
channel.send('Type !join');
}
});

AFK command discord.js v12

I have already made an AFK command that changes the user's nickname. Here is my code:
client.on('message', (message) => {
if (message.content.includes('start-afk')) {
message.member
.setNickname(`${message.author.username} [AFK]`)
.catch((error) => message.channel.send("Couldn't update your nickname."));
}
if (message.content.includes('end-afk')) {
message.member
.setNickname('')
.catch((error) => message.channel.send("Couldn't update your nickname."));
}
});
I want it to add an AFK reason to be shown whenever the AFK User is pinged and if the AFK user talks then his AFK is removed. I tried however I was unsure how to do it.
If you want the bot to remember who is AFK and maybe the reason for it, you'll have to use a database to store these pieces of information.
You could use npm i quick.db, which is a really simple database to use.
Here's an example code:
client.on('message', message => {
// checks if the message author is afk
if (db.has(message.author.id + '.afk')) {
message.reply("Oh you're back ! i removed your afk")
db.delete(message.author.id + '.afk')
db.delete(message.author.id + '.messageafk')
}
if (message.content.includes('start-afk')) {
message.member.setNickname(`${message.author.username} [AFK]`).catch(error => message.channel.send("Couldn't update your nickname."));
// then here you use the database :
db.set(message.author.id + '.afk', 'true')
db.set(message.author.id + '.messageafk', message.content.split(' ').slice(2))
// I made .slice(2) so that in the message array it also delete the command and the "start-afk"
}
if (message.content.includes('end-afk')) {
message.member.setNickname('').catch(error => message.channel.send("Couldn't update your nickname."));
// Here you delete it
db.delete(message.author.id + '.afk')
db.delete(message.author.id + '.messageafk')
}
});
Then, whenever the user gets pinged:
client.on('message', message => {
// If one of the mentions is the user
message.mentions.users.forEach(user => {
if (db.has(user.id + '.afk')) message.reply('the user ' + user.tag + ' is afk !')
})
})
Well first you would need a place to store the reason.
Then you get the reason from the end of the message.
Then store the reason with the user id of the user.
Then inside of the message listener, check if the author of the message is inside the array/map or whatever u choose to store the data in.
If the user doesnt exist inside the afk "list" just ignore
If the user exist inside the afk "list" just remove them from the afk "list".
Then just check if there is a mention inside the message by doing message.mentions.users.first() if a mentions exists just get the users afk reason from the list and send it to the channel.
Then if the user does end afk just remove them from the "list".
That's the idea anyways

Error when wanting to read the messages of a specific channel

I'm trying to "moderate" a channel, in this one should only be able to send certain commands, and when they send a command or message different from the one allowed, then I would send a message warning about it. The logic of moderation I have with the conditions, but my problem is that I can not get the messages that are sent on that channel (of anyone who writes on that specific channel)
When executing the code it does not show me anything in the console, which means that it is not recognizing the messages that they send in that channel :(
Code:
client.on("message", (message) => {
if (message.author.tag === "NAME#1234") {
if (message.content.startsWith(prefix + "on")) {
console.log(message.channel.id)
if (message.channel.id == "361344824500289538") {
//If the channel where they send the message has the id that I have set, then show me the messages that are sent
console.log(message.content)
} else {
console.log(message.content)
}
}
}
});
Let’s say you want to have a command only to be used in one channel (this could be /meme for a channel called “memes”). If the command is used elsewhere the bot will say “Command Not Allowed Here!”
Here Is The Code:
client.on("message", message => {
if (message.content.startsWith('/meme') {
if (message.channels.find(c => c.name ===! 'memes') {
message.reply("Command Not Allowed Here!")
} else {rest of meme command script}
}
}

Categories