discord.js 简单的踢用户命令
discord.js simple kick users command
我最近使用参数为我的机器人发出了踢命令。我不想使用 message.mentions.users.first()
,因为如果用 !kick
回复 his/her 消息,那会踢用户。我在下面给出了我的代码。即使我已经提到了一个用户,该机器人只要求我提及一个用户。
module.exports = {
name: 'kick',
description: 'kicks a user',
async execute(message, args){
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply('Insufficient Permissions')
return;
}
if (!message.guild.me.hasPermission('KICK_MEMBERS')) {
message.reply('I need the `KICK_MEMBERS` permission to execute this!')
return;
}
const user = message.guild.members.cache.get(args[1]);
if (!user) {
message.reply('Mention the uer to kick!')
return;
}
if (message.guild.members.cache.get(args[1]) == undefined) {
message.reply('Couldn\'t find the user')
return;
}
const reason = args.slice(2).join(' ')
await user.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
.catch(err => {
console.log(err)
});
await user.kick(reason);
await message.channel.send(`**${message.author.username}** kicked **${user.username}**! Reason : ${reason}`)
}
}
因为.get()需要ID作为参数
第一种方法
用这个替换你的用户常量
const member = message.mentions.members.first() || message.guild.members.cache.get(args[1])
这样你就可以同时支持 ID 和提及
第二种方法
使用正则表达式
const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {});
此正则表达式从字符串中删除所有非数字字符,在不和谐的提及中,这样组成 <@USERID>,正则表达式删除 <@> 和 returns 用户 ID,然后将其传递到 .fetch() 和 returns你要踢的成员
这将是正确的代码 iirc
module.exports = {
name: 'kick',
description: 'kicks a user',
async execute(message, args){
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply('Insufficient Permissions')
return;
}
if (!message.guild.me.hasPermission('KICK_MEMBERS')) {
message.reply('I need the `KICK_MEMBERS` permission to execute this!')
return;
}
if (!args[1) return message.reply('provide a member for me to kick');
const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {});
if (!member) return message.reply('Member not found :(')
const reason = args.slice(2).join(' ')
await member.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
.catch(err => {
console.log(err)
});
await member.kick(reason);
await message.channel.send(`**${message.author.username}** kicked **${member.user.username}**! Reason : ${reason}`)
}
}
我最近使用参数为我的机器人发出了踢命令。我不想使用 message.mentions.users.first()
,因为如果用 !kick
回复 his/her 消息,那会踢用户。我在下面给出了我的代码。即使我已经提到了一个用户,该机器人只要求我提及一个用户。
module.exports = {
name: 'kick',
description: 'kicks a user',
async execute(message, args){
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply('Insufficient Permissions')
return;
}
if (!message.guild.me.hasPermission('KICK_MEMBERS')) {
message.reply('I need the `KICK_MEMBERS` permission to execute this!')
return;
}
const user = message.guild.members.cache.get(args[1]);
if (!user) {
message.reply('Mention the uer to kick!')
return;
}
if (message.guild.members.cache.get(args[1]) == undefined) {
message.reply('Couldn\'t find the user')
return;
}
const reason = args.slice(2).join(' ')
await user.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
.catch(err => {
console.log(err)
});
await user.kick(reason);
await message.channel.send(`**${message.author.username}** kicked **${user.username}**! Reason : ${reason}`)
}
}
因为.get()需要ID作为参数
第一种方法
用这个替换你的用户常量
const member = message.mentions.members.first() || message.guild.members.cache.get(args[1])
这样你就可以同时支持 ID 和提及
第二种方法
使用正则表达式
const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {});
此正则表达式从字符串中删除所有非数字字符,在不和谐的提及中,这样组成 <@USERID>,正则表达式删除 <@> 和 returns 用户 ID,然后将其传递到 .fetch() 和 returns你要踢的成员
这将是正确的代码 iirc
module.exports = {
name: 'kick',
description: 'kicks a user',
async execute(message, args){
if (!message.member.hasPermission('KICK_MEMBERS')) {
message.reply('Insufficient Permissions')
return;
}
if (!message.guild.me.hasPermission('KICK_MEMBERS')) {
message.reply('I need the `KICK_MEMBERS` permission to execute this!')
return;
}
if (!args[1) return message.reply('provide a member for me to kick');
const member = await message.guild.members.fetch(args[1].replace(/\D/g,'')).catch(() => {});
if (!member) return message.reply('Member not found :(')
const reason = args.slice(2).join(' ')
await member.send(`You have been kicked from **${message.guild.name}**. Reason : ${reason}`)
.catch(err => {
console.log(err)
});
await member.kick(reason);
await message.channel.send(`**${message.author.username}** kicked **${member.user.username}**! Reason : ${reason}`)
}
}