(#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 <louis@hollingworth.ch>
This commit is contained in:
Louis Hollingworth 2023-06-18 14:46:21 +01:00
parent 530e99a7d0
commit 17d8746767
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
2 changed files with 77 additions and 0 deletions

View file

@ -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"

View file

@ -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,
},
});
}
}
}
}
}