From 7a1cf06715e7ab2be41850d0a36bb4db24650b69 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Mon, 27 Feb 2023 18:03:38 +0000 Subject: [PATCH] feat(arabot): add basic group creation --- src/commands/outreach/outreach.ts | 38 +++++++++++++++++-- src/utils/database/outreach.ts | 63 ++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/commands/outreach/outreach.ts b/src/commands/outreach/outreach.ts index f60a8f3..9f76d33 100644 --- a/src/commands/outreach/outreach.ts +++ b/src/commands/outreach/outreach.ts @@ -20,7 +20,12 @@ import { Subcommand } from '@sapphire/plugin-subcommands'; import { RegisterBehavior } from '@sapphire/framework'; import { updateUser } from '#utils/database/dbExistingUser'; -import { createEvent } from '#utils/database/outreach'; +import { + checkActiveEvent, + createEvent, + createStat, + getCurrentEvent, +} from '#utils/database/outreach'; export class OutreachCommand extends Subcommand { public constructor(context: Subcommand.Context, options: Subcommand.Options) { @@ -109,7 +114,6 @@ export class OutreachCommand extends Subcommand { if (modInteraction === null || guild === null) { await interaction.reply({ content: 'Mod or guild was not found!', - fetchReply: true, ephemeral: true, }); return; @@ -120,7 +124,14 @@ export class OutreachCommand extends Subcommand { if (mod === undefined) { await interaction.reply({ content: 'Mod was not found!', - fetchReply: true, + ephemeral: true, + }); + return; + } + + if (await checkActiveEvent()) { + await interaction.reply({ + content: 'There is already an active event!', ephemeral: true, }); return; @@ -130,4 +141,25 @@ export class OutreachCommand extends Subcommand { await createEvent(modInteraction.user.id); } + + public async groupCreate(interaction: Subcommand.ChatInputCommandInteraction) { + const leader = interaction.options.getUser('leader', true); + + const event = await getCurrentEvent(); + + if (event === null) { + await interaction.reply({ + content: 'There is no current event!', + ephemeral: true, + }); + return; + } + + await createStat(event.id, leader.id); + + await interaction.reply({ + content: `Created a group with the leader being ${leader}`, + ephemeral: true, + }); + } } diff --git a/src/utils/database/outreach.ts b/src/utils/database/outreach.ts index 6f21cd7..07a2f6b 100644 --- a/src/utils/database/outreach.ts +++ b/src/utils/database/outreach.ts @@ -1,11 +1,12 @@ import { container } from '@sapphire/framework'; import type { Snowflake } from 'discord.js'; +// Events export async function createEvent( modId: Snowflake, ) { // Add the user to the database - await container.database.event.create({ + const event = await container.database.event.create({ data: { leader: { connect: { @@ -19,8 +20,68 @@ export async function createEvent( }, }, }); + + return event.id; } +export async function checkActiveEvent() { + const event = await container.database.event.findFirst({ + where: { + endTime: null, + }, + }); + + return event !== null; +} + +export async function getCurrentEvent() { + const event = await container.database.event.findFirst({ + where: { + endTime: null, + }, + }); + + return event; +} + +// Stats +export async function addStatUser(statId: number, userId: Snowflake) { + await container.database.participantStat.create({ + data: { + stat: { + connect: { + id: statId, + }, + }, + user: { + connect: { + id: userId, + }, + }, + }, + }); +} + +export async function createStat(eventId: number, leaderId: Snowflake) { + const stat = await container.database.stat.create({ + data: { + event: { + connect: { + id: eventId, + }, + }, + leader: { + connect: { + id: leaderId, + }, + }, + }, + }); + + await addStatUser(stat.id, leaderId); +} + +// Misc export async function countTypes() { const count = await container.database.eventType.count(); return count;