How do I fetch the creator of a channel in discord?

Viewed 1924
bot.on('channelCreate', async channel => {
    if (!channel.guild) return;
    const fetchedLogs = await channel.guild.fetchAuditLogs({
      limit: 1,
      type: 'CHANNEL_CREATE',
    });

    const logbook = channel.guild.channels.cache.get("ChannelID")
    const deletionLog = fetchedLogs.entries.first();

    if (!deletionLog) return logbook.send(`A channel was updated but no relevant autid logs were found`);

    const { executor, user } = deletionLog;

    if (user.id) {
      logbook.send(`${executor.tag} created a channel`);
    }   else {
      logbook.send(`A channel was created but idk who did.`);
    }
});

I am a newbie when it comes to fetching actions through Discord Audit Logs; so I am experimenting and somehow came up with this code. However, when I create a channel, it does not send any messages saying that a channel has been created by @user. I have no idea what my next step will be. All I wanted to do was to know who created the channel.

Discord.JS: v12.2.0

1 Answers
client.on("channelCreate", async channel => {
    if (!channel.guild) return false; // This is a DM channel.

    const AuditLogFetch = await channel.guild.fetchAuditLogs({limit: 1, type: "CHANNEL_CREATE"}); // Fetching the audot logs.
    const LogChannel = client.channels.cache.get("722052103060848664"); // Getting the loggin channel. (Make sure its a TextChannel)

    if (!LogChannel) return console.error(`Invalid channel.`); // Checking if the channel exists.

    if (!AuditLogFetch.entries.first()) return console.error(`No entries found.`);

    const Entry = AuditLogFetch.entries.first(); // Getting the first entry of AuditLogs that was found.

    LogChannel.send(`${Entry.executor.tag || "Someone"} created a new channel. | ${channel}`) // Sending the message to the logging channel.
});

If the code I provided is not working, please make sure the bot has access to view AuditLogs.

Related