如何在不必提及的情况下将成员添加到角色中?[Discord.JS]
目前,我正试图为我的机器人制作一个角色命令。例如:如何在不必提及的情况下将成员添加到角色中?[Discord.JS],discord.js,Discord.js,目前,我正试图为我的机器人制作一个角色命令。例如:t!角色添加@person。但是我不知道如何获得角色,因为人们有不同长度的用户名,所以我无法使用.slice创建角色 目前我的机器人说它找不到那个角色 我的代码: const config = require("../../config.json"); module.exports = { name: "role", description: "Creates/removes r
t!角色添加@person
。但是我不知道如何获得角色,因为人们有不同长度的用户名,所以我无法使用.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
}
}
}
切片
参数的数量
。如果对数组进行切片,它将从中删除一个整数值
[1,2,3]。切片(2)
//结果:[3]
由于
args
是一个数组,您所要做的就是args.slice(2)
,假设args
不包含命令消息。您好,谢谢您的回复!我已经尝试过了,但是当我尝试args.slice(2)
或者对于这个问题,机器人回答说:“add@Dennis a”不是一个角色,虽然args.shift似乎有帮助,但我还没有找到如何转换2个参数的方法。为什么不使用message.indications.roles
?