feat: add support for guild templates (#4907)

Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
izexi
2020-11-21 14:09:56 +00:00
committed by GitHub
parent eaecd0e8b7
commit 2b2994badc
8 changed files with 339 additions and 4 deletions

View File

@@ -24,16 +24,40 @@ class DataResolver {
* @typedef {string} InviteResolvable
*/
/**
* Data that can be resolved to give an template code. This can be:
* * A template code
* * A template URL
* @typedef {string} GuildTemplateResolvable
*/
/**
* Resolves the string to a code based on the passed regex.
* @param {string} data The string to resolve
* @param {RegExp} regex The RegExp used to extract the code
* @returns {string}
*/
static resolveCode(data, regex) {
const match = regex.exec(data);
return match ? match[1] || data : data;
}
/**
* Resolves InviteResolvable to an invite code.
* @param {InviteResolvable} data The invite resolvable to resolve
* @returns {string}
*/
static resolveInviteCode(data) {
const inviteRegex = /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/i;
const match = inviteRegex.exec(data);
if (match && match[1]) return match[1];
return data;
return this.resolveCode(data, /discord(?:(?:app)?\.com\/invite|\.gg(?:\/invite)?)\/([\w-]{2,255})/i);
}
/**
* Resolves GuildTemplateResolvable to a template code.
* @param {GuildTemplateResolvable} data The template resolvable to resolve
* @returns {string}
*/
static resolveGuildTemplateCode(data) {
return this.resolveCode(data, /discord(?:app)?\.(?:com\/template|new)\/([\w-]{2,255})/i);
}
/**