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
}
Related
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
}
so what my bot is meant to do is that when someone joins a certain channel, it will send a message to log channel "SomeGuy123 joined the channel!". So I was constructing it for like an hour, and now I resolved all the errors, but it doesnt say anything, nor it doesnt give any errors. I can send the whole code if you want. Here is just the part about sending the message upon joining:
client.on("voiceStateUpdate", (oldState, newState) => {
const newUserChannel = newState.ChannelID;
const oldUserChannel = oldState.ChannelID
const textChannel = newState.guild.channels.cache.get('715141269395079208')
if(newUserChannel === '715141827644358707') {
textChannel.send(`${newState.user.username} (${newState.id}) has joined the channel`)
} else if (oldUserChannel === '715141827644358707' && newUserChannel !== '715141827644358707') {
textChannel.send(`${newState.user.username} (${newState.id}) has left the channel`)
}
})
Thank you in advance.
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate
https://discord.js.org/#/docs/main/stable/class/VoiceState
<VoiceState>.ChannelID is undefined, its <VoiceState>.channelID, javascript is not pascal case except in classes
Right now I am making a discord bot (in version 12 of discord.js).
It works like this:
Someone sends and insult
If the insult is included in a list (stored in insultes.json) the bot send a message and adds a reaction
If we add the same reaction the bot sends another message
The problem I'm facing is that if I keep adding the reaction the bot keeps replying 2, 3, 4 times and so on: every time (n) I check the reaction it replies with n+1 messages.
This is the code:
bot.on('message', message => {
const insulte = require('./insultes.json');
for (let p = 0; p < insulte.length; p++) {
// Check if the insult is in the list and make sure it's not from the bot itself
if (message.content.toLowerCase().includes(insulte[p]) && message.author.id !== "711337757435363468") {
message.channel.send("First message").then(messageReaction => {
messageReaction.react("➡️");
});
bot.on('messageReactionAdd', (reaction, user) => {
if (reaction.emoji.name === "➡️" && user.id !== "711337757435363468") {
message.channel.send("Additional message");
}
});
}
}
});
I think your problem comes from the fact that you're using bot.on('messageReactionAdd', ...: that means that every time you run that part of the code the code will add another listener that adds up to the ones that you used before.
Also, that code will trigger when a reaction to any message is added, not just the one you sent.
From your question, I don't understand if the bot is supposed to reply with one message every time you hit the reaction on that message, or just do it once and then ignore the message. I'll assume it's the latter.
Here's my take on this:
bot.on('message', message => {
const insults = require('./insultes.json')
if (insults.some(i => message.content.toLowerCase().includes(i)) && message.author.id !== "711337757435363468") {
message.channel.send("First message").then(myMsg=> {
myMsg.react("➡️");
let reactionFilter = (reaction, user) => reaction.emoji.name === '➡️' && user.id !== "711337757435363468"
myMsg.awaitReactions(reactionFilter, { max: 1 }).then(() => {
myMsg.channel.send('Additional message')
})
});
}
})
As you can see, I'm using Array.some() to check whether any insult is in the message, instead of a for loop. I'm using Message.awaitReactions() to fetch the first reaction and respond to that: after that, the bot will just ignore any other reaction on that message, but will still work on others.
Feel free to let me know if something is not clear or doesn't work :)
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');
}
})
So, I want my Discord bot to send a message to a specific user whenever someone joins into a specific channel. But my code doesn't seem to work.
Does someone know how to fix it?
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
if(voiceChannel.id === "530501827389685762"){
client.users.get("188984116618854400").send("Ein neuer Spieler ist im Aufnahmebereich!")
}
else {
return;
}
}
})
On line 6, you write:
if (voiceChannel.id ...
voiceChannel is not defined.
For future reference, include the error. Some style remarks:
1. the else and return statement are unnecessary if that's the end of your function
2. research truthy and falsy values in javascript, they can save you some time checking if channels are undefined.