xCesir
xCesir

Reputation: 25

How do I get the permission of an existing channel in Discord with node.js?

The concept behind this is to clone a specific channel. Create a new one if there is no empty channel left to join, and delete all empty channels except one. I'm trying to get the permissions of an existing Discord channel and create a new channel with the same permissions. The first part of the code detects what action was done and whether the channel is relevant to "clone" or not. The second part creates a new channel with the same permissions. (discord.js: v.13.8.1)

module.exports = {
    name: "voiceStateUpdate",
    async execute(oldChannel, newChannel) {
        try {
            var newUserChannel = newState.channelId; //new channel
            var oldUserChannel = oldState.channelId; //old channel

            if (oldUserChannel === null && newUserChannel !== null) {
                console.log("join " + toString(await querychannelcount(newUserChannel)));
                // User Joins a voice channel
            } else if (oldUserChannel !== null && newUserChannel === null) {
                console.log("leave " + toString(await querychannelcount(oldUserChannel)));
                // User leaves a voice channel
            } else if (oldUserChannel !== null && newUserChannel !== null && oldUserChannel !== newUserChannel) {
                channelCreate(newChannel);
                console.log("change " + toString(await querychannelcount(newUserChannel)));

                // User change a voice channel
            } else if (oldUserChannel === newUserChannel) {
                // channel state change (e.g. mute mice/deafen/stream etc.)
            } else {
                logger.warn("Cannot identify join/leave Event");
            }
        } catch (error) {
            logger.warn("Error while performing interactionCreate");
            console.log(error);
        }
    },
};

function querychannelcount(channelId) {
    try {
        var sql = "SELECT COUNT(id)*10 FROM channels Where id = ?";
        var Inserts = [channelId];
        sql = mysql.format(sql, Inserts);
        return new Promise((resolve, reject) => {
            con.query(sql, (err, result) => {
                return err ? reject(err) : resolve(result);
            });
        });
    } catch (error) {
        logger.error(`Error while performing 'SELECT' in the database: ${cascadingChannels_DB_database}, in Event JoinLeave`);
    }
}

function toString(object) {
    let ergebnis = JSON.stringify(object).length - 20;
    return ergebnis;
}

async function channelCreate(channelobj) {
    try {
        //create new channel with permissions of the old channel
    } catch (error) {
        console.log(error);
    }
}

Upvotes: 0

Views: 152

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

oldChannel is not a channel. It is VoiceState(voiceStateUpdate). Rename oldChannel, newChannel to oldState, newState for better understanding and use oldState.channel.clone() to clone the channel with the same permissions: channel#clone

let newChannel = await oldState.channel.clone({ name: 'new-channel-name' })

Upvotes: 1

Related Questions