forked from abalabahaha/eris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuildAuditLogEntry.js
135 lines (128 loc) · 6.55 KB
/
GuildAuditLogEntry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
const Base = require("./Base");
const Invite = require("./Invite");
const Constants = require("../Constants");
/**
* Represents a guild audit log entry describing a moderation action
* @prop {String} id The ID of the entry
* @prop {Guild} guild The guild containing the entry
* @prop {Number} actionType The action type of the entry. See Constants.AuditLogActions for more details
* @prop {String?} reason The reason for the action
* @prop {User} user The user that performed the action
* @prop {String} targetID The ID of the action target
* @prop {(CategoryChannel | Guild | Member | Invite | Role | Object | TextChannel | VoiceChannel | NewsChannel)?} target The object of the action target
* If the item is not cached, this property will be null
* If the action targets a guild, this could be a Guild object
* If the action targets a guild channel, this could be a CategoryChannel, TextChannel, or VoiceChannel object
* If the action targets a member, this could be a Member object
* If the action targets a role, this could be a Role object
* If the action targets an invite, this is an Invite object
* If the action targets a webhook, this is null
* If the action targets a emoji, this could be an emoji Object
* If the action targets a message, this is a User object
* @prop {Object?} before The properties of the targeted object before the action was taken
* For example, if a channel was renamed from #general to #potato, this would be `{name: "general"}``
* @prop {Object?} after The properties of the targeted object after the action was taken
* For example, if a channel was renamed from #general to #potato, this would be `{name: "potato"}``
* @prop {Number?} count The number of messages deleted, action type 72 (MESSAGE_DELETE) only
* @prop {(CategoryChannel | TextChannel | VoiceChannel)?} channel The channel containing the deleted messages, action type 72 (MESSAGE_DELETE) only
* @prop {Number?} deleteMemberDays The number of days of inactivity to prune for, action type 21 (MEMBER_PRUNE) only
* @prop {Number?} membersRemoved The number of members pruned from the server, action type 21 (MEMBER_PRUNE) only
* @prop {(Member | Object)?} member The member described by the permission overwrite, action types 13-15 (CHANNEL\_OVERWRITE\_CREATE/UPDATE/DELETE) only. If the member is not cached, this could be {id: String}
* @prop {(Role | Object)?} role The role described by the permission overwrite, action types 13-15 (CHANNEL\_OVERWRITE\_CREATE/UPDATE/DELETE) only. If the role is not cached, this could be {id: String, name: String}
*/
class GuildAuditLogEntry extends Base {
constructor(data, guild) {
super(data.id);
this.guild = guild;
this.actionType = data.action_type;
this.reason = data.reason || null;
this.user = guild.shard.client.users.get(data.user_id);
this.before = null;
this.after = null;
if(data.changes) {
this.before = {};
this.after = {};
data.changes.forEach((change) => {
if(change.old_value != undefined) {
this.before[change.key] = change.old_value;
}
if(change.new_value != undefined) {
this.after[change.key] = change.new_value;
}
});
}
if(data.target_id) {
this.targetID = data.target_id;
}
if(data.options) {
if(data.options.count) {
this.count = +data.options.count;
}
if(data.options.channel_id) {
this.channel = guild.channels.get(data.options.channel_id);
}
if(data.options.delete_member_days) {
this.deleteMemberDays = +data.options.delete_member_days;
this.membersRemoved = +data.options.members_removed;
}
if(data.options.type) {
if(data.options.type === "member") {
this.member = guild.members.get(data.options.id) || {
id: data.options.id
};
} else if(data.options.type === "role") {
this.role = guild.roles.get(data.options.id) || {
id: data.options.id,
name: data.options.role_name
};
}
}
}
}
get target() { // pay more, get less
if(this.actionType < 10) { // Guild
return this.guild;
} else if(this.actionType < 20) { // Channel
return this.guild && this.guild.channels.get(this.targetID);
} else if(this.actionType < 30) { // Member
if(this.actionType === Constants.AuditLogActions.MEMBER_MOVE || this.actionType === Constants.AuditLogActions.MEMBER_DISCONNECT) { // MEMBER_MOVE / MEMBER_DISCONNECT
return null;
}
return this.guild && this.guild.members.get(this.targetID);
} else if(this.actionType < 40) { // Role
return this.guild && this.guild.roles.get(this.targetID);
} else if(this.actionType < 50) { // Invite
const changes = this.actionType === 42 ? this.before : this.after; // Apparently the meaning of life is a deleted invite
return new Invite({
code: changes.code,
channel: changes.channel,
guild: this.guild,
uses: changes.uses,
max_uses: changes.max_uses,
max_age: changes.max_age,
temporary: changes.temporary
}, this.guild && this.guild.shard.client);
} else if(this.actionType < 60) { // Webhook
return null; // Go get the webhook yourself
} else if(this.actionType < 70) { // Emoji
return this.guild && this.guild.emojis.find((emoji) => emoji.id === this.targetID);
} else if(this.actionType < 80) { // Message
return this.guild && this.guild.shard.client.users.get(this.targetID);
} else if(this.actionType < 90) { // Integrations
return null;
} else {
throw new Error("Unrecognized action type: " + this.actionType);
}
}
toJSON() {
const base = super.toJSON(true);
for(const prop of ["actionType", "after", "before", "channel", "count", "deleteMemberDays", "member", "membersRemoved", "reason", "role", "targetID", "user"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
}
}
module.exports = GuildAuditLogEntry;