Code Disease
Code Disease

Reputation: 64

How can I make a user specific channel in Discord.Js?

How can I make a user specific channel in Discord.Js?

So I am making a Discord bot where you click a reaction and it takes you into a private channel and nobody else can see the channel

This is what I have so far:

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)

I am trying to be as specific as possible for what I want. If you need any more info please say.

Upvotes: 0

Views: 4854

Answers (2)

Rokkkit
Rokkkit

Reputation: 40

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

  server.createChannel(name, `${username}`); 
  var channel = server.find("name", `${username}`)
  channel.overwritePermissions(message.author.id,{'VIEW_CHANNEL':true,'VIEW_CHANNEL':true}))
} 

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

client.login(bot.token)

This piece of code is not yet tested, I'll do this later.

Upvotes: -1

slothiful
slothiful

Reputation: 5623

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().

Upvotes: 3

Related Questions