Discord.js counting system - javascript

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.

Related

'messageCreate' keeps firing when an image is attached to the message

client.on('messageCreate', message => {
if (message.author.id === client.user.id) return;
if (message.author.bot) return;
var findChannel = message.guild.channels.cache.find(ch => ch.name === "temp-channel");
if(message.channel === findChannel) {
if(message.attachments.size > 0) {
var playerPoints = db.get(`${message.author.id}_points`);
if (!playerPoints) {
db.set(`${message.author.id}_points`, 1);
} else {
db.add(`${message.author.id}_points`, 1);
}
message.reply(`**+1 Game Point**, you now have **${db.get(`${message.author.id}_points`)}** points`)
}
}
})
For the most part, it works. But the issue is when a message is sent with an image, it repeatedly replies to the message author non-stop. What have I done wrong here?
I've been on discord.js v12 for years, but v13 changed too much..
I have tried numerous ways to attempt to stop the bot from repeatedly replying to the author, but it doesn't work.

Why does bulkDelete not work with no error?

I've tried to look for typo and other inaccuracies and tried to add permission requirement for the prune command but still, the ping pong and the "not a valid number" replies work but not the prune when I enter the amount.
Details: I'm trying to make a Discord bot that can prune based on input. I use DJS v12 and follow(ed) this guide https://v12.discordjs.guide/creating-your-bot/commands-with-user-input.html#number-ranges
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
if (!msg.member.hasPermission("BAN_MEMBERS")) {
msg.channel.send("You don\'t have permission.");
}
const args = msg.content.slice(prefix.length).trim().split('/ +/');
const cmd = args.shift().toLowerCase();
if (cmd === `ping`) {
msg.reply('pong.');
} else if (cmd === `prune`) {
if (!msg.guild.me.hasPermission("MANAGE_MESSAGES")) return;
const amount = parseInt(args[0]) + 1;
if (isNaN(amount)) {
return msg.reply('Not a valid number.');
} else if (amount <= 1 || amount > 100) {
return msg.reply('Please input a number between 1 and 99.');
}
msg.channel.bulkDelete(amount, true).catch(err => {
console.error(err);
msg.channel.send('Error!');
});
}
});
The reason why your prune command doesn't work, is because of your command parsing. args is always null whereas cmd always contains the whole string.
So if you enter $prune 3, your args will be empty and cmd contains prune 3. That's why your if here:
else if (cmd === `prune`)
doesn't match (if you specified arguments) and your prune command never gets executed.
To fix that, you have change your command parsing:
const cmd = msg.content.split(" ")[0].slice(prefix.length);
const args = msg.content.split(" ").slice(1);
Note: Also you seem to have a typo in your question:
if (!msg.guild.me.hasPermission("MANAGE_MESSAGES") return;
// Missing ")" here ----^
So change that line to
if (!msg.guild.me.hasPermission("MANAGE_MESSAGES")) return;

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