Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Client): guildAuditLogEntryCreate event #9058

Merged
merged 6 commits into from
Feb 17, 2023

Conversation

almeidx
Copy link
Member

@almeidx almeidx commented Jan 13, 2023

Please describe the changes this PR makes and why it should be merged:
Implements basic support for the GUILD_AUDIT_LOG_ENTRY_CREATE event

Changes:

  • Add Client#guildAuditLogEntryCreate event
  • GuildAuditLogsEntry#targetId and GuildAuditLogsEntry#executorId to address feat(Client): guildAuditLogEntryCreate event #9058 (comment)
  • Moved logs to the last constructor parameter of GuildAuditLogsEntry (which is private) so as to not make it nullable (we don't received it via the gateway)

Upstream:

Status and versioning classification:

  • I know how to update typings and have done so, or typings don't need updating

@vercel
Copy link

vercel bot commented Jan 13, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
discord-js ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 17, 2023 at 8:12PM (UTC)
discord-js-guide ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 17, 2023 at 8:12PM (UTC)

Copy link
Member

@Jiralite Jiralite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should update the list of intents too via the issue tracker.

Copy link
Member

@Jiralite Jiralite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work because the gateway event is called GUILD_AUDIT_LOG_ENTRY_CREATE but this is looking for AUDIT_LOG_ENTRY_CREATE.

@Jiralite
Copy link
Member

Jiralite commented Jan 14, 2023

I seem to have found a bug with this event. Use this very basic code sample:

import { Client, Events, GatewayIntentBits } from "discord.js";

const client = new Client({ intents: GatewayIntentBits.Guilds | GatewayIntentBits.GuildBans });

client.on("raw", console.log);
client.on(Events.AuditLogEntryCreate, console.log);

client.login();

Upon pinning a message, this event will be received:

{
  t: 'GUILD_AUDIT_LOG_ENTRY_CREATE',
  s: 6,
  op: 0,
  d: {
    user_id: 'user_id',
    target_id: 'target_id',
    options: {
      message_id: 'message_id',
      channel_id: 'channel_id'
    },
    id: 'id',
    action_type: 74,
    guild_id: 'guild_id'
  }
}

When discord.js constructs a GuildAuditLogsEntry out of it, GuildAuditLogsEntry#executor and GuildAuditLogsEntry#target are both undefined.

You can see how these are defined here:

this.executor = data.user_id
? guild.client.options.partials.includes(Partials.User)
? guild.client.users._add({ id: data.user_id })
: guild.client.users.cache.get(data.user_id)
: null;

} else if (targetType === Targets.Message) {
// Discord sends a channel id for the MessageBulkDelete action type.
this.target =
data.action_type === AuditLogEvent.MessageBulkDelete
? guild.channels.cache.get(data.target_id) ?? { id: data.target_id }
: guild.client.users.cache.get(data.target_id);

When audit logs are fetched (as they have been all this time), Discord sends across the user objects that the audit log references and we store them safely away. However, the gateway does not do this. These are now potentially undefined and the same may go for application commands, auto moderation rules, scheduled events, integrations, threads, and webhooks. Currently, these are typed as always present, but they are documented as nullable, so that is good for now. The typings for this are somewhat complex, though.

@almeidx almeidx marked this pull request as draft January 14, 2023 23:54
@Jiralite Jiralite removed the blocked label Jan 18, 2023
@almeidx almeidx force-pushed the discord.js/audit-log-entry-create branch from 4207efd to 5e41087 Compare January 19, 2023 01:21
@vercel
Copy link

vercel bot commented Jan 19, 2023

@almeidx is attempting to deploy a commit to the discordjs Team on Vercel.

A member of the Team first needs to authorize it.

@almeidx almeidx changed the title feat(Client): auditLogEntryCreate event feat(Client): guildAuditLogEntryCreate event Jan 19, 2023
@almeidx almeidx marked this pull request as ready for review January 20, 2023 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

6 participants