【发布时间】:2021-06-29 21:55:00
【问题描述】:
我有一个机器人,它应该在特定类别下创建一个频道,然后将两个用户添加到该频道。
以下代码“假定”可以工作并添加用户 2,但由于 DiscordAPIError: Missing Permissions 而失败。 我不知道这是实际所需的权限?
function addUserToChannel(ch, user, altCh) {
console.log(user.username,"sendmsg",ch.permissionsFor(client.user).has("MANAGE_CHANNELS", false)); //returns true
if (!ch.permissionsFor(user).has("SEND_MESSAGES", false)) {
console.log("User", user.username, "lacks view-channel permission");
ch.updateOverwrite(user, { //fails here obviously
SEND_MESSAGES: true,
VIEW_CHANNEL: true
});
} else {
console.log("WARN:", user.username, " already in channel ", ch.name);
altCh.send(user.toString() + " join conversation here: " + ch.toString());
}
}
使用以下代码创建频道:
function createPrivateChannel(guild, convo, user) {
let everyoneRole = guild.roles.cache.find(r => r.name === "@everyone");
let parent = guild.channels.cache.find(ch => {
//console.log(ch.id,secret.convoCat.id);
return ch.id == secret.convoCat.id;
});
return guild.channels.create(convo.chName, {
type: "text",
parent: parent,
permissionOverwrites: [
{
id: everyoneRole.id, //private channel
deny: ["VIEW_CHANNEL", "SEND_MESSAGES"]
},
{
id: client.user.id, //bot permissions
allow: [ "VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY", "MANAGE_CHANNELS" ]
},
{
id: user.user.id, //a different user
allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY"]
}
]
});
}
【问题讨论】:
-
我添加了一个catch处理程序来处理错误消息,发现有一个错误代码:50013,如果有帮助的话
标签: discord discord.js