如何使用discord.js v13.4锁定和解锁频道



所以我尝试了很多方法将通道锁定到特定角色,这是我的尝试代码之一:

锁定通道

const Command = require("../structures/Command.js");
const { Permissions } = require('discord.js');
module.exports = new Command({
name: "lock",
description: "lock the channel that the command is executed",
async run(message, args, client) {
if (message.author.bot) return;
if (message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)){
let role = message.guild.roles.cache(r => r.id === "895151537511362600");
role.overwritePermissions(UserResolvable, {
VIEW_CHANNEL: true,
SEND_MESSAGES: false,
READ_MESSAGE_HISTORY: true,
ATTACH_FILES: false
});            
} else message.reply(`why do you want to use a mod command when you're not a mod`)
}
});

解锁:

const Command = require("../structures/Command.js");
const { Permissions } = require('discord.js');
module.exports = new Command({
name: "unlock",
description: "unlock the channel that the command is executed",
async run(message, args, client) {
if (message.author.bot) return;
if (message.member.permissions.has(Permissions.FLAGS.ADMINISTRATOR)){
let role = message.guild.roles.cache(r => r.id === "895151537511362600");
role.overwritePermissions(UserResolvable, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
READ_MESSAGE_HISTORY: true,
ATTACH_FILES: true
});            
} else message.reply(`why do you want to use a mod command when you're not a mod`)
}
});

它不起作用,还返回了一个错误,比如UserResolvable is not defined我也尝试过这种方法,但它仍然不起作用

根据使用channel.permissionOverwrites.edit()的文档

所以你的锁定代码看起来像这样:

message.channel.permissionOverwrites.edit(message.guild.everyone.id, {
VIEW_CHANNEL: false,
SEND_MESSAGES: false,
READ_MESSAGE_HISTORY: false,
ATTACH_FILES: false
});
message.channel.permissionOverwrites.edit("ROLE_ID", {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
READ_MESSAGE_HISTORY: true,
ATTACH_FILES: true
});

然后,对于解锁命令,只需更改权限值。

要像这样锁定使用

channel.permissionOverwrites.edit(channel.guild.roles.everyone, {
SEND_MESSAGES: false
}).catch((e) => { console.error(e) })

最新更新