首页 > 解决方案 > 如何在 Discord.Js 中创建用户特定频道?

问题描述

如何在 Discord.Js 中创建用户特定频道?

所以我正在制作一个 Discord 机器人,你点击一个反应,它就会把你带到一个私人频道,其他人都看不到这个频道

这是我到目前为止所拥有的:

const Discord = require('discord.js');
const client = new Discord.Client();
const { bot } = require('./config.json');
const request = require('request');

client.on('message', message => {
    var command = message.content.replace('t#', '');
    var command = command.replace('t# ', '')
    if(command.startsWith('post') === true){
        message.delete();
        var postEmbed = new Discord.RichEmbed();
        postEmbed.setTitle('Twotter Post')
        postEmbed.setAuthor(message.author.tag, message.author.avatarURL)
        postEmbed.setDescription(command.replace('post ', ''))
        postEmbed.setFooter('Created by Happy Fone on YouTube')
        this.message = message;
        message.channel.send(postEmbed).then(message => {
            message.react('')
            message.react('')
            message.react('')
            this.messageId = message.id;
        });
    }
});

client.on('messageReactionAdd', (messageReaction, user) => {
    if(user.bot)return;
    const { message, emoji } = messageReaction;
    if(emoji.name == "") {
        if(message.id == this.messageId) {
            makeChannel(this.message)
        }
    }
});

function makeChannel(message){
    var server = message.guild;
    var name = message.author.username;

    server.createChannel(name, "text");
}

client.login(bot.token)

我试图尽可能具体地表达我想要的东西。如果您需要更多信息,请说。

标签: javascriptdiscorddiscord.js

解决方案


Since you need the user that reacted to the message in makeChannel(), you'll have to add a parameter for it. You don't actually need the relevant message in your function, so you can substitute that parameter for a Guild (which you do need).

function makeChannel(guild, user) {
  const name = user.username;
  ...
}
// within messageReactionAdd listener

makeChannel(message.guild, user);

When creating the channel, you can pass in a ChannelData object containing permissions (PermissionResolvables) for it. By doing so, you can deny everyone (except for members with the Administrator permission) access to the channel except the user.

// within makeChannel()

guild.createChannel(name, {
  type: 'text',
  permissionOverwrites: [
    {
      id: guild.id, // shortcut for @everyone role ID
      deny: 'VIEW_CHANNEL'
    },
    {
      id: user.id,
      allow: 'VIEW_CHANNEL'
    }
  ]
})
  .then(channel => console.log(`${name} now has their own channel (ID: ${channel.id})`))
  .catch(console.error);

Refer to the examples in the Discord.js documentation for Guild#createChannel().


推荐阅读