forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GuildInviteManager.js
185 lines (165 loc) · 5.67 KB
/
GuildInviteManager.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
'use strict';
const BaseManager = require('./BaseManager');
const { TypeError, Error } = require('../errors');
const Invite = require('../structures/Invite');
const Collection = require('../util/Collection');
const DataResolver = require('../util/DataResolver');
/**
* Manages API methods for GuildInvites and stores their cache.
* @extends {BaseManager}
*/
class GuildInviteManager extends BaseManager {
constructor(guild, iterable) {
super(guild.client, iterable, Invite);
/**
* The guild this Manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
/**
* The cache of this Manager
* @type {Collection<Snowflake, Invite>}
* @name GuildInviteManager#cache
*/
add(data, cache) {
return super.add(data, cache, { id: data.code, extras: [this.guild] });
}
/**
* Data that resolves to give a Invite object. This can be:
* * An invite code
* * An invite URL
* @typedef {string} InviteResolvable
*/
/**
* Resolves a InviteResolvable to a Invite object.
* @method resolve
* @memberof GuildInviteManager
* @instance
* @param {InviteResolvable} invite The invite resolvable to resolve
* @returns {?Invite}
*/
/**
* Resolves a InviteResolvable to a invite code string.
* @method resolveID
* @memberof GuildInviteManager
* @instance
* @param {InviteResolvable} invite The invite resolvable to resolve
* @returns {?string}
*/
/**
* Options used to fetch a single invite from a guild.
* @typedef {Object} FetchInviteOptions
* @property {InviteResolvable} code The invite to fetch
* @property {boolean} [cache=true] Whether or not to cache the fetched invite
* @property {boolean} [force=false] Whether to skip the cache check and request the API
*/
/**
* Options used to fetch all invites from a guild.
* @typedef {Object} FetchInvitesOptions
* @property {boolean} cache Whether or not to cache the fetched invites
*/
/**
* Fetches invite(s) from Discord.
* @param {InviteResolvable|FetchInviteOptions|FetchInvitesOptions} [options] Options for fetching guild invite(s)
* @returns {Promise<Invite>|Promise<Collection<Snowflake, Invite>>}
* @example
* // Fetch all invites from a guild
* guild.invites.fetch()
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch all invites from a guild without caching
* guild.invites.fetch({ cache: false })
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch a single invite
* guild.invites.fetch('bRCvFy9')
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch a single invite without checking cache
* guild.invites.fetch({ code: 'bRCvFy9', force: true })
* .then(console.log)
* .catch(console.error)
* @example
* // Fetch a single invite without caching
* guild.invites.fetch({ code: 'bRCvFy9', cache: false })
* .then(console.log)
* .catch(console.error);
*/
fetch(options) {
if (!options) return this._fetchMany();
if (typeof options === 'string') {
const code = DataResolver.resolveInviteCode(options);
if (!code) return Promise.reject(new Error('INVITE_RESOLVE_CODE'));
return this._fetchSingle({ code, cache: true });
}
if (!options.code) {
if ('cache' in options) return this._fetchMany(options.cache);
return Promise.reject(new Error('INVITE_RESOLVE_CODE'));
}
return this._fetchSingle({
...options,
code: DataResolver.resolveInviteCode(options.code),
});
}
async _fetchSingle({ code, cache, force = false }) {
if (!force) {
const existing = this.cache.get(code);
if (existing) return existing;
}
const invites = await this._fetchMany(cache);
const invite = invites.get(code);
if (!invite) throw new Error('INVITE_NOT_FOUND');
return invite;
}
async _fetchMany(cache) {
const data = await this.client.api.guilds(this.guild.id).invites.get();
return data.reduce((col, invite) => col.set(invite.code, this.add(invite, cache)), new Collection());
}
/**
* Create a invite to the guild from the provided channel.
* @param {GuildChannelResolvable} channel The options for creating the invite from a channel.
* @param {CreateInviteOptions} [options={}] The options for creating the invite from a channel.
* @returns {Promise<Invite>}
* @example
* // Create an invite to a selected channel
* guild.invites.create('599942732013764608')
* .then(console.log)
* .catch(console.error);
*/
async create(
channel,
{ temporary = false, maxAge = 86400, maxUses = 0, unique, targetUser, targetApplication, targetType, reason } = {},
) {
if (typeof options !== 'object') throw new TypeError('INVALID_TYPE', 'options', 'object', true);
const id = this.guild.channels.resolveID(channel);
if (!id) throw new Error('GUILD_CHANNEL_RESOLVE');
const invite = await this.client.api.channels(id).invites.post({
data: {
temporary,
max_age: maxAge,
max_uses: maxUses,
unique,
target_user_id: this.client.users.resolveID(targetUser),
target_application_id: targetApplication?.id ?? targetApplication?.applicationID ?? targetApplication,
target_type: targetType,
},
reason,
});
return new Invite(this.client, invite);
}
/**
* Deletes a invite.
* @param {InviteResolvable} invite The invite to delete
* @param {string} [reason] Reason for deleting the invite
* @returns {Promise<void>}
*/
async delete(invite, reason) {
const code = DataResolver.resolveInviteCode(invite);
await this.client.api.invites(code).delete({ reason });
}
}
module.exports = GuildInviteManager;