首页 > 解决方案 > 如何获取不和谐频道的创建者?

问题描述

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.`);
    }
});

在通过以下方式获取操作时,我是一个新手Discord Audit Logs;所以我正在试验,并以某种方式想出了这段代码。但是,当我创建频道时,它不会发送任何消息说频道已由@user. 我不知道我的下一步是什么。我只想知道是谁创建了这个频道。

不和谐.JS:v12.2.0

标签: javascriptnode.jsdiscordbotsdiscord.js

解决方案


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.
});

如果我提供的代码不起作用,请确保机器人有权查看 AuditLogs。


推荐阅读