From ab56b5192a15c75ac20b655aaa0e2fecb236eef2 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Wed, 1 Mar 2023 13:49:48 +0000 Subject: [PATCH] feat(arabot): add ending outreach event --- src/commands/outreach/outreach.ts | 77 +++++++++++++++++++++++++++---- src/utils/database/outreach.ts | 28 +++++++++-- 2 files changed, 91 insertions(+), 14 deletions(-) diff --git a/src/commands/outreach/outreach.ts b/src/commands/outreach/outreach.ts index ee7d8f6..712f7ec 100644 --- a/src/commands/outreach/outreach.ts +++ b/src/commands/outreach/outreach.ts @@ -25,7 +25,7 @@ import { addStatUser, checkActiveEvent, createEvent, - createStat, + createStat, endEvent, getCurrentEvent, getStatFromLeader, getStatFromRole, @@ -117,10 +117,10 @@ export class OutreachCommand extends Subcommand { public async eventCreate(interaction: Subcommand.ChatInputCommandInteraction) { // const start = interaction.options.getBoolean('start'); - const modInteraction = interaction.member; + const modUser = interaction.user; const { guild } = interaction; - if (modInteraction === null || guild === null) { + if (guild === null) { await interaction.reply({ content: 'Mod or guild was not found!', ephemeral: true, @@ -128,7 +128,7 @@ export class OutreachCommand extends Subcommand { return; } - const mod = guild.members.cache.get(modInteraction.user.id); + const mod = guild.members.cache.get(modUser.id); if (mod === undefined) { await interaction.reply({ @@ -148,7 +148,62 @@ export class OutreachCommand extends Subcommand { await updateUser(mod); - await createEvent(modInteraction.user.id); + await createEvent(modUser.id); + + await interaction.reply({ + content: 'Created the event!', + ephemeral: true, + }); + } + + public async eventEnd(interaction: Subcommand.ChatInputCommandInteraction) { + const modUser = interaction.user; + const { guild } = interaction; + + if (guild === null) { + await interaction.reply({ + content: 'Guild not found!', + ephemeral: true, + }); + return; + } + + const mod = guild.members.cache.get(modUser.id); + + if (mod === undefined) { + await interaction.reply({ + content: 'Your guild member was not found!', + ephemeral: true, + }); + return; + } + + if (mod.roles.cache.has(IDs.roles.staff.outreachCoordinator)) { + await interaction.reply({ + content: 'You need to be an Outreach Coordinator to run this command!', + ephemeral: true, + }); + return; + } + + await interaction.deferReply({ ephemeral: true }); + + const event = await getCurrentEvent(); + + if (event === null) { + await interaction.editReply('There is currently no event!'); + return; + } + + const [stat] = await Promise.all([getStatGroups(event.id)]); + + stat.forEach((group) => { + guild.roles.delete(group.role[0].roleId); + }); + + await endEvent(event.id); + + await interaction.editReply('Event has now ended!'); } public async groupCreate(interaction: Subcommand.ChatInputCommandInteraction) { @@ -177,19 +232,21 @@ export class OutreachCommand extends Subcommand { const statGroups = await getStatGroups(event.id); const groupNo = statGroups.length + 1; - const role = await guild.roles.create({ name: `Outreach Group ${groupNo}` }); - - await createStat(event.id, leader.id, role.id); - const leaderMember = await guild.members.cache.get(leader.id); if (leaderMember === undefined) { await interaction.editReply({ - content: `Created a group with the leader being ${leader}, however could not give the role.`, + content: `Could not find ${leader}'s guild member.`, }); return; } + await updateUser(leaderMember); + + const role = await guild.roles.create({ name: `Outreach Group ${groupNo}` }); + + await createStat(event.id, leader.id, role.id); + await leaderMember.roles.add(role); await interaction.editReply({ diff --git a/src/utils/database/outreach.ts b/src/utils/database/outreach.ts index 64c8b1f..376c589 100644 --- a/src/utils/database/outreach.ts +++ b/src/utils/database/outreach.ts @@ -2,15 +2,13 @@ import { container } from '@sapphire/framework'; import type { Snowflake } from 'discord.js'; // Events -export async function createEvent( - modId: Snowflake, -) { +export async function createEvent(leaderId: Snowflake) { // Add the user to the database const event = await container.database.event.create({ data: { leader: { connect: { - id: modId, + id: leaderId, }, }, type: { @@ -24,6 +22,27 @@ export async function createEvent( return event.id; } +export async function endEvent(eventId: number) { + await container.database.event.update({ + where: { + id: eventId, + }, + data: { + endTime: new Date(), + }, + }); + + await container.database.statRole.deleteMany({ + where: { + stat: { + event: { + id: eventId, + }, + }, + }, + }); +} + export async function checkActiveEvent() { const event = await container.database.event.findFirst({ where: { @@ -139,6 +158,7 @@ export async function getStatGroups(eventId: number) { orderBy: { id: 'asc', }, + include: { role: true }, }); return stats;