Skip to content

Commit

Permalink
refactor(hub): improve readability of /hub moderator command
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Aug 19, 2024
1 parent 7ad404c commit f37ccb8
Showing 1 changed file with 179 additions and 154 deletions.
333 changes: 179 additions & 154 deletions src/commands/slash/Main/hub/moderator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { emojis } from '#main/utils/Constants.js';
import db from '#main/utils/Db.js';
import { t } from '#main/utils/Locale.js';
import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import Hub from './index.js';
import { emojis } from '#main/utils/Constants.js';
import { supportedLocaleCodes, t } from '#main/utils/Locale.js';
import { hubs } from '@prisma/client';
import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';

export default class Moderator extends Hub {
async execute(interaction: ChatInputCommandInteraction): Promise<void> {
Expand All @@ -29,166 +30,190 @@ export default class Moderator extends Hub {
}

switch (interaction.options.getSubcommand()) {
case 'add': {
const user = interaction.options.getUser('user', true);

if (hub.moderators.find((mod) => mod.userId === user.id)) {
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.add.alreadyModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
{ ephemeral: true },
);
break;
}

const position = interaction.options.getString('position') ?? 'network_mod';
await db.hubs.update({
where: { id: hub.id },
data: { moderators: { push: { userId: user.id, position } } },
});

await interaction.reply({
content: t(
{ phrase: 'hub.moderator.add.success', locale },
{ user: user.toString(), position, emoji: emojis.yes },
),
});
case 'add':
await this.handleAddSubcommand(interaction, hub, locale);
break;
case 'remove':
await this.handleRemoveSubcommand(interaction, hub, locale);
break;
case 'update':
await this.handleUpdateSubcommand(interaction, hub, locale);
break;
case 'list':
await this.handleListSubcommand(interaction, hub, locale);
break;
}

case 'remove': {
const user = interaction.options.getUser('user', true);

if (!hub.moderators.find((mod) => mod.userId === user.id)) {
await interaction.reply({
content: t(
{ phrase: 'hub.moderator.remove.notModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
ephemeral: true,
});
break;
}

const isExecutorOwner = hub.ownerId === interaction.user.id;
const mod = hub.moderators.find((m) => m.userId === user.id);
const userIsManager = mod?.position === 'manager';
default:
break;
}
}
private async handleRemoveSubcommand(
interaction: ChatInputCommandInteraction,
hub: hubs,
locale: supportedLocaleCodes,
) {
const user = interaction.options.getUser('user', true);
if (!hub.moderators.find((mod) => mod.userId === user.id)) {
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.remove.notModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
{ ephemeral: true },
);
return;
}

if (
(userIsManager && !isExecutorOwner) ||
(user.id === interaction.user.id && !isExecutorOwner)
) {
await interaction.reply({
content: t({ phrase: 'hub.moderator.remove.notOwner', locale }, { emoji: emojis.no }),
ephemeral: true,
});
break;
}
const mod = hub.moderators.find((m) => m.userId === user.id);
const userIsManager = mod?.position === 'manager';
const isExecutorOwner = hub.ownerId === interaction.user.id;

await db.hubs.update({
where: { id: hub.id },
data: {
moderators: { deleteMany: { where: { userId: user.id } } },
},
});
/* executor needs to be owner to:
- change position of other managers
- change their own position
*/
if (!isExecutorOwner && (userIsManager || user.id === interaction.user.id)) {
await this.replyEmbed(
interaction,
t({ phrase: 'hub.moderator.remove.notOwner', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
return;
}

await interaction.reply(
t(
{ phrase: 'hub.moderator.remove.success', locale },
{ user: user.toString(), emoji: emojis.yes },
),
);
break;
}
await db.hubs.update({
where: { id: hub.id },
data: {
moderators: { deleteMany: { where: { userId: user.id } } },
},
});

case 'update': {
const user = interaction.options.getUser('user', true);
const position = interaction.options.getString('position', true);
const isUserMod = hub.moderators.find((mod) => mod.userId === user.id);
const isExecutorMod = hub.moderators.find(
(mod) =>
(mod.userId === interaction.user.id && mod.position === 'manager') ||
hub.ownerId === interaction.user.id,
);
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.remove.success', locale },
{ user: user.toString(), emoji: emojis.yes },
),
);
}
private async handleUpdateSubcommand(
interaction: ChatInputCommandInteraction,
hub: hubs,
locale: supportedLocaleCodes,
) {
const user = interaction.options.getUser('user', true);
const position = interaction.options.getString('position', true);
const isUserMod = hub.moderators.find((mod) => mod.userId === user.id);
const isExecutorMod = hub.moderators.find(
(mod) =>
(mod.userId === interaction.user.id && mod.position === 'manager') ||
hub.ownerId === interaction.user.id,
);

if (!isExecutorMod) {
await this.replyEmbed(
interaction,
t({ phrase: 'hub.moderator.update.notAllowed', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
return;
}
else if (!isUserMod) {
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.update.notModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
{ ephemeral: true },
);
return;
}
else if (user.id === interaction.user.id || isUserMod.position === 'manager') {
await this.replyEmbed(
interaction,
t({ phrase: 'hub.moderator.update.notOwner', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
return;
}

if (!isExecutorMod) {
await this.replyEmbed(
interaction,
t({ phrase: 'hub.moderator.update.notAllowed', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
break;
}
else if (!isUserMod) {
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.update.notModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
{ ephemeral: true },
);
break;
}
else if (
(hub.ownerId !== interaction.user.id && user.id === interaction.user.id) ||
isUserMod.position === 'manager'
) {
await this.replyEmbed(
interaction,
t({ phrase: 'hub.moderator.update.notOwner', locale }, { emoji: emojis.no }),
{ ephemeral: true },
);
break;
}
await db.hubs.update({
where: { id: hub.id },
data: {
moderators: {
updateMany: { where: { userId: user.id }, data: { position } },
},
},
});

await db.hubs.update({
where: { id: hub.id },
data: {
moderators: {
updateMany: { where: { userId: user.id }, data: { position } },
},
},
});
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.update.success', locale },
{ user: user.toString(), position, emoji: emojis.yes },
),
);
}
private async handleListSubcommand(
interaction: ChatInputCommandInteraction,
hub: hubs,
locale: supportedLocaleCodes,
) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle('Hub Moderators')
.setDescription(
hub.moderators.length > 0
? hub.moderators
.map(
(mod, index) =>
`${index + 1}. <@${mod.userId}> - ${
mod.position === 'network_mod' ? 'Network Moderator' : 'Hub Manager'
}`,
)
.join('\n')
: t({ phrase: 'hub.moderator.noModerators', locale }, { emoji: emojis.no }),
)
.setColor('Aqua')
.setTimestamp(),
],
ephemeral: true,
});
}
private async handleAddSubcommand(
interaction: ChatInputCommandInteraction,
hub: hubs,
locale: supportedLocaleCodes,
) {
const user = interaction.options.getUser('user', true);

if (hub.moderators.find((mod) => mod.userId === user.id)) {
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.add.alreadyModerator', locale },
{ user: user.toString(), emoji: emojis.no },
),
{ ephemeral: true },
);
return;
}

await interaction.reply(
t(
{ phrase: 'hub.moderator.update.success', locale },
{ user: user.toString(), position, emoji: emojis.yes },
),
);
break;
}
const position = interaction.options.getString('position') ?? 'network_mod';
await db.hubs.update({
where: { id: hub.id },
data: { moderators: { push: { userId: user.id, position } } },
});

case 'list': {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle('Hub Moderators')
.setDescription(
hub.moderators.length > 0
? hub.moderators
.map(
(mod, index) =>
`${index + 1}. <@${mod.userId}> - ${
mod.position === 'network_mod' ? 'Network Moderator' : 'Hub Manager'
}`,
)
.join('\n')
: t({ phrase: 'hub.moderator.noModerators', locale }, { emoji: emojis.no }),
)
.setColor('Aqua')
.setTimestamp(),
],
ephemeral: true,
});
break;
}
default:
break;
}
await this.replyEmbed(
interaction,
t(
{ phrase: 'hub.moderator.add.success', locale },
{ user: user.toString(), position, emoji: emojis.yes },
),
);
}
}

0 comments on commit f37ccb8

Please sign in to comment.