我正在尝试提供踢踢功能,如果AdminRole输入/ kick @user会踢他们,但是如果MemberRole中有人尝试说/ kick @user则说对不起,您没有使用此权限!
client.on("message", (message) =>
{
if (message.content.startsWith("/kick"))
//checking if user is an admin role
if (message.member.roles.find('AdminRole'))
{
var member= message.mentions.members.first();
// Kick
member.kick().then((member) => {
// Successmessage
message.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
}).catch(() => {
// Failmessage
message.channel.send("Sorry, command failed");
});
}
if (message.member.roles.find('MemberRole'))
{
message.reply("Sorry, you don't have permissions to use this!");
}
});
这是它吐出的错误,请记住,只有当有人键入/ kick命令而不管角色时,它才会吐出此错误
如果(typeof value ==='undefined')抛出新错误('Value必须被指定。'); ^
错误:必须指定值。 在Map.find(/Users/MainUser/Desktop/bot/node_modules/discord.js/src/util/Collection.js:499:45) 在Client.client.on(/Users/MainUser/Desktop/bot/index.js:12:34) 在Client.emit(events.js:198:13) 在MessageCreateHandler.handle(/Users/MainUser/Desktop/bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34) 在WebSocketPacketManager.handle(/Users/MainUser/Desktop/bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65) 在WebSocketConnection.onPacket(/Users/MainUser/Desktop/bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35) 在WebSocketConnection.onMessage(/Users/MainUser/Desktop/bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17) 在WebSocket.onMessage(/Users/MainUser/Desktop/bot/node_modules/ws/lib/event-target.js:120:16) 在WebSocket.emit(events.js:198:13) 在Receiver.receiverOnMessage(/Users/MainUser/Desktop/bot/node_modules/ws/lib/websocket.js:789:20)
答案 0 :(得分:0)
您的错误是由于您当前使用Collection.find()
而引起的。
不正确:Collection.find('property')
不推荐使用:Collection.find('property', 'value')
正确:Collection.find(element => element.property === value)
const prefix = '/';
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const [cmd, ...args] = message.content.trim().slice(prefix.length).split(/\s+/g);
switch(cmd.toLowerCase()) {
case 'kick':
try {
if (!message.guild) return await message.channel.send(':x: Guilds only.');
if (!message.member.roles.find(r => r.name === 'Admin')) return await message.channel.send(':x: No permission.');
if (!args[1] || !message.mentions.members.first()) return await message.channel.send(':x: No member provided.');
const toKick = message.mentions.members.first();
const reason = args.slice(2).join(' ') || 'No Reason';
if (!toKick.kickable || toKick.roles.find(r => r.name === 'Admin')) return await message.channel.send(':x: No permission.');
await toKick.kick(reason);
await message.channel.send(`:wave: ${toKick.user.displayName} has been kicked.`);
} catch(err) {
console.error(err);
}
break;
}
});