Added schecks to prevent creating multiple roles.

Fixes #4 but need to allow feature to be disabled in guild config

Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
This commit is contained in:
Louis Hollingworth 2023-06-22 18:50:21 +01:00
parent 2d4504dbe0
commit 78a8c63c3d
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
2 changed files with 45 additions and 33 deletions

14
.runner
View file

@ -1,14 +0,0 @@
{
"WARNING": "This file is automatically generated by act-runner. Do not edit it manually unless you know what you are doing. Removing this file will cause act runner to re-register as a new runner.",
"id": 1,
"uuid": "174b9b82-0e04-44fe-a2b4-025557d65c60",
"name": "Louiss-MBP.lan",
"token": "e043b8b3e648abb069765370c25f09fbd24aecec",
"address": "https://git.ludoviko.ch/",
"labels": [
"ubuntu-latest:docker://node:16-bullseye",
"ubuntu-22.04:docker://node:16-bullseye",
"ubuntu-20.04:docker://node:16-bullseye",
"ubuntu-18.04:docker://node:16-buster"
]
}

View file

@ -30,42 +30,68 @@ export class MemberEvent {
async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) { async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) {
if (oldM.premiumSince !== newM.premiumSince) { if (oldM.premiumSince !== newM.premiumSince) {
if (newM != null) { if (newM != null) {
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({ 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;
} }
} }
} }