所以我像机器人一样编码,检测频道是否被删除,如果是,我需要这个人,这样我就可以用机器人踢他们。这是代码
client.on('channelDelete', async (channel) => {
// Get the channel ID
const channelDeleteId = channel.id;
// Fetch all channel deletions from the audit log
const auditLogs = await channel.guild.fetchAuditLogs({ type: 'CHANNEL_DELETE' });
// Find the log entry for this specific channel
const channelDeleteEntry = auditLogs.entries.find((entry) => entry.target.id === channelDeleteId);
if (channelDeleteEntry) {
const deleter = channelDeleteEntry.executor;
console.log(`User ${deleter.username} deleted channel ${channel.name} at time ${channelDeleteEntry.createdAt}`);
}
});
我尝试询问不和谐的人,仍然出现错误,错误是
DiscordAPIError[50035]: Invalid Form Body action_type[NUMBER_TYPE_COERCE]: Value "CHANNEL_DELETE" is not int.
请帮忙
fetchAuditLogs()
函数需要 type:
属性的数值,而不是您在此处使用的字符串值。您可能发现了来自先前版本的此功能的过时教程或示例。在某些时候,API 已更新为使用数值进行枚举。完整的可能性列表及其数值可以在here找到,但这行代码应该是这样的:
const auditLogs = await channel.guild.fetchAuditLogs({ type: 12 });
遗憾的是,我对 JavaScript 的处理还不够深入,不知道是否有一种方法可以像在 C# 中那样从枚举列表中访问这些值。也许有人可以提供编辑/评论来包含这一点。有了数字,您必须参考外部文档才能了解它们的含义,这并不理想。