Silent Watcher
Silent Watcher

Reputation: 125

Bot doesn't add message member to the channel - Discord.JS

So, I'm currently working on a "temporary channels" module for my bot. When a user with a certain rank does !newvc, the bot creates a private voice channel that they can use, add people, and when everyone leaves, it gets automatically deleted after some time.

Everything was working fine, but I've noticed a bug that I can't find the reason behind why it happens. Basically, when you first use the command all works fine, the channel is made, you get added and it's moved to the category. But if you use it again, let's say a minute later you don't get added. The channel is made, set as private but you message.member doesn't get added. Then it again does and doesn't, You get the point right?

I honestly can't find a reason why it does it and the only thing I can think of is something to do with Discord's API.

Here's my code

        let member = message.member
        user = member.user

            message.delete()
            message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{

                id: message.guild.id,
                deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']

            }]).then(channel => {

                channel.overwritePermissions(member, {
                    CONNECT: true,
                    USE_VAD: true,
                    PRIORITY_SPEAKER: true
                })

                channel.setParent('567718414454358026')

            })

                let privatevc = new Discord.RichEmbed()
                .setDescription(':white_check_mark: Successfully created a voice channel!')
                .setColor(config.green)

                message.channel.send({ embed: privatevc }).then(msg => msg.delete(10000))

FYI: My Discord.JS version is 11.4 (didn't had time to update it, due to work)

Upvotes: 2

Views: 1589

Answers (2)

Silent Watcher
Silent Watcher

Reputation: 125

I found the issue. Basically, because the user was added after the channel is created, Discord API was losing it (or something, these are only my guess atm).

After changing it to this:

            message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{

                id: message.guild.id,
                deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']

            }, {

                id: message.author.id,
                allow: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']

            }])

Everything works again. Thank you PiggyPlex.

Upvotes: 1

PiggyPlex
PiggyPlex

Reputation: 703

Firstly, the first 2 lines should be changed to:

let member = message.member,
    user = message.author;
// or
const { member, author: user } = message;

as whilst this isn't a problem, in strict mode it will cause an error as you technically do not have a variable keyword in front of user = member.user. You should try and use const if you aren't going to be changing the value of the variables. Note that message.author is the same as message.member.user.

Secondly, using the permissionOverwrites arg in Guild#createChannel is deprecated (see https://discord.js.org/#/docs/main/v11/class/Guild?scrollTo=createChannel). I am aware that Discord.JS has abolished many things despite them saying "deprecated". Try using the typeOrOptions argument to create the channel with the appropriate overrides instead. Here's my suggested code:

(async () => {
  message.delete();
  message.guild.createChannel(`⭐ ${message.author.username}'s Room`, {
    type: 'voice',
    parent: '567718414454358026',
    permissionOverwrites: [{
      id: message.guild.id, // @everyone has the ID of the guild
      deny: ['VIEW_CHANNEL', 'CONNECT'],
    }, {
      id: message.author.id, // attach the permission overrides for the user directly here
      allow: ['VIEW_CHANNEL', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER']
    }]
  });
  const embed = new Discord.RichEmbed()
                .setDescription(':white_check_mark: Successfully created a voice channel!')
                .setColor(config.green);
  const sentMessage = await message.channel.send(embed);
  sentMessage.delete(10 * 1000);
})();

Upvotes: 3

Related Questions