此刻,我正尝试为我的机器人设置角色命令。例如:t!role add @person <name of role>
。但是我无法弄清楚如何获得角色,因为人们使用的用户名长度不同,因此我无法使用.slice
来创建角色。
当前我的机器人说找不到该角色
我的代码:
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
}
}
}
答案 0 :(得分:-1)
切片args
的数量。如果您对数组进行切片,它将从其中删除一个完整的值。
[1, 2, 3].slice(2)
// result: [3]
由于args
是一个数组,因此假设args.slice(2)
不包含命令消息,您只需要做args
。