I'm extremely new to coding in JavaScript and I'm setting up a simple event bot for a Discord server.
It's very simple in concept, all the bot really does is allow anyone to assign a "Dead" role to any member they mention.
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
var prefix = "!";
client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`) });
client.on("messageCreate", (message) => {
let staff = message.guild.roles.cache.find(r => r.name === "Staff");
let dead = message.guild.roles.cache.find(r => r.name === "Dead");
let member = message.mentions.members.first();
if (!message.content.startsWith(prefix)) return;
//Ping command
if (message.content.startsWith(prefix + "ping")) {
message.channel.send("Pong!");
}
//Error if command doesn't include a mention
if (!member)
return message.channel.send('You need to mention a member, <@' + message.author.id + '>')
//Kill command
if (message.content.startsWith(prefix + "kill")) {
if (message.member.roles.cache.has(staff)) {
return message.channel.send(`${member} is not participating in the event!`);
}
if (message.member.roles.cache.has(dead)) {
return message.channel.send(`${member.toString()} is already dead!`);
} else {
member.roles.add(dead);
return message.channel.send(`${member.toString()} has been eliminated!`);
}
}
}
);
client.login('Token');
However, the problems I'm having are:
• The kill command, while it works in giving people the Dead role and announcing a member getting eliminated, it doesn't throw the corresponding errors if a member is already dead or has the Staff role. It just announces that the member has been eliminated, regardless of what roles they have.
• Typing any word with a ! before (like !test) results in the "Need to mention a member" error. (This includes the !ping command)
What am I doing wrong here? I've checked the Discord.js documentation for v13 and I can't seem to find an answer. Thanks in advance!
EDIT:
Fixed everything! I had forgotten a return for the ping and I ended up using incorrect functions for the kill command. This is the final code and it works as intended.
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
var prefix = "!";
client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`) });
client.on("messageCreate", (message) => {
if (!message.content.startsWith(prefix)) return;
//Ping command
if (message.content.startsWith(prefix + "ping")) {
return message.channel.send('Pong! <@' + message.author.id + '>');
}
//Kill command
if (message.content.startsWith(prefix + "kill")) {
let member = message.mentions.members.first();
let dead = message.guild.roles.cache.find(r => r.name === "Dead");
//No mention
if (!member)
return message.channel.send('You need to mention a member, <@' + message.author.id + '>');
//Staff check
if (member.roles.cache.find(r => r.name == "Staff")) {
return message.channel.send(`${member} is not participating in the event!`);
}
//Dead check
if (member.roles.cache.find(r => r.name == "Dead")) {
return message.channel.send(`${member.toString()} is already dead!`);
} else {
member.roles.add(dead);
return message.channel.send(`${member.toString()} has been eliminated!`);
}
}
});
Thank you all for your help! And feel free to critique the final code as well, all tips and tidbits are welcome!