【问题标题】:Discord bot how to create a private text channelDiscord bot如何创建私人文本频道
【发布时间】:2019-12-11 19:49:01
【问题描述】:

我正在使用 node.js 创建一个不和谐机器人,我希望它在服务器上创建一个私有文本通道,并将发送命令“!create”的用户和机器人本身添加到其中。

我找到了一种使用此答案制作文本频道的方法:How to create a text channel 但我找不到将其设为私有并添加人员的方法。

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    感谢@gilles-heinesch 的领导。 The APIdiscord.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]},
      ]);
    }
    

    【讨论】:

      【解决方案2】:

      https://discord.js.org/#/docs/main/stable/class/ChannelManager

      使用缓存集合

      const channels = message.guild.channels.cache
      const myChannel = channels.find(channel => channel.name === 'channel name')
      
      

      【讨论】:

        【解决方案3】:

        我总是这样:

        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()的方法覆盖新创建的公会文本通道的权限。在那里,我们授予消息作者和机器人查看频道的权限,并撤销每个人角色查看此频道的权限。

        【讨论】:

        • 当我尝试运行它时,它在第一行显示错误:TypeError: Cannot read property 'roles' of undefined
        • 您必须在“服务器 ID”@BenedictDhm 中输入您的服务器 ID
        • client.guilds.get is not a function
        猜你喜欢
        • 2018-07-27
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 2020-03-12
        • 2019-05-05
        • 2019-03-25
        • 2019-10-08
        • 2019-04-27
        相关资源
        最近更新 更多