如何获取不和谐频道的创建者?

Posted

技术标签:

【中文标题】如何获取不和谐频道的创建者?【英文标题】:How do I fetch the creator of a channel in discord? 【发布时间】:2020-11-07 20:47:17 【问题描述】:
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 创建。我不知道我的下一步是什么。我只想知道是谁创建了这个频道。

Discord.JS:v12.2.0

【问题讨论】:

您是否在控制台中遇到任何错误? @Jakye 不,我没有收到任何错误。我只是忘了添加try...catch 【参考方案1】:
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。

【讨论】:

以上是关于如何获取不和谐频道的创建者?的主要内容,如果未能解决你的问题,请参考以下文章

无法在获取的频道中发送消息 |不和谐.js

如何让我的不和谐机器人只读取某个频道中的内容

如何让不和谐机器人在命令上创建频道

获取Bot的频道?不和谐.py

如何使用 discord.py 获取不和谐用户的用户 ID

如何使用 dhooks 查找频道中发送的最新消息