Add --exclusive/-e to !reaction_roles

When reaction roles are set as exclusive, a user can only have 1
reaction role from that message. Others are removed automatically when
picking a role if needed.
This commit is contained in:
Dragory 2019-11-30 23:39:29 +02:00
parent 546835d421
commit d2a6cb1684
4 changed files with 42 additions and 4 deletions

View file

@ -50,13 +50,14 @@ export class GuildReactionRoles extends BaseGuildRepository {
await this.reactionRoles.delete(criteria);
}
async add(channelId: string, messageId: string, emoji: string, roleId: string) {
async add(channelId: string, messageId: string, emoji: string, roleId: string, exclusive?: boolean) {
await this.reactionRoles.insert({
guild_id: this.guildId,
channel_id: channelId,
message_id: messageId,
emoji,
role_id: roleId,
is_exclusive: Boolean(exclusive),
});
}
}

View file

@ -19,4 +19,6 @@ export class ReactionRole {
emoji: string;
@Column() role_id: string;
@Column() is_exclusive: boolean;
}

View file

@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";
export class AddIsExclusiveToReactionRoles1575145703039 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumn(
"reaction_roles",
new TableColumn({
name: "is_exclusive",
type: "tinyint",
unsigned: true,
default: 0,
}),
);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropColumn("reaction_roles", "is_exclusive");
}
}

View file

@ -268,9 +268,17 @@ export class ReactionRolesPlugin extends ZeppelinPlugin<TConfigSchema> {
* :zep_twitch: = 473086848831455234
* :zep_ps4: = 543184300250759188
*/
@d.command("reaction_roles", "<messageId:string> <reactionRolePairs:string$>")
@d.command("reaction_roles", "<messageId:string> <reactionRolePairs:string$>", {
options: [
{
name: "exclusive",
shortcut: "e",
isSwitch: true,
},
],
})
@d.permission("can_manage")
async reactionRolesCmd(msg: Message, args: { messageId: string; reactionRolePairs: string }) {
async reactionRolesCmd(msg: Message, args: { messageId: string; reactionRolePairs: string; exclusive?: boolean }) {
const savedMessage = await this.savedMessages.find(args.messageId);
if (!savedMessage) {
msg.channel.createMessage(errorMessage("Unknown message"));
@ -331,7 +339,7 @@ export class ReactionRolesPlugin extends ZeppelinPlugin<TConfigSchema> {
// Save the new reaction roles to the database
for (const pair of emojiRolePairs) {
await this.reactionRoles.add(channel.id, targetMessage.id, pair[0], pair[1]);
await this.reactionRoles.add(channel.id, targetMessage.id, pair[0], pair[1], args.exclusive);
}
// Apply the reactions themselves
@ -370,6 +378,14 @@ export class ReactionRolesPlugin extends ZeppelinPlugin<TConfigSchema> {
const matchingReactionRole = await this.reactionRoles.getByMessageAndEmoji(msg.id, emoji.id || emoji.name);
if (!matchingReactionRole) return;
// If the reaction role is exclusive, remove any other roles in the message first
if (matchingReactionRole.is_exclusive) {
const messageReactionRoles = await this.reactionRoles.getForMessage(msg.id);
for (const reactionRole of messageReactionRoles) {
this.addMemberPendingRoleChange(userId, "-", reactionRole.role_id);
}
}
this.addMemberPendingRoleChange(userId, "+", matchingReactionRole.role_id);
}