Discord Clear Command role permission doesnt work - discord

I have tried to make a simple discord bot with the command (!clear) with permissions. I want the "Admin perms" role to be the only role allowed to do the !clear command. Every time I try to do the command its says that message.guild.roles.find is not a function. The code I have right now is:
switch(args[0]){
case 'clear':
if(message.guild.roles.find(role => role.name === 'Admin perms')) {
if(!args[1]) {
return channel.reply ('How many message do you want me to delete idot!')
}
message.channel.bulkDelete(args[1]);

if(!message.member.roles.cache.find(r => r.name === "Admin perms")) return message.channel.send('You dont have permissions to do that idot!')
if(!args[1]) return message.reply('How many message do you want me to delete idot!')
message.channel.bulkDelete(args[1]);
break;

Try Doing this:
switch(args[0]) {
case 'clear':
if (message.member.roles.cache.find(role => role.name === 'Admin perms')) {
if(!args[1]) {
return channel.reply ('How many message do you want me to delete idot!')
}
else {
message.channel.bulkDelete(args[1]);
}
}
break;

Related

Declaration or statement expected. ts(1128) (11, 1)

Im quite new to coding and ive come across an issue which I'm not really sure what the issue is.
Ive been making my discord bot kick/ban command and its giving me the error "Declaration or statement expected. ts(1128) (11, 1)"
Here is my code helps appreciated.
CODE:
member.kick().then((member) => {
message.channel.send(`:wave: ${member.displayName} has been kicked`);
}).catch(() => {
if (!message.member.hasPermission(['KICK_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannot kick members");
} else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannont kick this member");
}
})
}
if (message.content.startsWith(`${prefix}ban`)) {
let member = message.mentions.members.first();
member.ban().then((member) => {
message.channel.send(`:wave: ${member.displayName} has been kicked`);
}).catch(() => {
if (!message.member.hasPermission(['BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannot ban members");
} else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannont ban this member");
}
})
}
The reason why the code is throwing an error is because you can't check for permissions in a .catch block because catch blocks handle errors thrown by code. Instead, you should check for permissions before the command is executed. Additionally, you are checking that the user has all three permissions. You can use the JavaScript OR (||) to check that they have any of the permissions and the Administrator permission has ALL PERMISSIONS, eliminating the need to add it to the list. I've rewritten the code below
// You never need to check for ADMINISTRATOR, because it has all permissions
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply("you cannot kick members");
} else if (member.hasPermission('KICK_MEMBERS' || 'BAN_MEMBERS')) {
message.reply("you cannot kick this member");
}
member.kick()
.then(member => message.channel.send(`:wave: ${member.displayName} has been kicked`))
.catch(e => console.log(e));
}
if (message.content.startsWith(`${prefix}ban`)) {
let member = message.mentions.members.first();
if (!message.member.hasPermission('BAN_MEMBERS')) {
message.reply("You cannot ban members")
} else if (member.hasPermission('KICK_MEMBERS' || 'BAN_MEMBERS')) {
message.reply("You cannont ban this member")
}
member.ban()
.then(member => message.channel.send(`:wave: ${member.displayName} has been banned`)
.catch(e => console.log(e));
}

How to add member to role without having to mention it? [Discord.JS]

at the moment I am trying to make a role command for my bot. For example: t!role add #person <name of role>. But I can't figure out how to get the role as people have usernames with different lengths so I cannot use .slice which I have been able to use for creating a role
Currently my bot says that it can't find that role
My code:
const config = require("../../config.json");
module.exports = {
name: "role",
description: "Creates/removes roles from server and adds/removes roles from members",
category: "utility",
run: async (client, message, args) => {
const mentionedMember = message.mentions.members.first();
let messageRole = message.content.slice(config.prefix.length + 12).trim();
const findRole = message.guild.roles.cache.find(r => r.name === messageRole);
if (!message.member.permissions.has("MANAGE_ROLES")) return message.channel.send("You do not have permission to use this command");
if (args[0].toLowerCase() == "create") {
let roleName = message.content.slice(config.prefix.length + 12);
if (!roleName) return message.channel.send("You need to specify a name for the role");
let newRole = await message.guild.roles.create({ data: { name: roleName, permissions: false } });
message.channel.send(`**${roleName}** has been created`);
} else if (args[0].toLowerCase() === "delete") {
if (!findRole) return message.channel.send("You need to specify a role to be deleted\n\n" + messageRole);
findRole.delete();
message.channel.send(`${findRole.name} has been deleted`);
} else if (args[0].toLowerCase() === "add") {
if (!args[1]) return message.channel.send("You need to mention the member you want to give the role to");
if (!mentionedMember) return message.channel.send("I can't find that member");
if (!args[2]) return message.channel.send("You need to specify a role I have to give");
if (!findRole) return message.channel.send("I can't find that role");
if (mentionedMember.roles.cache.has(findRole)) return message.channel.send(`${mentionedMember.name} already has that role`);
mentionedMember.addRole(findRole);
message.channel.send(`${mentionedMember.name} has been given the role ${findRole.name}`)
return undefined
} else if (args[0].toLowerCase() === "remove") {
if (!args[1]) return message.channel.send("You need to mention the member you want to remove the role from");
if (!mentionedMember) return message.channel.send("I can't find that member");
if (!args[2]) return message.channel.send("You need to specify a role I have to remove");
if (!mentionedRole) return message.channel.send("I can't find that role");
if (!mentionedMember.roles.cache.has(mentionedRole.id)) return message.channel.send(`${mentionedMember} doesnt have that role`);
mentionedMember.roles.remove(mentionedRole.id);
message.channel.send(`**${mentionedRole.name}** has been removed from ${mentionedMember}`);
return undefined
}
}
}
Slice the amount of args. If you slice an array, it will remove one entire value from it.
[1, 2, 3].slice(2)
// result: [3]
Since args is an array, all you have to do is args.slice(2), assuming args does not include the command message.

Discord.js v12 Role add to mentioned user problem

so I am trying to make a command like $roletested #user which should give the user mentioned the specific role. I get an error called:_ "Cannot read property 'roles' of undefined... Help me out please, here's my code :D
if (message.content.startsWith(prefix + "roletested")) {
let testedUser = message.mentions.members.first()
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌');
declineMsg.delete({timeout: 5000});
let testedRole = message.guild.roles.cache.find(role => role.id === "724676382776492113");
testedUser.roles.add(testedRole);
message.channel.send("Added the role `TESTED` to user.")
})}})
Your code but fixed
if (message.content.startsWith(prefix + "roletested")) {
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
let testedRole = message.guild.roles.cache.get('724676382776492113');
let testedUser = message.mentions.members.first();
if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌')
declineMsg.delete({timeout: 5000});
});
testedUser.roles.add(testedRole);
message.channel.send("Added the role `TESTED` to user.")
}
I fixed your code :
I put in order something (the code is the same, I only changed something)
You can't use message.guild.roles.cache.find(...) so I changed it to message.guild.role.cache.get('Role ID')
Source : Link
Code :
if (message.content.startsWith(prefix + 'roletested')) {
const testedUser = message.mentions.members.first();
if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
if (!testedUser) {
message.channel.send('You have to mention the person you want to assign the role to!').then(declineMsg => {
message.react('❌');
declineMsg.delete({ timeout: 5000 });
return;
});
}
const testedRole = message.guild.roles.cache.get('395671618912780289');
testedUser.roles.add(testedRole);
message.channel.send('Added the role `TESTED` to user.');
}
I tested this code on my bot and it worked as expected.

How can I unban every member from a guild

i was wondering if someone can share a code with me or message me on discord to help me --> Maniac#3833
i tried this code below.. and it failed.
if (msg.startsWith(prefix + 'unbanall')) {
if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send('You don\'t have permissions to use this command')
message.guild.fetchBans().then(bans => {
bans.forEach(member => {
message.guild.members.unban(member);
message.channel.send(`Unbanned **${bans.size}** users`)
})
})
}
The fetchBans() method returns a collection of BanInfo. So you need to do it this way:
if (msg.startsWith(prefix + 'unbanall')) {
if (!message.member.hasPermission('BAN_MEMBERS')) return message.channel.send('You don\'t have permissions to use this command')
message.guild.fetchBans().then(bans => {
bans.forEach(banInfo => {
message.guild.members.unban(banInfo.user);
});
message.channel.send(`Unbanned **${bans.size}** users`)
})
}
Also, the message.channel.send() won't wait the forEach, so it will send it before all the members are banned.

How to iterate through an enmap?

I'm trying to iterate through an enmap for my discord.js bot, I've managed to set and get values from a single entry but I'm trying to set up a command that adds people to a newsletter like DM about minor major updates.
if (args[0] === 'minor') {
if (devlog.updates === 'minor') return message.channel.send('You are already recieving minor updates.').then(m => m.delete(5000))
await client.devlog.set(userID, "yes", 'subscribed');
await client.devlog.set(userID, "minor", 'updates');
return message.channel.send('You will now recieve minor and major updates.').then(m => m.delete(5000))
}
if (args[0] === 'major') {
if (devlog.updates === 'major') return message.channel.send('You are already recieving major updates.').then(m => m.delete(5000))
await client.devlog.set(userID, "yes", 'subscribed');
await client.devlog.set(userID, "major", 'updates');
return message.channel.send('You will now recieve only major updates.').then(m => m.delete(5000))
}
if (!args[0]) {
if (devlog.subscribed === 'yes') {
await client.devlog.set(userID, "no", 'subscribed');
await client.devlog.set(userID, "none", 'updates');
return message.channel.send('You will stop recieving updates about RoboTurtle all together').then(m => m.delete(5000))
}
if (devlog.subscribed === 'no') {
return message.channel.send(`Please choose wether you\'d like to recieve minor or major updates! (minor has both) **devlog minor/major**`).then(m => m.delete(10000))
}
}
It kind of works but it won't trigger the message if they already are subscribed to the same type of update and if they do just !devlog it's meant to either set them to not receive updates if they already are, or tell them to choose between the two if they aren't, however it just sends the last message either way.
I tried setting up my enmap iteration for DMing all subscribed people with a for...of function based off the .map related docs (since they're meant to be just "fancier" maps) but to no avail, since they don't really show discord style use cases.
if (args[0] === 'minor') {
for (let entry of client.devlog) {
if (entry.updates === 'minor') {
let user = client.users.get(entry)
user.send(`**[Minor Update]\n${args.slice(1)}`)
}
}
}
if (args[0] === 'major') {
for (let entry of client.devlog) {
if (entry.subscribed === 'yes') {
let user = client.users.get(entry)
user.send(`**[Major Update!]\n${args.slice(1)}`)
}
}
}
In case anyone wanted to look at the full code to get a better idea of what Im trying to do here ya go: https://pastebin.com/bCML6EQ5
Since they are just an extension of the normal Map class, I would iterate through them with Map.forEach():
let yourEnmap = new Enmap();
yourEnmap.set('user_id', 'your_values');
yourEnmap.forEach((value, key, map) => {
...
});
In your case it would be something like:
client.devlog.forEach((values, user_id) => {
// you can check your subscription here, the use the user_id to send the DM
client.users.get(user_id).send("Your message.");
});

Resources