你好,我试图在discord.js中创建一个禁止所有禁止用户的命令,但是当我执行ub all命令时,它说全部禁止但没有禁止任何人,这是到目前为止的代码
bot.on('message', message =>{
const member = message.member;
switch(message.content.toLowerCase()){
case (PREFIX + "ub all"):
if(member.hasPermission('ADMINISTRATOR')){
async function ubAll(){
const users = await message.guild.fetchBans()
for (const user of users.array()) {
await message.guild.unban(user)
}
}
message.reply("Unbanned all users from the server.")
}
if(!member.hasPermission('ADMINISTRATOR')){
message.reply("You do not have enough permissions for this command!")
}
}
})
答案 0 :(得分:1)
unban
不是guild
的方法。您需要使用guild.members.unban()
。
bot.on("message", message => {
switch(message.content.toLowerCase()) {
case (PREFIX + "ub all"):
if (message.member.hasPermission("ADMINISTRATOR")) {
message.guild.fetchBans().then(bans => {
if (bans.size == 0) {message.reply("There are no banned users."); throw "No members to unban."};
bans.forEach(ban => {
message.guild.members.unban(ban.user.id);
});
}).then(() => message.reply("Unbanned all users.")).catch(e => console.log(e))
} else {message.reply("You do not have enough permissions for this command.")}
break;
}
});
答案 1 :(得分:0)
bot.on('message', message =>{
const member = message.member;
switch(message.content.toLowerCase()){
case (PREFIX + "ub all"):
if(member.hasPermission('ADMINISTRATOR')){
message.guild.fetchBans().forEach((fB)=>{
message.guild.members.unban(fB.user.id);
})
// All Users get unbanned
} else {
// User does not have permission.
}
}
})
我强烈建议您看一下Discord.JS v12文档。 https://discord.js.org/#/docs/main/stable/general/welcome
答案 2 :(得分:-1)
您在声明ubAll
函数,但实际上并未调用它。
您也可以用if
替换最后一个else
。
bot.on("message", async (message) => {
const member = message.member;
switch (message.content.toLowerCase()) {
case PREFIX + "ub all":
if (member.hasPermission("ADMINISTRATOR")) {
const users = await message.guild.fetchBans();
for (const user of users.array()) {
await message.guild.unban(user);
}
message.reply("Unbanned all users from the server.");
} else
message.reply("You do not have enough permissions for this command!");
}
});