feat(arabot): add ending outreach event

This commit is contained in:
smyalygames
2023-03-01 13:49:48 +00:00
parent dec6de8398
commit ab56b5192a
2 changed files with 91 additions and 14 deletions

View File

@@ -25,7 +25,7 @@ import {
addStatUser, addStatUser,
checkActiveEvent, checkActiveEvent,
createEvent, createEvent,
createStat, createStat, endEvent,
getCurrentEvent, getCurrentEvent,
getStatFromLeader, getStatFromLeader,
getStatFromRole, getStatFromRole,
@@ -117,10 +117,10 @@ export class OutreachCommand extends Subcommand {
public async eventCreate(interaction: Subcommand.ChatInputCommandInteraction) { public async eventCreate(interaction: Subcommand.ChatInputCommandInteraction) {
// const start = interaction.options.getBoolean('start'); // const start = interaction.options.getBoolean('start');
const modInteraction = interaction.member; const modUser = interaction.user;
const { guild } = interaction; const { guild } = interaction;
if (modInteraction === null || guild === null) { if (guild === null) {
await interaction.reply({ await interaction.reply({
content: 'Mod or guild was not found!', content: 'Mod or guild was not found!',
ephemeral: true, ephemeral: true,
@@ -128,7 +128,7 @@ export class OutreachCommand extends Subcommand {
return; return;
} }
const mod = guild.members.cache.get(modInteraction.user.id); const mod = guild.members.cache.get(modUser.id);
if (mod === undefined) { if (mod === undefined) {
await interaction.reply({ await interaction.reply({
@@ -148,7 +148,62 @@ export class OutreachCommand extends Subcommand {
await updateUser(mod); 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) { public async groupCreate(interaction: Subcommand.ChatInputCommandInteraction) {
@@ -177,19 +232,21 @@ export class OutreachCommand extends Subcommand {
const statGroups = await getStatGroups(event.id); const statGroups = await getStatGroups(event.id);
const groupNo = statGroups.length + 1; 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); const leaderMember = await guild.members.cache.get(leader.id);
if (leaderMember === undefined) { if (leaderMember === undefined) {
await interaction.editReply({ 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; 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 leaderMember.roles.add(role);
await interaction.editReply({ await interaction.editReply({

View File

@@ -2,15 +2,13 @@ import { container } from '@sapphire/framework';
import type { Snowflake } from 'discord.js'; import type { Snowflake } from 'discord.js';
// Events // Events
export async function createEvent( export async function createEvent(leaderId: Snowflake) {
modId: Snowflake,
) {
// Add the user to the database // Add the user to the database
const event = await container.database.event.create({ const event = await container.database.event.create({
data: { data: {
leader: { leader: {
connect: { connect: {
id: modId, id: leaderId,
}, },
}, },
type: { type: {
@@ -24,6 +22,27 @@ export async function createEvent(
return event.id; 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() { export async function checkActiveEvent() {
const event = await container.database.event.findFirst({ const event = await container.database.event.findFirst({
where: { where: {
@@ -139,6 +158,7 @@ export async function getStatGroups(eventId: number) {
orderBy: { orderBy: {
id: 'asc', id: 'asc',
}, },
include: { role: true },
}); });
return stats; return stats;