From 17d8746767edb5502bbd190e12a982ea9de96f57 Mon Sep 17 00:00:00 2001 From: Louis Hollingworth Date: Sun, 18 Jun 2023 14:46:21 +0100 Subject: [PATCH] (#2) Members now created in db on join and leave. Boosters now have a role created for them and added to their profile. Signed-off-by: Louis Hollingworth --- Dockerfile | 4 +++ src/events/member_event.ts | 73 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/events/member_event.ts diff --git a/Dockerfile b/Dockerfile index 566555f..f0dc8ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,3 +34,7 @@ COPY --from=build-runner /tmp/app/build /app/build # Start bot CMD [ "npm", "run", "start" ] + +LABEL org.opencontainers.image.source=https://git.ludoviko.ch/lucxjo/er +LABEL org.opencontainers.image.description="A Discord bot for the Young Royals guild" +LABEL org.opencontainers.image.licenses="GPL-3.0-or-later" diff --git a/src/events/member_event.ts b/src/events/member_event.ts new file mode 100644 index 0000000..bd85a0e --- /dev/null +++ b/src/events/member_event.ts @@ -0,0 +1,73 @@ +import { ArgsOf, Discord, On } from "discordx"; +import { prisma } from "../main.js"; + +@Discord() +export class MemberEvent { + @On({ event: "guildMemberAdd" }) + async memberJoin([member]: ArgsOf<"guildMemberAdd">) { + await prisma.member.create({ + data: { + dgid: member.guild.id, + duid: member.id, + name: member.displayName, + }, + }); + } + + @On({ event: "guildMemberRemove" }) + async memberRemove([member]: ArgsOf<"guildMemberRemove">) { + await prisma.member.delete({ + where: { + dgid_duid: { + duid: member.id, + dgid: member.guild.id, + }, + }, + }); + } + + @On({ event: "guildMemberUpdate" }) + async memberUpdate([oldM, newM]: ArgsOf<"guildMemberUpdate">) { + if (oldM.premiumSince !== newM.premiumSince) { + 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({ + 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, + }, + }); + } + } + } + } +}