Discord.js 禁止/踢命令可供所有用户使用。我怎样才能解决这个问题?
Posted
技术标签:
【中文标题】Discord.js 禁止/踢命令可供所有用户使用。我怎样才能解决这个问题?【英文标题】:Discord.js ban/kick commands available to all users. How can I fix this? 【发布时间】:2019-07-20 09:49:59 【问题描述】:我正在制作自己的 Discord Bot,因为我不相信更大的(Dyno、Hime、NosoBot 等)而且我的机器人已经完成了。唯一的问题是我的代码允许所有成员使用这些命令。我只希望人们能够使用他们有权使用的功能。该代码有效,但我怎样才能让它只允许有权踢/禁止的人?
if (msg.content.startsWith("$kick "))
if (msg.mentions.members.first())
msg.mentions.members.first.kick().then((member) =>
msg.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
).catch(() =>
msg.channel.send("I do not have permissions to do this");
);
else if (msg.content.startsWith("$ban "))
if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
if (msg.mentions.members.first())
msg.mentions.members.first.ban().then((member) =>
msg.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
).catch(() =>
msg.channel.send("I do not have permissions to do this");
);
【问题讨论】:
【参考方案1】:在服务器中创建一个角色,并在您的代码中检查该角色
例如
if(!message.member.roles.find("name", "Admin"))
return;
如果用户没有角色,将退出该方法
所以代码看起来像这样
if (message.content.startsWith("$kick"))
if (!message.member.roles.find("name", "Admin"))
return;
// Easy way to get member object though mentions.
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("Access Denied");
);
);
client.on("message", (message) =>
if (message.content.startsWith("$ban"))
if (!message.member.roles.find("name", "Admin"))
return;
// Easy way to get member object though mentions.
var member = message.mentions.members.first();
// ban
member.ban().then((member) =>
// Successmessage
message.channel.send(":wave: " + member.displayName + " has been successfully banned https://gfycat.com/playfulfittingcaribou :point_right: ");
).catch(() =>
// Failmessage
message.channel.send("Access Denied");
);
);
一个更好的方法是创建一个函数来返回用户是否有权限并在你的代码中使用它
【讨论】:
我一直看到这个答案,我会把它放在哪里? 非常感谢,我制作了两个机器人,都遇到了这个问题!【参考方案2】:“KICK_MEMBERS”权限告诉您他们是否有权踢成员,因此得名。
“BAN_MEMBERS”权限告诉您他们是否有权禁止成员,因此得名。
你的踢腿命令:
if (msg.member.hasPermission("KICK_MEMBERS"))
if (msg.members.mentions.first())
try
msg.members.mentions.first().kick();
catch
msg.reply("I do not have permissions to kick " + msg.members.mentions.first());
else
msg.reply("You do not have permissions to kick " + msg.members.mentions.first());
你的禁令命令:
if (msg.member.hasPermission("BAN_MEMBERS"))
if (msg.members.mentions.first())
try
msg.members.mentions.first().ban();
catch
msg.reply("I do not have permissions to ban" + msg.members.mentions.first());
else
msg.reply("You do not have permissions to ban" + msg.members.mentions.first());
try
和 catch
的原因确保如果机器人没有权限踢或禁止该用户,它不会导致错误。
另一个说明:
您不必创建另一个bot.on('message')
事件。相反,只需使用elseif
【讨论】:
这个答案来自几年前......但是我现在注意到我的 msg 对象没有“members”属性......这样做的新方法是什么? 【参考方案3】:如果 (message.content.startsWith($prefixkick
))
let member = message.mentions.members.first();
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(`$prefixban`))
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");
)
【讨论】:
【参考方案4】:我将它用于角色权限:
if(message.member.roles.find(r => r.name === 'name of role'))
//Enter command coding here
else
return message.channel.send('Enter Speech here')
break;
也许这对你有帮助,我不知道。
【讨论】:
【参考方案5】:嘿,我有更好的东西给你
const Discord = require('discord.js');
if (message.content.startsWith($prefixBAN)) =>
if (!message.guild.member(message.author).hasPermission('BAN_MEMBERS')) return message.channel.send('You do not have the permission for ban users" !');
if (!message.guild.member(client.user).hasPermission('BAN_MEMBERS')) return message.channel.send('I don\'t have the permission for ban users" !');
if (message.mentions.users.size === 0) return message.channel.send('You need to ping a user !');
let banMember = message.guild.member(message.mentions.users.first());
if (!banMember) return message.channel.send('User not found!');
banMember.ban().then((member) =>
message.channel.send(member.displayName + " has left the server")
message.channel.send(member.displayName + " has been successfully banned by " + message.author);
)
只需删除 BAN 并替换为 KICK
【讨论】:
【参考方案6】: if (message.content.startWith(`$prefixban`))
let buser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!buser) return message.channel.send("Please provid a user to ban")
let reason = args.join(" ").slice(22)
if (!reason) return message.channel.send("Please provide a reason")
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("You don't have acces to this command")
if (buser.hasPermission("BAN_MEMBERS")) return message.channel.send("This user has the perm ban user")
let embed = new Discord.RichEmbed()
buser.ban()
.setAuthor("Ban")
.setDescription(`The user $buser has been ban for: $reason\n by the admin $message.author.tag`)
bot.channels.get("an channel id").send(embed);
【讨论】:
除了您提供的答案之外,请考虑提供一个简要说明,说明为什么以及如何解决此问题。【参考方案7】:if (msg.content.startsWith("$kick "))
if (msg.author.hasPermission(`BANNING_MEMBERS`)
if (msg.mentions.members.first())
msg.mentions.members.first.kick().then((member) =>
msg.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
).catch(() =>
msg.channel.send("I do not have permissions to do this");
);
else if (msg.content.startsWith("$ban "))
if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
if (msg.mentions.members.first())
msg.mentions.members.first.ban().then((member) =>
msg.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
).catch(() =>
msg.channel.send("I do not have permissions to do this");
);
else
msg.reply(`you can't ban members ask owner for admin`)
;
我建议您使用 prettierc(如果您在 vs 代码中编码,例如内部人员)
【讨论】:
以上是关于Discord.js 禁止/踢命令可供所有用户使用。我怎样才能解决这个问题?的主要内容,如果未能解决你的问题,请参考以下文章
Discord.js 当我踢一个人时我想在频道中看到它+作为嵌入消息