forked from timotejroiko/discord.js-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.js
258 lines (224 loc) · 7.45 KB
/
extensions.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"use strict";
const { Collection } = require("@discordjs/collection");
const { override, getOrCreateChannel, getOrCreateGuild } = require("./functions");
override("/rest/APIRequest.js", X => class APIRequest extends X {
async make() {
const response = await super.make();
if(this.client.listenerCount("rest")) {
let data = "";
response.body.on("data", d => { data += d.toString(); });
response.body.on("end", () => {
this.client.emit("rest", {
path: this.path,
method: this.method,
responseHeaders: response.headers.raw(),
responseBody: data
});
});
}
return response;
}
});
override("/managers/GuildMemberRoleManager.js", X => class GuildMemberRoleManager extends X {
get cache() {
const everyone = this.guild.roles.everyone;
const roles = new Collection([[everyone.id, everyone]]);
for(const id of this.member._roles) {
let role = this.guild.roles.cache.get(id);
if(!role) {
role = this.guild.roles._add({ id, permissions: 0 }, false);
role.partial = true;
}
roles.set(id, role);
}
return roles;
}
});
override("/managers/RoleManager.js", X => class RoleManager extends X {
get everyone() {
return super.everyone || this._add({ id: this.guild.id, permissions: 0 }, false);
}
});
override("/structures/MessageMentions.js", X => class MessageMentions extends X {
constructor(message, users, roles, everyone, crosspostedChannels, repliedUser) {
super(message, users, roles, everyone, crosspostedChannels, repliedUser);
if(users?.length) {
for(const mention of users) {
if(mention.member && !this.members.has(mention.id)) {
this._members.set(mention.id, this.guild.members._add(Object.assign(mention.member, { user: mention })));
}
}
}
if(roles?.length && this.guild) {
for(const id of roles) {
if(!this.roles.has(id)) {
this.roles.set(id, this.guild.roles._add({ id, permissions: 0 }, false));
}
}
}
}
get channels() {
if(this._channels) { return this._channels; }
this._channels = new Collection();
let matches;
while((matches = this.constructor.CHANNELS_PATTERN.exec(this._content)) !== null) {
const chan = getOrCreateChannel(this.client, matches[1], this.guild);
this._channels.set(chan.id, chan);
}
return this._channels;
}
});
override("/structures/Message.js", obj => {
obj.Message = class Message extends obj.Message {
constructor(client, data) {
super(client, data);
if(data.guild_id && !this.guildId) {
this.guildId = data.guild_id;
}
}
_patch(data) {
super._patch(data);
if(data.member && !this.member && this.author && this.guild) {
this._member = this.guild.members._add(Object.assign(data.member, { user: this.author }));
}
}
get member() {
if(!this.guild) { return null; }
const id = this.author?.id || this._member?.id;
if(!id) { return null; }
return this.guild.members.cache.get(id) || this._member || null;
}
get channel() {
return getOrCreateChannel(this.client, this.channelId, this.guild);
}
get guild() {
return this.guildId ? getOrCreateGuild(this.client, this.guildId) : null;
}
};
return obj;
});
override("/structures/BaseGuild.js", X => class BaseGuild extends X {
get nameAcronym() {
return this.name ? super.nameAcronym : null;
}
});
override("/structures/Guild.js", obj => {
obj.Guild = class Guild extends obj.Guild {
_patch(data) {
super._patch(data);
if(data.members) {
const me = data.members.find(member => member.user.id === this.client.user.id);
if(me && !this.me) {
this.members.cache.forceSet(me.user.id, this.members._add(me));
}
}
if(data.roles) {
const everyone = data.roles.find(role => role.id === this.id);
if(everyone && !this.roles.cache.has(everyone.id)) { this.roles.cache.forceSet(everyone.id, this.roles._add(everyone)); }
}
}
};
return obj;
});
override("/structures/Interaction.js", X => class Interaction extends X {
get channel() {
return this.channelId ? getOrCreateChannel(this.client, this.channelId, this.guild) : null;
}
get guild() {
return this.guildId ? getOrCreateGuild(this.client, this.guildId) : null;
}
});
const Discord = require("discord.js");
const { create } = Discord.Channel;
Discord.Channel.create = function(client, data, guild, { fromInteraction } = {}) {
if(data instanceof this) { return data; }
const channel = create(client, data, guild, { allowUnknownGuild: true, fromInteraction });
if(channel && channel.guild && channel.guild.channels) { channel.guild.channels.cache.set(channel.id, channel); }
return channel;
};
Discord.GuildChannel.prototype.fetchOverwrites = async function() {
const channel = await this.client.api.channels(this.id).get();
const collection = new Collection();
if(channel.permission_overwrites) {
for(const overwrite of channel.permission_overwrites) {
collection.set(overwrite.id, this.permissionOverwrites._add(overwrite));
}
}
return collection;
};
Discord.UserManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.GuildManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.ChannelManager.prototype.forge = function(id, _type = "DM") {
const type = Discord.Constants.ChannelTypes[_type.toUpperCase()];
let guild;
if(type !== 1) { guild = this.client.guilds.forge("0"); }
return this._add({ id, type }, guild, { cache: false, allowUnknownGuild: true });
};
Discord.GuildChannelManager.prototype.forge = function(id, type = "GUILD_TEXT") {
return this.client.channels._add({
id,
type: Discord.Constants.ChannelTypes[type.toUpperCase()]
}, this.guild, { cache: false, allowUnknownGuild: true });
};
Discord.GuildMemberManager.prototype.forge = function(id) {
return this._add({ user: { id } }, false);
};
Discord.GuildEmojiManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.ReactionManager.prototype.forge = function(id) {
const emoji = {};
if(isNaN(id)) {
emoji.name = id;
} else {
emoji.id = id;
}
return this._add({ emoji }, false);
};
Discord.RoleManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.PresenceManager.prototype.forge = function(id) {
return this._add({ user: { id } }, false);
};
Discord.MessageManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.GuildBanManager.prototype.forge = function(id) {
return this._add({ user: { id } }, false);
};
Discord.ApplicationCommandManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.GuildInviteManager.prototype.forge = function(code, id) {
return this._add({ code, channel: { id } }, false);
};
Discord.GuildStickerManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.GuildScheduledEventManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.StageInstanceManager.prototype.forge = function(id) {
return this._add({ id }, false);
};
Discord.VoiceStateManager.prototype.forge = function(user_id) {
return this._add({ user_id }, false);
};
Discord.PermissionOverwriteManager.prototype.forge = function(id, type) {
return this._add({ id, type, allow: 0, deny: 0 });
};
Discord.ThreadMemberManager.prototype.forge = function(user_id) {
return this._add({ user_id });
};
Discord.LimitedCollection.prototype.forceSet = function(key, value) {
return Object.getPrototypeOf(Object.getPrototypeOf(this)).set.call(this, key, value);
};
Discord.Collection.prototype.forceSet = function(key, value) {
return this.set(key, value);
};
module.exports = Discord;