-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reaction-roles): Add reaction roles
- Loading branch information
Marco (Valandur)
committed
Dec 1, 2019
1 parent
1b0cd72
commit be67b2e
Showing
11 changed files
with
262 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Cache } from '../../framework/cache/Cache'; | ||
import { ReactionRole } from '../models/ReactionRole'; | ||
|
||
export class ReactionRoleCache extends Cache<ReactionRole[]> { | ||
public async init() { | ||
// TODO | ||
} | ||
|
||
protected async _get(guildId: string): Promise<ReactionRole[]> { | ||
return this.client.db.getReactionRolesForGuild(guildId); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Message, Role } from 'eris'; | ||
|
||
import { IMClient } from '../../../client'; | ||
import { Command, Context } from '../../../framework/commands/Command'; | ||
import { BooleanResolver, RoleResolver, StringResolver } from '../../../framework/resolvers'; | ||
import { CommandGroup, GuildPermission, ManagementCommand } from '../../../types'; | ||
|
||
const THUMBS_UP = '👍'; | ||
const CUSTOM_EMOJI_REGEX = /<:(\w+):(\d+)>/; | ||
|
||
export default class extends Command { | ||
public constructor(client: IMClient) { | ||
super(client, { | ||
name: ManagementCommand.reactionRole, | ||
aliases: ['rr'], | ||
args: [ | ||
{ | ||
name: 'messageId', | ||
resolver: StringResolver, | ||
required: true | ||
}, | ||
{ | ||
name: 'emoji', | ||
resolver: StringResolver, | ||
required: true | ||
}, | ||
{ | ||
name: 'role', | ||
resolver: RoleResolver, | ||
required: false | ||
} | ||
], | ||
flags: [ | ||
{ | ||
name: 'remove', | ||
resolver: BooleanResolver, | ||
short: 'r' | ||
} | ||
], | ||
group: CommandGroup.Other, | ||
botPermissions: [GuildPermission.MANAGE_MESSAGES], | ||
guildOnly: true, | ||
defaultAdminOnly: true | ||
}); | ||
} | ||
|
||
public async action( | ||
message: Message, | ||
[messageId, emoji, role]: [string, string, Role], | ||
{ remove }: { remove: boolean }, | ||
{ t, guild }: Context | ||
): Promise<any> { | ||
const dbMessage = await this.client.db.getMessageById(guild.id, messageId); | ||
|
||
if (!dbMessage) { | ||
return this.sendReply(message, t('cmd.reactionRole.noMessageFoundInDatabase')); | ||
} | ||
|
||
const matches = emoji.match(CUSTOM_EMOJI_REGEX); | ||
const emojiId = matches ? `${matches[1]}:${matches[2]}` : emoji; | ||
|
||
if (remove) { | ||
await this.client.db.removeReactionRole(dbMessage.guildId, dbMessage.channelId, dbMessage.id, emojiId); | ||
await this.client.removeMessageReaction(dbMessage.channelId, dbMessage.id, emojiId); | ||
} else { | ||
const reactionRole = { | ||
guildId: dbMessage.guildId, | ||
channelId: dbMessage.channelId, | ||
messageId: dbMessage.id, | ||
roleId: role.id, | ||
emoji: emojiId | ||
}; | ||
|
||
try { | ||
await this.client.addMessageReaction(dbMessage.channelId, dbMessage.id, emojiId); | ||
await this.client.db.saveReactionRole(reactionRole); | ||
} catch (error) { | ||
if (error.code === 10014) { | ||
await this.sendReply(message, t('cmd.reactionRole.unknownEmoji')); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
this.client.cache.reactionRoles.flush(guild.id); | ||
|
||
await this.client.addMessageReaction(message.channel.id, message.id, THUMBS_UP); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class ReactionRole { | ||
public id: number; | ||
public messageId: string; | ||
public channelId: string; | ||
public guildId: string; | ||
public createdAt: Date; | ||
public updatedAt: Date; | ||
public roleId: string; | ||
public emoji: string; | ||
} |
Oops, something went wrong.