Hey everyone! I’m a newbie to JavaScript and I’m working on a Discord bot. I’ve got a basic command working, but I’m looking to add more features. Here’s what I have so far:
if (message.content === '!ping') {
message.reply(`Your ping is ${client.ws.ping}ms`);
}
I want to create a ‘!say’ command where the bot repeats what the user types. Also, I’d like to set it up so only users with a certain permission level can use this command. Maybe something like level 8 or higher?
Can anyone help me figure out how to do this? I’m still learning discord.js, so any tips or code examples would be super helpful. Thanks in advance!
hey claire! for the !say command, try something like:
if (msg.content.startsWith(‘!say’)) {
if (msg.member.roles.cache.some(r => r.name === ‘Moderator’)) {
let txt = msg.content.slice(5);
msg.channel.send(txt);
}
}
This checks for the ‘Moderator’ role. adjust as needed for ur server setup. good luck with ur bot!
Hey Claire29, I’ve been tinkering with Discord bots for a while now, and I think I can help you out. For the ‘!say’ command, you’ll want to use message.content.slice() to grab everything after the command. As for permissions, instead of using a specific level, it’s often easier to check for a role. Here’s a quick example:
if (message.content.startsWith('!say')) {
if (message.member.roles.cache.some(role => role.name === 'Trusted')) {
const sayMessage = message.content.slice(5).trim();
if (sayMessage) {
message.channel.send(sayMessage);
message.delete().catch(console.error); // Optionally delete the command message
} else {
message.reply('You need to specify something for me to say!');
}
} else {
message.reply('Sorry, you don\'t have permission to use this command.');
}
}
This checks for a ‘Trusted’ role, but you can change that to whatever fits your server structure. Hope this helps you get started!
To implement the ‘!say’ command with permission checks, you can use the following code:
if (message.content.startsWith('!say')) {
if (message.member.permissions.has('MANAGE_MESSAGES')) {
const content = message.content.slice(5).trim();
if (content) {
message.channel.send(content);
} else {
message.reply('Please provide a message to repeat.');
}
} else {
message.reply('You do not have permission to use this command.');
}
}
This checks for the ‘MANAGE_MESSAGES’ permission, which is typically assigned to moderators or higher roles. You can adjust the permission check based on your server’s hierarchy. Remember to handle empty messages and provide feedback to users without the required permissions.