/
Action.js
75 lines (61 loc) · 1.89 KB
/
Action.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
'use strict';
const { PartialTypes } = require('../../util/Constants');
/*
ABOUT ACTIONS
Actions are similar to WebSocket Packet Handlers, but since introducing
the REST API methods, in order to prevent rewriting code to handle data,
"actions" have been introduced. They're basically what Packet Handlers
used to be but they're strictly for manipulating data and making sure
that WebSocket events don't clash with REST methods.
*/
class GenericAction {
constructor(client) {
this.client = client;
}
handle(data) {
return data;
}
getPayload(data, store, id, partialType, cache) {
const existing = store.get(id);
if (!existing && this.client.options.partials.includes(partialType)) {
return store.add(data, cache);
}
return existing;
}
getChannel(data) {
const id = data.channel_id || data.id;
return data.channel || this.getPayload({
id,
guild_id: data.guild_id,
}, this.client.channels, id, PartialTypes.CHANNEL);
}
getMessage(data, channel, cache) {
const id = data.message_id || data.id;
return data.message || this.getPayload({
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
}, channel.messages, id, PartialTypes.MESSAGE, cache);
}
getReaction(data, message, user) {
const id = data.emoji.id || decodeURIComponent(data.emoji.name);
return this.getPayload({
emoji: data.emoji,
count: 0,
me: user.id === this.client.user.id,
}, message.reactions, id, PartialTypes.MESSAGE);
}
getMember(data, guild) {
const id = data.user.id;
return this.getPayload({
user: {
id,
},
}, guild.members, id, PartialTypes.GUILD_MEMBER);
}
getUser(data) {
const id = data.user_id;
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
}
}
module.exports = GenericAction;