diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2023-09-02 19:12:47 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2023-09-02 19:12:47 -0400 |
commit | e4450c8417624b71d779cb4f41692538f9165e10 (patch) | |
tree | b70826542223ecdf8a7a259f61b0a1abb8a217d8 /node_modules/discord.js/src/client/actions/Action.js | |
download | sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.gz sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.bz2 sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.zip |
first commit
Diffstat (limited to 'node_modules/discord.js/src/client/actions/Action.js')
-rw-r--r-- | node_modules/discord.js/src/client/actions/Action.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/client/actions/Action.js b/node_modules/discord.js/src/client/actions/Action.js new file mode 100644 index 0000000..6c04a64 --- /dev/null +++ b/node_modules/discord.js/src/client/actions/Action.js @@ -0,0 +1,120 @@ +'use strict'; + +const Partials = require('../../util/Partials'); + +/* + +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, manager, id, partialType, cache) { + return this.client.options.partials.includes(partialType) ? manager._add(data, cache) : manager.cache.get(id); + } + + getChannel(data) { + const payloadData = {}; + const id = data.channel_id ?? data.id; + + if ('recipients' in data) { + payloadData.recipients = data.recipients; + } else { + // Try to resolve the recipient, but do not add the client user. + const recipient = data.author ?? data.user ?? { id: data.user_id }; + if (recipient.id !== this.client.user.id) payloadData.recipients = [recipient]; + } + + if (id !== undefined) payloadData.id = id; + if ('guild_id' in data) payloadData.guild_id = data.guild_id; + if ('last_message_id' in data) payloadData.last_message_id = data.last_message_id; + + return ( + data[this.client.actions.injectedChannel] ?? + this.getPayload(payloadData, this.client.channels, id, Partials.Channel) + ); + } + + getMessage(data, channel, cache) { + const id = data.message_id ?? data.id; + return ( + data[this.client.actions.injectedMessage] ?? + this.getPayload( + { + id, + channel_id: channel.id, + guild_id: data.guild_id ?? channel.guild?.id, + }, + channel.messages, + id, + Partials.Message, + cache, + ) + ); + } + + getReaction(data, message, user) { + const id = data.emoji.id ?? decodeURIComponent(data.emoji.name); + return this.getPayload( + { + emoji: data.emoji, + count: message.partial ? null : 0, + me: user?.id === this.client.user.id, + }, + message.reactions, + id, + Partials.Reaction, + ); + } + + getMember(data, guild) { + return this.getPayload(data, guild.members, data.user.id, Partials.GuildMember); + } + + getUser(data) { + const id = data.user_id; + return data[this.client.actions.injectedUser] ?? this.getPayload({ id }, this.client.users, id, Partials.User); + } + + getUserFromMember(data) { + if (data.guild_id && data.member?.user) { + const guild = this.client.guilds.cache.get(data.guild_id); + if (guild) { + return guild.members._add(data.member).user; + } else { + return this.client.users._add(data.member.user); + } + } + return this.getUser(data); + } + + getScheduledEvent(data, guild) { + const id = data.guild_scheduled_event_id ?? data.id; + return this.getPayload( + { id, guild_id: data.guild_id ?? guild.id }, + guild.scheduledEvents, + id, + Partials.GuildScheduledEvent, + ); + } + + getThreadMember(id, manager) { + return this.getPayload({ user_id: id }, manager, id, Partials.ThreadMember, false); + } +} + +module.exports = GenericAction; |