If a message is from 3 specific users , ignore them - node.js

I want to know is it possible to stop specific users from using the bot, e.g. user b can not use any commands if they are in the blacklist of the config.json . thank you

Here is something you could use
const bannedUsers = ['userid1', 'userid2', 'userid3'];
client.on('message', msg => {
if(bannedUsers.includes(msg.author.id)) return;
//command execution code here
})
You will have to put this in all your client.on('message') listeners.

You don't show any code, but you could try something like this where you process commands..
//when bot receives a message
if (message.author.id === 'blacklisted ids'){
return;
} else {
//process commands
}

Related

Trying to dedicate a channel to only sending pictures and reddit links

Update: Problem with double condition solved. The Code Still Does Not Function.
client.on("message", message => {
if (message.author.bot) return;
if (message.author === client) return;
if (message.channel.id === "605839623372931093") {
if (message.attachments.size > 0) {
if (!message.attachments.every(attachIsImage) || !message.content.includes("https://www.reddit.com/")) {
(bulkDelete(message))
(message.channel.send("This channel Only Supports Picture Messages or Reddit Links!"));
}}}});
It is worth noting that my code doesn't give out any errors, it just doesnt do anything.
If you would like to know more, just ask me and I'm willing to answer.
You can use the OR operator (||) to check if the message doesn't fullfil either of your conditions. Your if statement would then look like this:
if (!message.attachments.every(attachIsImage) || !message.content.includes("https://www.reddit.com/")) {
// Delete the message and let the user know about the channel rules
}

How can i check if a person has went online, offline, etc. in discord.js?

const channel = client.channels.cache.get('<channelid>');
const person1 = client.users.cache.get('<userid>');
const person = client.users.cache.get('<userid>');
client.on('message', message =>{
client.on('presenceUpdate', () =>{
if(person1.user.presence.status === 'dnd' || person1.user.presence.status === 'online'){
channelforstatus.send('person1 is now online');
}
else if(peron1.user.presence.status === 'offline' || person1.user.presence.status === 'idle'){
channel.send('person1 is offline');
}
client.on('message', message => {
client.on('presenceUpdate', () =>{
if(person.user.presence.status === 'dnd' || person.user.presence.status === 'online'){
channel.send('person is now on');
}
else if(person.user.presence.status === 'offline' || person.user.presence.status === 'idle'){
channel.send('person is now off');
}
});
});
});
});
This is what I've tried and the .send() the function is not working. I've looked everywhere and found nothing that could help me with this problem. I just need it so it checks every time if a specific person has went online, offline, etc. And sends a message to a specific channel.
First of all, one rule to abide with is that event listeners should always be in top level of your code and never nested. (Else you are subject to memory leaks and other issues like duplicated and unintended code execution).
client.on("message", (message) => {
...
});
client.on('presenceUpdate', (oldPresence, newPresence) => {
...
});
Now when looking at presenceUpdate event and Presence object documentation you can manage to see if a status evolved like that :
client.on('presenceUpdate', (oldPresence, newPresence) => {
let member = newPresence.member;
// User id of the user you're tracking status.
if (member.id === '<userId>') {
if (oldPresence.status !== newPresence.status) {
// Your specific channel to send a message in.
let channel = member.guild.channels.cache.get('<channelId>');
// You can also use member.guild.channels.resolve('<channelId>');
let text = "";
if (newPresence.status === "online") {
text = "Our special member is online!";
} else if (newPresence.status === "offline") {
text = "Oh no! Our special member is offline.";
}
// etc...
channel.send(text);
}
}
});
Be aware that presenceUpdate event is fire by EACH guild the user and bot share, meaning that if user status change and share two guilds with your bot, this code will be executed twice.
In case you use presence but get offline instead of the user being online I spent like.. 2 whole days looking for the answer so ill share it anywayz
Common mistakes on presence.status is forgetting to check these stuff at the the developer applications. which i have no idea what means
A screenshot
now on your message (command handler) function.. if you have one
message.guild.members.cache.get('userId').presence.status
or
${message.author.username} is now ${message.author.presence.status};
Ill update this if I found out how to presence all the users instead of just one
my first post... I SHALL REMEMBER THIS xD
To get the presence, you can use user.presence, which will get all kinds of info about the user, but you only need user.presence.clientStatus.desktop
so your code would, for example, be
bot.on('presenceUpdate', () =>{
let person1 = bot.users.cache.get('USERID')
console.log(person1.presence.clientStatus.desktop)
if(person1.presence.clientStatus.desktop === 'dnd' || person1.presence.clientStatus.desktop === 'online'){
channel.send('person1 is now online');
}
else if(person1.presence.clientStatus.desktop === 'offline' || person1.presence.clientStatus.desktop === 'idle'){
channel.send('person1 is offline');
}
})

Unresolved method in WebStorm (discord.js, code works)

I'm wrapping my head around this. Autocompletion does not work .then(r => { HERE }) either.
Kinda starting out with this and would be way easier if it just works (works outside of the promise).
Code runs without any problems as well. delete method is recognized as well but not at that part.
I have this problem in a bigger project as well and it gets me confused.
Trying to find something on the web for a few hours, but couldn't find anything that helps me out. Hope I wasn't blind at some point :P
Whole test source:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something').then(r => r.delete(5000));
}
});
Problem:
If you need to delete command triget message you can use
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something')
message.delete(1000)
.catch(console.error)
}
});
If you use need to delete response message after some time you code must work, but you can try use reply method.
client.on("message", message => {
if (message.content === 'test'){
message.reply('somethink')
.then(msg => {
msg.delete(10000)
})
.catch(console.error);
}
});
Maybe problem in you discord.js version? In v.12 version you need use
msg.delete({ timeout: 10000 })
Just means webstorm can't discern what functions the object will have after the promise resolves. This is because the message create action in discord.js has multiple return types. So it's possible for the message object not to be passed into r, for instance in the event that the message failed to send, possibly by trying to send a message to a channel without the proper permissions.
If you add a check to confirm that r is the same type as message before trying to call .delete() I believe the warning will go away.
You can observe the potential error this highlight is warning you of by removing the bots permission to send messages in a channel, then by sending "test" to that same channel.
Having had the error recently and having found a solution, if for example you want a text channel, by doing a get you can have a text or a voice . so check if the channel instance TextChannel (for my example)
let channel = client.guilds.cache.get(process.env.GUILD_ID).channels.cache.get(config.status_channel);
if (channel instanceof TextChannel) {
await channel.messages.fetch()
}

Discord.js mentioning

client.on('message', message => {
if (message.content === `L!hug`) {
if (!message.mentions.users.size) {
return message.reply('you need to tag a user in order to hug them!!');
const taggeduser = message.mentions.users.first();
}
// message goes below!
message.channel.send(userID + ` you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
}
});
I have tried a few ideas and I am very new to this library of language (discord.js)
You can use message.mentions.members.first()
client.on('message', message => {
if (message.content.startsWith('L!hug')) {
let targetMember = message.mentions.members.first();
if(!targetMember) return message.reply('you need to tag a user in order to hug them!!');
// message goes below!
message.channel.send(`<#${targetMember.user.id}> you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
}
});
Discord.js uses many custom toString() and an User return his mention.
So if you want to mention an user in a message you can do
This :
`<#${user.id}>`
But a faster way is this :
`${user}`
And you can simply put user without any String it works also if the function runs a .toString() on your string.
like this :
message.channel.send(user + " has made something");
Will mention the user.
Note :
It won't work anymore in v13.
If you use a command handler like this one
this is how you would make it work
module.exports = {
name: "hug",
description: "# someone to hug them through the bot command.",
nsfw: false,
execute(message, args){
const targetmember = message.mentions.members.first()
if (!targetmember) return message.reply("you need to tag a user in order to hug them.");
var huggifs = [`${targetmember} Recieved a hug https://imgur.com/r9aU2xv`, `${targetmember} Recieved a hug https://tenor.com/LUqw.gif`, `${targetmember} Recieved a hug https://media.giphy.com/media/3ZnBrkqoaI2hq/giphy.gif`, `${targetmember} Recieved a hug https://tenor.com/1jRF.gif`, `${targetmember} Recieved a hug https://media.giphy.com/media/lrr9rHuoJOE0w/giphy.gif`]
var hugrandomform = huggifs[Math.floor(Math.random()*huggifs.length)];
message.channel.send(hugrandomform).then().catch(console.error);
}
}
Here, you can use message.mentions.users.first():
let user = message.mentions.users.first(); // As a shortcut to it
// Check if there is actually a mention
if(!user) {
return message.reply("You need to tag a user in order to hug them!");
}
// If there is a user, do this code
message.channel.send(`${user.toString()}, you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
// user.toString() will convert the user object to a mention
Hope this helped!
Here is a simple version of the code. userID isn't defined, meaning you'd need to use the targetmember. Here, this is how it should be.
client.on('message', message => {
if (message.content === "L!hug") {
const targetmember = message.mentions.members.first()
if (!targetmember) return message.reply("you need to tag a user in order to hug them!!");
message.channel.send(`${targetmember} you just got a hug https://tenor.com/view/anime-cuddle-cute-gif-12668750`);
}
})
You can use this to create a mention (note that it will not work in pre, as no mention works in it)
'<#&' + user_id + '>'
Note the # and &.

Discord bot JS Can someone explain what happened and how to prevent it?

When code is:
client.on('message', msg => {
const args = msg.content;
const command = args.toLowerCase()
if (command === 'frick'){
msg.reply('Sorry Sir this is a Christian server so no swearing! >:(');
}
});
message is sent normally
and when I added 'shit' to it
client.on('message', msg => {
const args = msg.content;
const command = args.toLowerCase()
if (command === 'frick' || 'shit'){
msg.reply('Sorry Sir this is a Christian server so no swearing! >:(');
}
});
this was a result
it just looped
and I know I can add a line which ignores bots but I want it to be active for bots as well
Your if statement always evaluates to true, because you are essentially checking if ('shit'), which is truthy because its length is greater than 0.
You aren't ignoring bot messages. So you're creating an infinite loop where bot sends message -> bot receives own message -> bot sends message -> bot receives own message -> ....
To fix 1, make sure you write your if statements properly:
if (command === 'frick' || command === 'shit') {
And to fix 2, you can add a simple if to the start of your message handler to check if the author is a bot:
if (msg.author.bot) {
return;
}
To make it even shorter you could do:
if (command === ('frick' || 'shit')) {

Resources