reaction a only first message in "if" - javascript

client.on('message', message => {
if (message.channel.type === 'text' && message.channel.name.toLowerCase() == "channel-2" && message.content === "test")
{
message.react("✅")
}
})
this is the code, is there any way for it to react to just the first message? for example: if 5 people send "test" on the same channel, I wanted her to react only to the first, ignoring the other 4

Just call .off with the handler when you react:
client.on('message', function handler(message) {
if (message.channel.type === 'text' && message.channel.name.toLowerCase() == "channel-2" && message.content === "test") {
message.react("✅");
client.off('message', handler);
}
});

Related

Delete a Discord message if it doesn't start with "!test"

I want the bot to delete the message if it doesn't have the command !test
client.on("message", function(message) {
if (message.channel.id === '823216214737289266') return;
if (input.indexOf("!test") != -1) {
if (J===null) {
setTimeout(function() {
bot.deleteMessage(message);
}, 1000);
}
}
});
You can use the startsWith() method to check if the message content starts with the argument provided. You could also just simply delete the message using message.delete():
client.on('message', (message) => {
if (message.channel.id === '823216214737289266') return;
if (!message.content.startsWith('!test')) {
// not sure what J is, so leaving it here
if (J === null) {
setTimeout(() => message.delete(), 1000);
}
}
});

Discord.js counting system

so I was trying to make my own version of a counting system for my server like how other bots such as countr do it, so I made the following:
if (message.channel.id === "794733520458612736") {
const numdb = db.get("numdb");
if (message.content === numdb) {
db.add("numdb", 1);
message.react("✅");
} else if (typeof message.content === "number") {
db.set("numdb", 1);
message.channel.send(`${message.author} ruined it at **${numdb}**! The next number is **1**.`);
message.react("❌");
};
};
Yet, when I try it, it doesn't seem to work. Does anyone know what I am doing wrong?
Alright, I solved it as shown below:
if (message.channel.id === "794733520458612736") {
const numdb = db.get("numdb");
if (message.content == `${numdb}`) {
db.add("numdb", 1);
message.react("✅");
} else if (!isNaN(message.content) && message.content != `${numdb}`) {
db.set("numdb", 1);
message.channel.send(`${message.author} ruined it at **${numdb-1}**! The next number is **1**.`);
message.react("❌");
};
};
I figured out that the reason was because === checks both value and type (credit to Zsolt) and the old method I used to check if it was a number didn't work, so I switched it to isNaN.

Bot plays same audio for joining and leaving the channel

my Bot is playing the same audio File for joining and leaving the Voice Channel and i dont know how to solve it:
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
const voiceChannel = client.channels.cache.get('807305239941480543')
if(oldUserChannel === undefined && newUserChannel !== undefined && oldMember.member.user.bot === false && newMember.member.user.bot === false) {
voiceChannel.join().then(connection => { connection.play("./a.mp3").on("finish", () => connection.disconnect())
});
} else if(newUserChannel === undefined && oldMember.member.user.bot === false && newMember.member.user.bot === false){
voiceChannel.join().then(connection => { connection.play("./b.mp3").on("finish", () => connection.disconnect())
});
}
})
I dont get any error and the Bot works but the Bot is playing the audio file ./b.mp3 everytime and not the audio ./a.mp3.
Thanks!
You are already on the right track but you're overthinking the if statement.
First off, when the voiceStateUpdate event is triggered, the parameters available are voiceStates so we'll use oldState and newState in this code.
You have the right idea when you check if the user is a bot but you can do that in a separate check.
if (oldState.member.user.bot) return;
Now that we know a user is joining/leaving we can differentiate, however we need to check three cases:
a user joins a new channel, here case A
a user leaves a channel, here case B
a user switches channels, here case C
A joining is indicated if oldState.channelID is either null or undefined.
oldState.channelID === null || typeof oldState.channelID == 'undefined'
A leaving is indicated if newState.channelID is either null or undefined.
newState.channelID === null || typeof newState.channelID == 'undefined'
A switching is indicated if neither of the above fit which we simply cover with else.
Since I don't have your MP3 files I simply console.log() a "PLAYING (A/B/C)" which you need to switch out for your audiofiles.
The entire voiceStateUpdate looks like this:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.member.user.bot) return;
const voiceChannel = client.channels.cache.get('807305239941480543');
if (oldState.channelID === null || typeof oldState.channelID == 'undefined') {
voiceChannel.join().then(connection => {
console.log("PLAYING (A)");
connection.disconnect();
});
} else if (newState.channelID === null || typeof newState.channelID == 'undefined') {
voiceChannel.join().then(connection => {
console.log("PLAYING (B)");
connection.disconnect();
});
} else {
voiceChannel.join().then(connection => {
console.log("PLAYING (C)");
connection.disconnect();
});
}
})
;

javascript how to fill a return from values of array

I don't know how to formule the question(title), so I'll just say it here, and maybe someone may help or provide a new point of view about it.
I'm working with discord.js and I want to check if user has a role/roles from a message
client.on('message', message => {
if (message.member.roles.find(r => r.name === 'ADMIN')) {
console.log('I am admin');
}
});
Now my idea is to make this a polymorphism functions, because if I want to check two roles I have to do this:
client.on('message', message => {
if (message.member.roles.find(r => r.name === 'ADMIN') || message.member.roles.find(r => r.name === 'Other')) {
console.log('I am admin');
}
});
And I would like to move this if condition to a function and it will check it even if I want to check one or more `permissions.
For now I have tried to deal with it, but nothing, and stopped here... Maybe someone can give me some advice or other point of view. The idea is to prevent duplicate code and an if as long as the width of my monitor.
function hasRole(msg, permissions, condition = 'or') {
if (Array.isArray(permission)) {
if (condition === 'or') {
permissions.forEach(function (element) {
});
// permission.join('||');
return msg.member.roles.find(r => r.name === permissions) ||
msg.member.roles.find(r => r.name === permissions);
} else if (condition === 'and') {
}
}
return msg.member.roles.find(r => r.name === permissions);
}
If you want to check DiscordJS DOCS.
Response:
function checkPermissions(msg, permissions, condition = 'or') {
if (condition === 'or') {
return msg.member.roles.some(r => permissions.includes(r.name));
} else if (condition === 'and') {
return msg.member.roles.every(r => permissions.includes(r.name));
}
}
Something like this?
function hasRole(msg, permissions, condition = 'or') {
//harmonise #permissions to array, if isn't already one
permissions = Array.isArray(permissions) ? permissions : [permissions];
//if or, at least 1 member role must be present in #permissions
if (condition === 'or')
return msg.member.roles.some(role => permissions.includes(role));
//if and, all member roles must be found in #permissions
else
return permissions.every(perm => msg.member.roles.includes(perm));
}
function checkPermissions(msg, permissions, condition = 'or') {
if (Array.isArray(permissions)) {
if (condition === 'or') {
return msg.member.roles.some(r => permissions.includes(r.name));
} else if (condition === 'and') {
let value = false;
permissions.forEach(permission => {
value = !!msg.member.roles.find(r => r.name === permission);
});
return value;
}
}
return !!msg.member.roles.find(r => r.name === permissions);
}

Trying to figure out the issue with my code

I have a problem with my code and try to find the error
I have already tried to use let instead of var but it still did not work. There is also no output in the console.
I believe the error to be within the if (tylerdel == true) of my code:
if (command === prefixfile.prefix + `active`) {
var tylerdel = true
message.channel.send (`test`)
if (tylerdel == true) {
if (message.author.id === ("")) {
message.delete (1)
}
}
}
It is supposed to delete a message if it comes from a certain person but I also need it to be toggleable.
As per your code boolean variable tylerdel will always be true so there is no need to use this variable in your if condition.
if(command === prefixfile.prefix + 'active') {
message.channel.send('test');
if (message.author.id === '') {
message.delete(1);
}
}
Be careful with == (Equality) and === (Identity).
More info about the operators
You should know what is "Debugging". You can try printing something in each if to see where is the problem.
Hope this helps you to solve your problem.
Try this:
if (command === prefixfile.prefix + 'active') {
var tylerdel = true;
message.channel.send ('test');
if (tylerdel) {
if (message.author.id === '') {
message.delete(1);
}
}
}

Categories