diff --git a/prisma/migrations/20230622181924_allow_guild_perk_role_disable/migration.sql b/prisma/migrations/20230622181924_allow_guild_perk_role_disable/migration.sql new file mode 100644 index 0000000..df90ef8 --- /dev/null +++ b/prisma/migrations/20230622181924_allow_guild_perk_role_disable/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "guild" ADD COLUMN "auto_create_booster_roles" BOOLEAN NOT NULL DEFAULT true; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5463950..20920dd 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -9,9 +9,10 @@ datasource db { } model guild { - id String @id @unique - name String - reports_channel_id String? + id String @id @unique + name String + reports_channel_id String? + auto_create_booster_roles Boolean @default(true) } model member { diff --git a/src/commands/admin.ts b/src/commands/admin.ts index 49cdfa4..c130f31 100644 --- a/src/commands/admin.ts +++ b/src/commands/admin.ts @@ -133,4 +133,34 @@ export class AdminCmds { ephemeral: true, }); } + + @Slash({ + description: "Enable or disable auto perk roles", + defaultMemberPermissions: PermissionsBitField.Flags.Administrator, + }) + async toggle_perk_role_creation(interaction: CommandInteraction) { + if (interaction.guildId) { + const g = await prisma.guild.findUnique({ + where: { + id: interaction.guildId, + }, + }); + + await prisma.guild.update({ + where: { + id: interaction.guildId, + }, + data: { + auto_create_booster_roles: !g?.auto_create_booster_roles, + }, + }); + + interaction.reply({ + content: `Booster role creation is now ${ + g?.auto_create_booster_roles ? "disabled" : "enabled" + }`, + ephemeral: true, + }); + } + } } diff --git a/src/events/member_event.ts b/src/events/member_event.ts index 29cd5be..8ef45f6 100644 --- a/src/events/member_event.ts +++ b/src/events/member_event.ts @@ -28,70 +28,78 @@ export class MemberEvent { @On({ event: "guildMemberUpdate" }) async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) { - if (oldM.premiumSince !== newM.premiumSince) { - if (newM != null) { - const m = await prisma.member.findUnique({ - where: { - dgid_duid: { - dgid: newM.guild.id, - duid: newM.id, - }, - }, - }); - - if (m?.booster_role_id != null && newM.premiumSince != null) { - return; - } else if (newM.premiumSince == null) { - await prisma.member.update({ - where: { - id: m?.id, - }, - data: { - booster_role_id: null, - }, - }); - const r = await newM.guild.roles.fetch(m!.booster_role_id!); - if (r) await newM.guild.roles.delete(r); - return; - } else { - const guild = newM.guild; - - const role = await guild.roles.create({ - name: newM.nickname ?? newM.displayName, - reason: `${newM.nickname ?? newM.displayName} just boosted!`, - }); - - await newM.roles.add(role); + const g = await prisma.guild.findUnique({ + where: { + id: newM.guild.id, + }, + }); + if (g?.auto_create_booster_roles) { + if (oldM.premiumSince !== newM.premiumSince) { + if (newM != null) { const m = await prisma.member.findUnique({ where: { dgid_duid: { - dgid: guild.id, + dgid: newM.guild.id, duid: newM.id, }, }, }); - if (m) { + if (m?.booster_role_id != null && newM.premiumSince != null) { + return; + } else if (newM.premiumSince == null) { await prisma.member.update({ where: { - id: m.id, + id: m?.id, }, data: { - booster_role_id: role.id, + booster_role_id: null, }, }); + const r = await newM.guild.roles.fetch(m!.booster_role_id!); + if (r) await newM.guild.roles.delete(r); + return; } else { - await prisma.member.create({ - data: { - duid: newM.id, - dgid: guild.id, - booster_role_id: role.id, - name: newM.displayName, + const guild = newM.guild; + + const role = await guild.roles.create({ + name: newM.nickname ?? newM.displayName, + reason: `${newM.nickname ?? newM.displayName} just boosted!`, + }); + + await newM.roles.add(role); + + const m = await prisma.member.findUnique({ + where: { + dgid_duid: { + dgid: guild.id, + duid: newM.id, + }, }, }); + + if (m) { + await prisma.member.update({ + where: { + id: m.id, + }, + data: { + booster_role_id: role.id, + }, + }); + } else { + await prisma.member.create({ + data: { + duid: newM.id, + dgid: guild.id, + booster_role_id: role.id, + name: newM.displayName, + }, + }); + } + return; } - return; } } }