mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
Experimental support for Audit Logs (#1403)
* start audit logs * make better var types so gawdl3y doesn't shit on this * add constructor stuff * make more changes * add entry creation * add methods * make it all work hopefully * aaa * aaaa * i wish i could test this locally * fix users, guild when i feel like it * make guild prop non-enumerable * make better types * change nouns * e * Update GuildAuditLogs.js * Update GuildAuditLogs.js * Update GuildAuditLogs.js * eek * Update GuildAuditLogs.js * Update GuildAuditLogs.js * friggin trailing spaces * Update GuildAuditLogs.js * docs! * Update GuildAuditLogs.js * reason stuff * Update GuildAuditLogs.js * Update GuildAuditLogs.js * support before/after for pagination * Update Guild.js * Update GuildAuditLogs.js * mfw using github web editor * fix build * Update Guild.js * amazing cache fuckery shit evil * cool stuff * make building audit logs nicer * ban endpoint stuff * dox * <.<
This commit is contained in:
196
src/structures/GuildAuditLogs.js
Normal file
196
src/structures/GuildAuditLogs.js
Normal file
@@ -0,0 +1,196 @@
|
||||
const Targets = {
|
||||
GUILD: 'GUILD',
|
||||
CHANNEL: 'CHANNEL',
|
||||
USER: 'USER',
|
||||
ROLE: 'ROLE',
|
||||
INVITE: 'INVITE',
|
||||
WEBHOOK: 'WEBHOOK',
|
||||
EMOJI: 'EMOJI',
|
||||
};
|
||||
|
||||
const Actions = {
|
||||
GUILD_UPDATE: 1,
|
||||
CHANNEL_CREATE: 10,
|
||||
CHANNEL_UPDATE: 11,
|
||||
CHANNEL_DELETE: 12,
|
||||
CHANNEL_OVERWRITE_CREATE: 13,
|
||||
CHANNEL_OVERWRITE_UPDATE: 14,
|
||||
CHANNEL_OVERWRITE_DELETE: 15,
|
||||
MEMBER_KICK: 20,
|
||||
MEMBER_PRUNE: 21,
|
||||
MEMBER_BAN_ADD: 22,
|
||||
MEMBER_BAN_REMOVE: 23,
|
||||
MEMBER_UPDATE: 24,
|
||||
MEMBER_ROLE_UPDATE: 25,
|
||||
ROLE_CREATE: 30,
|
||||
ROLE_UPDATE: 31,
|
||||
ROLE_DELETE: 32,
|
||||
INVITE_CREATE: 40,
|
||||
INVITE_UPDATE: 41,
|
||||
INVITE_DELETE: 42,
|
||||
WEBHOOK_CREATE: 50,
|
||||
WEBHOOK_UPDATE: 51,
|
||||
WEBHOOK_DELETE: 52,
|
||||
EMOJI_CREATE: 60,
|
||||
EMOJI_UPDATE: 61,
|
||||
EMOJI_DELETE: 62,
|
||||
};
|
||||
|
||||
class GuildAuditLogs {
|
||||
constructor(guild, data) {
|
||||
if (data.users) for (const user of data.users) guild.client.dataManager.newUser(user);
|
||||
|
||||
/**
|
||||
* Entries for this Guild's audit logs
|
||||
* @type {GuildAuditLogsEntry[]}
|
||||
*/
|
||||
this.entries = [];
|
||||
for (const entry of data.audit_log_entries) this.entries.push(new GuildAuditLogsEntry(guild, entry));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles possible promises for entry targets
|
||||
* @returns {GuildAuditLogs}
|
||||
*/
|
||||
static build(...args) {
|
||||
return new Promise(resolve => {
|
||||
const logs = new GuildAuditLogs(...args);
|
||||
Promise.all(logs.entries.map(e => e.target)).then(() => resolve(logs));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Find target type from entry action
|
||||
* @param {number} target Action target
|
||||
* @returns {?string}
|
||||
*/
|
||||
static targetType(target) {
|
||||
if (target < 10) return Targets.GUILD;
|
||||
if (target < 20) return Targets.CHANNEL;
|
||||
if (target < 30) return Targets.USER;
|
||||
if (target < 40) return Targets.ROLE;
|
||||
if (target < 50) return Targets.INVITE;
|
||||
if (target < 60) return Targets.WEBHOOK;
|
||||
if (target < 70) return Targets.EMOJI;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find action type from entry action
|
||||
* @param {string} action Action target
|
||||
* @returns {string}
|
||||
*/
|
||||
static actionType(action) {
|
||||
if ([
|
||||
Actions.CHANNEL_CREATE,
|
||||
Actions.CHANNEL_OVERWRITE_CREATE,
|
||||
Actions.MEMBER_BAN_REMOVE,
|
||||
Actions.ROLE_CREATE,
|
||||
Actions.INVITE_CREATE,
|
||||
Actions.WEBHOOK_CREATE,
|
||||
Actions.EMOJI_CREATE,
|
||||
].includes(action)) return 'CREATE';
|
||||
|
||||
if ([
|
||||
Actions.CHANNEL_DELETE,
|
||||
Actions.CHANNEL_OVERWRITE_DELETE,
|
||||
Actions.MEMBER_KICK,
|
||||
Actions.MEMBER_PRUNE,
|
||||
Actions.MEMBER_BAN_ADD,
|
||||
Actions.ROLE_DELETE,
|
||||
Actions.INVITE_DELETE,
|
||||
Actions.WEBHOOK_DELETE,
|
||||
Actions.EMOJI_DELETE,
|
||||
].includes(action)) return 'DELETE';
|
||||
|
||||
if ([
|
||||
Actions.GUILD_UPDATE,
|
||||
Actions.CHANNEL_UPDATE,
|
||||
Actions.CHANNEL_OVERWRITE_UPDATE,
|
||||
Actions.MEMBER_UPDATE,
|
||||
Actions.ROLE_UPDATE,
|
||||
Actions.INVITE_UPDATE,
|
||||
Actions.WEBHOOK_UPDATE,
|
||||
Actions.EMOJI_UPDATE,
|
||||
].includes(action)) return 'UPDATE';
|
||||
|
||||
return 'ALL';
|
||||
}
|
||||
}
|
||||
|
||||
class GuildAuditLogsEntry {
|
||||
constructor(guild, data) {
|
||||
const targetType = GuildAuditLogs.targetType(data.action_type);
|
||||
/**
|
||||
* Target type of this entry
|
||||
* @type {string}
|
||||
*/
|
||||
this.targetType = targetType;
|
||||
|
||||
/**
|
||||
* Action type of this entry
|
||||
* @type {string}
|
||||
*/
|
||||
this.actionType = GuildAuditLogs.actionType(data.action_type);
|
||||
|
||||
/**
|
||||
* Specific action type of this entry
|
||||
* @type {string}
|
||||
*/
|
||||
this.action = Object.keys(Actions).find(k => Actions[k] === data.action_type);
|
||||
|
||||
/**
|
||||
* Reason of this entry
|
||||
* @type {?string}
|
||||
*/
|
||||
this.reason = data.reason || null;
|
||||
|
||||
/**
|
||||
* User that executed this entry
|
||||
* @type {User}
|
||||
*/
|
||||
this.executor = guild.client.users.get(data.user_id);
|
||||
|
||||
/**
|
||||
* Specific property changes
|
||||
* @type {Object[]}
|
||||
*/
|
||||
this.changes = data.changes ? data.changes.map(c => ({ name: c.key, old: c.old_value, new: c.new_value })) : null;
|
||||
|
||||
/**
|
||||
* ID of this entry
|
||||
* @type {Snowflake}
|
||||
*/
|
||||
this.id = data.id;
|
||||
|
||||
if (['USER', 'GUILD'].includes(targetType)) {
|
||||
/**
|
||||
* Target of this entry
|
||||
* @type {?Guild|User|Role|Emoji|Promise<Invite>|Promise<Webhook>}
|
||||
*/
|
||||
this.target = guild.client[`${targetType.toLowerCase()}s`].get(data.target_id);
|
||||
} else if (targetType === 'WEBHOOK') {
|
||||
this.target = guild.fetchWebhooks()
|
||||
.then(hooks => {
|
||||
this.target = hooks.find(h => h.id === data.target_id);
|
||||
return this.target;
|
||||
});
|
||||
} else if (targetType === 'INVITE') {
|
||||
const change = this.changes.find(c => c.name === 'code');
|
||||
this.target = guild.fetchInvites()
|
||||
.then(invites => {
|
||||
this.target = invites.find(i => i.code === (change.new || change.old));
|
||||
return this.target;
|
||||
});
|
||||
} else {
|
||||
this.target = guild[`${targetType.toLowerCase()}s`].get(data.target_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GuildAuditLogs.Actions = Actions;
|
||||
GuildAuditLogs.Targets = Targets;
|
||||
GuildAuditLogs.Entry = GuildAuditLogsEntry;
|
||||
|
||||
module.exports = GuildAuditLogs;
|
||||
Reference in New Issue
Block a user