Use fancier typings in ReactionRolesPlugin

This commit is contained in:
Dragory 2018-08-03 19:26:27 +03:00
parent 32856329b5
commit 1b151fff24

View file

@ -3,7 +3,7 @@ import { errorMessage, isSnowflake } from "../utils";
import { GuildReactionRoles } from "../data/GuildReactionRoles"; import { GuildReactionRoles } from "../data/GuildReactionRoles";
import { Channel, Emoji, Message, TextChannel } from "eris"; import { Channel, Emoji, Message, TextChannel } from "eris";
type ReactionRolePair = string[]; type ReactionRolePair = [string, string];
type CustomEmoji = { type CustomEmoji = {
id: string; id: string;
@ -86,14 +86,16 @@ export class ReactionRolesPlugin extends Plugin {
.trim() .trim()
.split("\n") .split("\n")
.map(v => v.split("=").map(v => v.trim())) .map(v => v.split("=").map(v => v.trim()))
.map(pair => { .map(
const customEmojiMatch = pair[0].match(/^<:(?:.*?):(\d+)>$/); (pair): ReactionRolePair => {
if (customEmojiMatch) { const customEmojiMatch = pair[0].match(/^<:(?:.*?):(\d+)>$/);
return [customEmojiMatch[1], pair[1]]; if (customEmojiMatch) {
} else { return [customEmojiMatch[1], pair[1]];
return pair; } else {
return pair as ReactionRolePair;
}
} }
}); );
// Verify the specified emojis and roles are valid // Verify the specified emojis and roles are valid
for (const pair of newRolePairs) { for (const pair of newRolePairs) {
@ -111,7 +113,9 @@ export class ReactionRolesPlugin extends Plugin {
} }
const oldReactionRoles = await this.reactionRoles.getForMessage(targetMessage.id); const oldReactionRoles = await this.reactionRoles.getForMessage(targetMessage.id);
const oldRolePairs: ReactionRolePair[] = oldReactionRoles.map(r => [r.emoji, r.role_id]); const oldRolePairs: ReactionRolePair[] = oldReactionRoles.map(
r => [r.emoji, r.role_id] as ReactionRolePair
);
// Remove old reaction/role pairs that weren't included in the new pairs or were changed in some way // Remove old reaction/role pairs that weren't included in the new pairs or were changed in some way
const toRemove = oldRolePairs.filter( const toRemove = oldRolePairs.filter(