mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-19 05:44:16 +02:00
feat(arabot): add command to add users to outreach group
This commit is contained in:
parent
da8088e157
commit
8070430976
@ -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!`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -56,6 +56,7 @@ const devIDs = {
|
||||
mentorCoordinator: '999431675140382809',
|
||||
verifierCoordinator: '999431675140382810',
|
||||
eventCoordinator: '999431675165556817',
|
||||
outreachCoordinator: '999431675140382807',
|
||||
restricted: '999431675123597407',
|
||||
moderator: '999431675123597408',
|
||||
trialModerator: '999431675123597404',
|
||||
|
@ -59,6 +59,7 @@ let IDs = {
|
||||
mentorCoordinator: '947905630939807785',
|
||||
verifierCoordinator: '940721280376778822',
|
||||
eventCoordinator: '944732860554817586',
|
||||
outreachCoordinator: '954804769476730890',
|
||||
restricted: '851624392928264222',
|
||||
moderator: '826157475815489598',
|
||||
trialModerator: '982074555596152904',
|
||||
|
Loading…
x
Reference in New Issue
Block a user