Now role auto creation on boost can be disabled

Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
This commit is contained in:
Louis Hollingworth 2023-06-22 20:40:29 +01:00
parent 9f53ee668c
commit a37a8a6935
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
4 changed files with 89 additions and 48 deletions

View file

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "guild" ADD COLUMN "auto_create_booster_roles" BOOLEAN NOT NULL DEFAULT true;

View file

@ -9,9 +9,10 @@ datasource db {
} }
model guild { model guild {
id String @id @unique id String @id @unique
name String name String
reports_channel_id String? reports_channel_id String?
auto_create_booster_roles Boolean @default(true)
} }
model member { model member {

View file

@ -133,4 +133,34 @@ export class AdminCmds {
ephemeral: true, 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,
});
}
}
} }

View file

@ -28,70 +28,78 @@ export class MemberEvent {
@On({ event: "guildMemberUpdate" }) @On({ event: "guildMemberUpdate" })
async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) { async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) {
if (oldM.premiumSince !== newM.premiumSince) { const g = await prisma.guild.findUnique({
if (newM != null) { where: {
const m = await prisma.member.findUnique({ id: newM.guild.id,
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);
if (g?.auto_create_booster_roles) {
if (oldM.premiumSince !== newM.premiumSince) {
if (newM != null) {
const m = await prisma.member.findUnique({ const m = await prisma.member.findUnique({
where: { where: {
dgid_duid: { dgid_duid: {
dgid: guild.id, dgid: newM.guild.id,
duid: newM.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({ await prisma.member.update({
where: { where: {
id: m.id, id: m?.id,
}, },
data: { 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 { } else {
await prisma.member.create({ const guild = newM.guild;
data: {
duid: newM.id, const role = await guild.roles.create({
dgid: guild.id, name: newM.nickname ?? newM.displayName,
booster_role_id: role.id, reason: `${newM.nickname ?? newM.displayName} just boosted!`,
name: newM.displayName, });
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;
} }
} }
} }