Created
April 25, 2019 18:13
-
-
Save Venipa/3b824322a7068d5838e541cebce12c3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Resolver, ResourceProxy, Lang, Message, Command, Util, Client, Logger } from "@yamdbf/core"; | |
import { Collection, Emoji } from "discord.js"; | |
const log = Logger.instance('EmojiResolver'); | |
export class EmoteResolver extends Resolver { | |
public constructor(client: Client) | |
{ | |
super(client, 'Emoji'); | |
} | |
public async validate(value: any): Promise<boolean> | |
{ | |
return value instanceof Emoji; | |
} | |
getEmoteRegex = () => /^(<?(.)?:(.*):)(\d+)>?$/; | |
getEmojiRegex = () => /^[\u0020-\u007e\u00a0-\u00ff]*$/; | |
public async resolveRaw(value: string, context: Partial<Message> = {}): Promise<Emoji | Collection<string, Emoji> | undefined> | |
{ | |
if (!context.guild) throw new Error('Cannot resolve given value: missing context'); | |
let emote: Emoji; | |
const emoteRegex: RegExp = this.getEmoteRegex(); | |
const emojiRegex: RegExp = this.getEmojiRegex(); | |
if (!emojiRegex.test(value)) | |
return; | |
if (emoteRegex.test(value)) | |
{ | |
const parsedValue: RegExpMatchArray = value.match(emoteRegex)!; | |
const isAnimated: boolean = parsedValue[2] === "a"; | |
const id: string = parsedValue[4]; | |
const name: string = parsedValue[3]; | |
log.debug(id + ", " + name); | |
emote = context.client.emojis.resolve(id)!; | |
if (!emote) return new Emoji(context.client, { | |
id, | |
name, | |
animated: isAnimated | |
}); | |
} | |
return emote; | |
} | |
public async resolve(message: Message, command: Command, name: string, value: string): Promise<Emoji> | |
{ | |
const lang: string = await Lang.getLangFromMessage(message); | |
const res: ResourceProxy = Lang.createResourceProxy(lang); | |
const dm: boolean = message.channel.type !== 'text'; | |
const prefix: string = !dm ? await message.guild.storage!.settings.get('prefix') : ''; | |
const usage: string = Lang.getCommandInfo(command, lang).usage.replace(/<prefix>/g, prefix); | |
const emoteRegex: RegExp = this.getEmoteRegex(); | |
const emojiRegex: RegExp = this.getEmojiRegex(); | |
let emote: Emoji | Collection<string, Emoji> = (await this.resolveRaw(value, message))!; | |
if (emoteRegex.test(value) || !emojiRegex.test(value)) | |
{ | |
if (!emote) | |
throw new Error(res.RESOLVE_ERR_RESOLVE_TYPE_ID({ name, arg: value, usage, type: 'Emote' })); | |
} | |
else | |
{ | |
if (emote instanceof Collection) | |
{ | |
if (emote.size > 1) | |
throw String(res.RESOLVE_ERR_MULTIPLE_ROLE_RESULTS({ | |
name, | |
usage, | |
roles: emote.map(r => `\`${r.name}\``).join(', ') | |
})); | |
emote = emote.first()!; | |
} | |
if (!emote) | |
throw new Error(res.RESOLVE_ERR_RESOLVE_TYPE_TEXT({ name, arg: value, usage, type: 'Emote' })); | |
} | |
return emote as Emoji; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment