我才刚刚开始学习如何创建discord机器人,我正在努力弄清楚如何记录是谁删除了一条消息.

我try 了message.author,但当然,这会记录谁发送了消息,而且我不知道很多语法,所以我没有try 其他任何事情.

推荐答案

您可以使用在删除消息时触发的messageDelete event.如果一个用户删除了另一个用户的消息,您可以判断审核日志(log).

首先,确保您有所需的意图:GuildsGuildMembersGuildMessages.你还需要partials:Channel,MessageGuildMember来处理在你的机器人上线之前发送的消息.

一旦消息被删除,您可以使用fetchAuditLogs方法来获取发送被删除消息的行会的审核日志(log).

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
  ],
  partials: [
    Partials.Channel,
    Partials.GuildMember,
    Partials.Message,
  ],
});

client.on('messageDelete', async (message) => {
  const logs = await message.guild.fetchAuditLogs({
    type: AuditLogEvent.MessageDelete,
    limit: 1,
  });
  // logs.entries is a collection, so grab the first one
  const firstEntry = logs.entries.first();
  const { executorId, target, targetId } = firstEntry;
  // Ensure the executor is cached
  const user = await client.users.fetch(executorId);

  if (target) {
    // The message object is in the cache and you can provide a detailed log here
    console.log(`A message by ${target.tag} was deleted by ${user.tag}.`);
  } else {
    // The message object was not cached, but you can still retrieve some information
    console.log(`A message with id ${targetId} was deleted by ${user.tag}.`);
  }
});

在discord.js v14.8+中有一个新事件,GuildAuditLogEntryCreate.一旦收到相应的审核日志(log)事件(GuildAuditLogEntryCreate),您就可以找出是谁删除了邮件.它需要启用GuildModeration意图.

const { AuditLogEvent, Events } = require('discord.js');

client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
  // Define your variables
  const { action, executorId, target, targetId } = auditLog;

  // Check only for deleted messages
  if (action !== AuditLogEvent.MessageDelete) return;

  // Ensure the executor is cached
  const user = await client.users.fetch(executorId);

  if (target) {
    // The message object is in the cache and you can provide a detailed log here
    console.log(`A message by ${target.tag} was deleted by ${user.tag}.`);
  } else {
    // The message object was not cached, but you can still retrieve some information
    console.log(`A message with id ${targetId} was deleted by ${user.tag}.`);
  }
});

Javascript相关问答推荐

如何判断属于多个元素的属性是否具有多个值之一

微软Edge Select 间隙鼠标退出问题

在React中获取数据后,如何避免不必要的组件闪现1秒?

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

CheckBox作为Vue3中的一个组件

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

在运行时使用Next JS App Router在服务器组件中运行自定义函数

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

搜索功能不是在分页的每一页上进行搜索

Vaadin定制组件-保持对javascrip变量的访问

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

我想将Sitecore搜索面过滤器从多个转换为单个

重新呈现-react -筛选数据过多

不协调嵌入图片

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空

在Press Reaction本机和EXPO av上播放单个文件

是否有静态版本的`instanceof`?