W
W
WayneWayne2020-04-30 17:15:15
JavaScript
WayneWayne, 2020-04-30 17:15:15

What is the correct way to check if a channel exists in discord.js?

So, I am writing a support bot with a ticket function. I tried many ways, but did not find the necessary one in order to prohibit the bot from creating a channel if a person has already asked a question.
Source:

client.on('message', async sbs => {
    const userMessageDict ={};
    let guild = client.guilds.get("703566530201518110");
    let role = guild.roles.find(r => r.name === "@everyone")
    let channel = guild.channels.find(c => c.type === 'category' && c.name === 'ticket')
    if (!sbs.channel.name.startsWith('support')) return;
    if (sbs.content.toLowerCase() === `${config.prefix}sob`) {
        var userDiscriminator = sbs.author.discriminator;  // начинается
        let zapret = guild.channels.find(c => c.type === 'text' && c.name === 'ticket-' + userDiscriminator)
        if (typeof zapret !== "undefined") {
            return sbs.channel.send("<@" + sbs.author.id + ">, Вы уже задавали вопрос, ожидайте ответа.")
        } // заканчивается
        var userDiscriminator = sbs.author.discriminator;
        let newchannel = await guild.createChannel('ticket-' + userDiscriminator, {
            type: 'text',
            permissionOverwrites: [{
                id: sbs.author.id,
                deny: ['MANAGE_MESSAGES'],
                allow: ['SEND_MESSAGES']
            }, {
                id: role.id,
                deny: ['VIEW_CHANNEL']
        }]
        });
        userMessageDict[sbs.author.id] = null;
        newchannel.setParent(channel.id)
        sbs.delete({ timeout: 0 })
            .then(() => {
                return sbs.channel.send("<@" + sbs.author.id + ">, перейдите в текстовый канал <#" + newchannel.id + ">");
            });
            setTimeout(() => {
                newchannel.delete();
                console.log('Канал <#' + newchannel.id + '> был удален по истечению срока действия.')
            }, 30000);
    }
});

From lines 8 to 12 (inclusive) I tried to check for the existence of the channel and make an alert that the person has already asked a question. The fact is that when (which means not found = found), the bot still does not create a channel, although there is no channel. I tried different options , values ​​, with different values ​​, , but the bot does not give the desired result. if (typeof zapret !== "undefined")typeofundefineddefined=!=

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WayneWayne, 2020-05-02
@WayneWayne

Actually, I did not wait for an answer, having tried several dozen options, I found a solution.
Maybe someone needs:

let channela = guild.channels.find(c => c.name == 'ticket')
            if(channela)
            sbs.delete({ timeout: 0 })
            .then(() => {
                return sbs.channel.send("<@" + sbs.author.id + ">, Вы уже задавали вопрос, ожидайте проверки.");
            });
            console.log("Канал найден.");
            if(!channela) {
                console.log("Канал не найден.");
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question