【发布时间】:2019-12-11 19:49:01
【问题描述】:
我正在使用 node.js 创建一个不和谐机器人,我希望它在服务器上创建一个私有文本通道,并将发送命令“!create”的用户和机器人本身添加到其中。
我找到了一种使用此答案制作文本频道的方法:How to create a text channel 但我找不到将其设为私有并添加人员的方法。
【问题讨论】:
标签: node.js discord discord.js
我正在使用 node.js 创建一个不和谐机器人,我希望它在服务器上创建一个私有文本通道,并将发送命令“!create”的用户和机器人本身添加到其中。
我找到了一种使用此答案制作文本频道的方法:How to create a text channel 但我找不到将其设为私有并添加人员的方法。
【问题讨论】:
标签: node.js discord discord.js
感谢@gilles-heinesch 的领导。 The API 的 discord.js 随着时间的推移发生了巨大的变化,所以这里是一个更新的版本:
const { Client, Permissions } = require('discord.js');
/** @param {string|number} serverId - a "snowflake" ID you can see in address bar */
async function createPrivateChannel(serverId, channelName) {
const guild = await client.guilds.fetch(serverId);
const everyoneRole = guild.roles.everyone;
const channel = await guild.channels.create(channelName, 'text');
await channel.overwritePermissions([
{type: 'member', id: message.author.id, allow: [Permissions.FLAGS.VIEW_CHANNEL]},
{type: 'member', id: client.user.id, allow: [Permissions.FLAGS.VIEW_CHANNEL]},
{type: 'role', id: everyoneRole.id, deny: [Permissions.FLAGS.VIEW_CHANNEL]},
]);
}
【讨论】:
https://discord.js.org/#/docs/main/stable/class/ChannelManager
使用缓存集合
const channels = message.guild.channels.cache
const myChannel = channels.find(channel => channel.name === 'channel name')
【讨论】:
我总是这样:
const everyoneRole = client.guilds.get('SERVER ID').roles.find('name', '@everyone');
const name = message.author.username;
message.guild.createChannel(name, 'text')
.then(r => {
r.overwritePermissions(message.author.id, { VIEW_CHANNEL: true });
r.overwritePermissions(client.id, { VIEW_CHANNEL: true });
r.overwritePermissions(everyoneRole, { VIEW_CHANNEL: false });
})
.catch(console.error);
首先,我们定义了everyoneRole。然后我们使用overwritePermissions()的方法覆盖新创建的公会文本通道的权限。在那里,我们授予消息作者和机器人查看频道的权限,并撤销每个人角色查看此频道的权限。
【讨论】:
client.guilds.get is not a function