From 8070430976038fed7edb8d24fdbee15d7b4c4d7c Mon Sep 17 00:00:00 2001 From: smyalygames Date: Wed, 1 Mar 2023 04:00:08 +0000 Subject: [PATCH] feat(arabot): add command to add users to outreach group --- src/commands/outreach/outreach.ts | 119 ++++++++++++++++++++++++++++-- src/utils/database/outreach.ts | 42 +++++++++++ src/utils/devIDs.ts | 1 + src/utils/ids.ts | 1 + 4 files changed, 155 insertions(+), 8 deletions(-) diff --git a/src/commands/outreach/outreach.ts b/src/commands/outreach/outreach.ts index e46148b..a3e3239 100644 --- a/src/commands/outreach/outreach.ts +++ b/src/commands/outreach/outreach.ts @@ -19,13 +19,16 @@ import { Subcommand } from '@sapphire/plugin-subcommands'; import { RegisterBehavior } from '@sapphire/framework'; +import type { Snowflake } from 'discord.js'; import { updateUser } from '#utils/database/dbExistingUser'; import { + addStatUser, checkActiveEvent, createEvent, createStat, - getCurrentEvent, getStatGroups, + getCurrentEvent, getStatFromLeader, getStatFromRole, getStatGroups, userInStats, } from '#utils/database/outreach'; +import IDs from '#utils/ids'; export class OutreachCommand extends Subcommand { public constructor(context: Subcommand.Context, options: Subcommand.Options) { @@ -82,12 +85,11 @@ export class OutreachCommand extends Subcommand { .setRequired(true))) .addSubcommand((command) => command.setName('add') .setDescription('Add a person to the group') - .addStringOption((option) => option.setName('group') - .setDescription('Group to add the user to') - .setRequired(true)) - .addStringOption((option) => option.setName('user') + .addUserOption((option) => option.setName('user') .setDescription('User to add to the group') - .setRequired(true))) + .setRequired(true)) + .addRoleOption((option) => option.setName('group') + .setDescription('Group to add the user to'))) .addSubcommand((command) => command.setName('update') .setDescription('Update the statistics for the group') .addIntegerOption((option) => option.setName('vegan') @@ -172,9 +174,110 @@ export class OutreachCommand extends Subcommand { await createStat(event.id, leader.id, role.id); - await interaction.reply({ + 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.`, + }); + return; + } + + await leaderMember.roles.add(role); + + await interaction.editReply({ content: `Created a group with the leader being ${leader}`, - ephemeral: true, + }); + } + + public async groupAdd(interaction: Subcommand.ChatInputCommandInteraction) { + const user = interaction.options.getUser('user', true); + const group = interaction.options.getRole('group'); + const leader = interaction.user; + const { guild } = interaction; + + if (guild === null) { + await interaction.reply({ + content: 'Could not find guild!', + ephemeral: true, + }); + return; + } + + await interaction.deferReply({ ephemeral: true }); + + let statId: number; + let roleId: Snowflake; + + // Find group from role + if (group !== null) { + const [stat] = await Promise.all([getStatFromRole(group.id)]); + + if (stat === null) { + await interaction.editReply({ + content: `Could not find the group for role ${group}`, + }); + return; + } + + const leaderMember = guild.members.cache.get(leader.id); + + if (leaderMember === undefined) { + await interaction.editReply({ + content: 'Could not find your GuildMember in cache!', + }); + return; + } + + if (leader.id !== stat.stat.leaderId + && !leaderMember.roles.cache.has(IDs.roles.staff.outreachCoordinator)) { + await interaction.editReply({ + content: `You are not the leader for ${group}`, + }); + return; + } + + statId = stat.statId; + roleId = stat.roleId; + } else { + // Find group from person who ran the command + const [stat] = await Promise.all([getStatFromLeader(leader.id)]); + + if (stat === null) { + await interaction.editReply({ + content: 'You\'re not a group leader!', + }); + return; + } + + statId = stat.id; + roleId = stat.role[0].roleId; + } + + if (await userInStats(statId, user.id)) { + await interaction.editReply({ + content: `${user} is already in this group!`, + }); + return; + } + + const member = guild.members.cache.get(user.id); + + if (member === undefined) { + await interaction.editReply({ + content: 'Could not fetch the member!', + }); + return; + } + + await updateUser(member); + + await addStatUser(statId, user.id); + + await member.roles.add(roleId); + + await interaction.editReply({ + content: `Added ${user} to the group!`, }); } } diff --git a/src/utils/database/outreach.ts b/src/utils/database/outreach.ts index d11e58a..15f0074 100644 --- a/src/utils/database/outreach.ts +++ b/src/utils/database/outreach.ts @@ -106,6 +106,48 @@ export async function getStatGroups(eventId: number) { return stats; } +export async function getStatFromRole(roleId: Snowflake) { + const group = await container.database.statRole.findFirst({ + where: { + roleId, + }, + include: { stat: true }, + }); + + return group; +} + +export async function getStatFromLeader(leaderId: Snowflake) { + const event = await getCurrentEvent(); + + if (event === null) { + return null; + } + + const group = await container.database.stat.findFirst({ + where: { + leaderId, + eventId: event.id, + }, + include: { role: true }, + }); + + return group; +} + +export async function userInStats(statId: number, userId: Snowflake) { + const stat = await container.database.participantStat.findUnique({ + where: { + statId_userId: { + statId, + userId, + }, + }, + }); + + return stat !== null; +} + // Misc export async function countTypes() { const count = await container.database.eventType.count(); diff --git a/src/utils/devIDs.ts b/src/utils/devIDs.ts index f01ba66..6c322bc 100644 --- a/src/utils/devIDs.ts +++ b/src/utils/devIDs.ts @@ -56,6 +56,7 @@ const devIDs = { mentorCoordinator: '999431675140382809', verifierCoordinator: '999431675140382810', eventCoordinator: '999431675165556817', + outreachCoordinator: '999431675140382807', restricted: '999431675123597407', moderator: '999431675123597408', trialModerator: '999431675123597404', diff --git a/src/utils/ids.ts b/src/utils/ids.ts index 5b1e7af..8f59995 100644 --- a/src/utils/ids.ts +++ b/src/utils/ids.ts @@ -59,6 +59,7 @@ let IDs = { mentorCoordinator: '947905630939807785', verifierCoordinator: '940721280376778822', eventCoordinator: '944732860554817586', + outreachCoordinator: '954804769476730890', restricted: '851624392928264222', moderator: '826157475815489598', trialModerator: '982074555596152904',