Skip to content

Commit

Permalink
Merge pull request #215 from Discord-InterChat/d-branch-2
Browse files Browse the repository at this point in the history
refactor: `HubService()` class
  • Loading branch information
dev-737 authored Nov 18, 2024
2 parents 682ed95 + 6ab6b96 commit 6b52799
Show file tree
Hide file tree
Showing 25 changed files with 473 additions and 218 deletions.
12 changes: 6 additions & 6 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ hub:
invalidName: '{emoji} Invalid hub name. It must not contain `discord`, `clyde` or \`\`\` . Please choose another name.'
nameTaken: '{emoji} This hub name is already taken. Please choose another name.'
success: |
### Hub Created!
Congratulations! Your private hub, **{name}**, has been successfully created.
### Your __private__ hub, **{name}**, has been successfully created.
To join, create an invite using `/hub invite create` and share the generated code. Then join using `/hub join`.
- **Edit hub:** `/hub edit`
- **Generate invite:** `/hub invite create`
- **Make private:** `/hub visibility`
- **Make public:** `/hub visibility`
- **Join hub:** `/hub join`
- **Edit hub:** `/hub edit`
- **Set report channel:** `/hub logging set_channe;`
- **Add moderators:** `/hub moderator add`
Learn more about hubs in our [guide]({docs_link}). Join the [support server]({support_invite}) for help.
Expand Down Expand Up @@ -211,7 +211,7 @@ hub:
list:
title: '**Invite Codes:**'
noInvites: '{emoji} This hub has no invites yet. Use `/hub invite create` to create one.'
notPrivate: '{emoji} Only private hubs can have invites. Use `/hub manage` to make this hub private.'
notPrivate: '{emoji} Only private hubs can have invites. Use `/hub edit` to make this hub private.'
joined:
noJoinedHubs: '{emoji} This server has not joined any hubs yet. Use `/hub browse` to view a list of hubs.'
joinedHubs: This server is a part of **{total}** hub(s). Use `/hub leave` to leave a hub.
Expand Down Expand Up @@ -331,7 +331,7 @@ errors:
**Error ID:**
```{errorId}```
mustVote: Please [vote](https://top.gg/bot/769921109209907241/vote) for InterChat to use this command, your support is very much appreciated!
inviteLinks: '{emoji} You may not send invite links to this hub. Set an invite in `/connection` instead! Hub mods can configure this using `/hub manage settings`'
inviteLinks: '{emoji} You may not send invite links to this hub. Set an invite in `/connection` instead! Hub mods can configure this using `/hub edit settings`'
invalidLangCode: '{emoji} Invalid language code. Please make sure you have entered a correct [language code](https://cloud.google.com/translate/docs/languages).'
unknownServer: '{emoji} Unknown server. Please make sure you have entered the correct **Server ID**.'
unknownNetworkMessage: '{emoji} Unknown Message. If it has been sent in the past minute, please wait few more seconds and try again.'
Expand Down
7 changes: 4 additions & 3 deletions src/commands/context-menu/editMsg.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable complexity */
import Constants, { ConnectionMode, emojis } from '#utils/Constants.js';
import BaseCommand from '#main/core/BaseCommand.js';
import { RegisterInteractionHandler } from '#main/decorators/RegisterInteractionHandler.js';
import HubSettingsManager from '#main/managers/HubSettingsManager.js';
import { SerializedHubSettings } from '#main/modules/BitFields.js';
import VoteBasedLimiter from '#main/modules/VoteBasedLimiter.js';
import { fetchHub } from '#main/utils/hub/utils.js';
import { HubService } from '#main/services/HubService.js';
import {
findOriginalMessage,
getBroadcast,
getBroadcasts,
getOriginalMessage,
} from '#main/utils/network/messageUtils.js';
import Constants, { ConnectionMode, emojis } from '#utils/Constants.js';
import { CustomID } from '#utils/CustomID.js';
import db from '#utils/Db.js';
import { getAttachmentURL } from '#utils/ImageUtils.js';
Expand Down Expand Up @@ -132,7 +132,8 @@ export default class EditMessage extends BaseCommand {
}

// Fetch the hub information
const hub = await fetchHub(originalMsgData.hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(originalMsgData.hubId);
if (!hub) {
await interaction.editReply(
t('errors.unknownNetworkMessage', await this.getLocale(interaction), {
Expand Down
6 changes: 4 additions & 2 deletions src/commands/context-menu/messageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import BaseCommand from '#main/core/BaseCommand.js';
import { RegisterInteractionHandler } from '#main/decorators/RegisterInteractionHandler.js';
import { modPanelButton } from '#main/interactions/ShowModPanel.js';
import HubLogManager from '#main/managers/HubLogManager.js';
import { HubService } from '#main/services/HubService.js';
import { findOriginalMessage, getOriginalMessage } from '#main/utils/network/messageUtils.js';
import type { RemoveMethods } from '#types/CustomClientProps.d.ts';
import { greyOutButton, greyOutButtons } from '#utils/ComponentUtils.js';
Expand All @@ -11,7 +12,7 @@ import { CustomID } from '#utils/CustomID.js';
import db from '#utils/Db.js';
import { InfoEmbed } from '#utils/EmbedUtils.js';
import { sendHubReport } from '#utils/hub/logger/Report.js';
import { fetchHub, isStaffOrHubMod } from '#utils/hub/utils.js';
import { isStaffOrHubMod } from '#utils/hub/utils.js';
import { supportedLocaleCodes, t } from '#utils/Locale.js';
import type { connectedList, Hub } from '@prisma/client';
import {
Expand Down Expand Up @@ -336,7 +337,8 @@ export default class MessageInfo extends BaseCommand {

// utils
private async fetchHub(hubId: string | undefined) {
return hubId ? await fetchHub(hubId) : null;
const hubService = new HubService(db);
return hubId ? await hubService.fetchHub(hubId) : null;
}

private async getMessageInfo(interaction: MessageContextMenuCommandInteraction) {
Expand Down
7 changes: 5 additions & 2 deletions src/commands/context-menu/modActions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import BaseCommand from '#main/core/BaseCommand.js';
import { buildModPanel } from '#main/interactions/ModPanel.js';
import { HubService } from '#main/services/HubService.js';
import db from '#main/utils/Db.js';
import {
findOriginalMessage,
getOriginalMessage,
OriginalMessage,
} from '#main/utils/network/messageUtils.js';
import { fetchHub, isStaffOrHubMod } from '#utils/hub/utils.js';
import { isStaffOrHubMod } from '#utils/hub/utils.js';
import { t, type supportedLocaleCodes } from '#utils/Locale.js';
import {
ApplicationCommandType,
Expand Down Expand Up @@ -52,7 +54,8 @@ export default class BlacklistCtxMenu extends BaseCommand {
originalMsg: OriginalMessage,
locale: supportedLocaleCodes,
) {
const hub = await fetchHub(originalMsg.hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(originalMsg.hubId);
if (!hub || !isStaffOrHubMod(interaction.user.id, hub)) {
await this.replyEmbed(interaction, t('errors.messageNotSentOrExpired', locale), {
ephemeral: true,
Expand Down
20 changes: 13 additions & 7 deletions src/commands/prefix/deleteMsg.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { emojis } from '#utils/Constants.js';
import BasePrefixCommand, { CommandData } from '#main/core/BasePrefixCommand.js';
import { fetchHub, isStaffOrHubMod } from '#main/utils/hub/utils.js';
import { isStaffOrHubMod } from '#main/utils/hub/utils.js';
import { deleteMessageFromHub } from '#main/utils/moderation/deleteMessage.js';
import {
findOriginalMessage,
getBroadcasts,
getMessageIdFromStr,
getOriginalMessage,
} from '#main/utils/network/messageUtils.js';
import { Message } from 'discord.js';
import { EmbedBuilder, Message } from 'discord.js';
import { HubService } from '#main/services/HubService.js';
import db from '#main/utils/Db.js';

export default class DeleteMsgCommand extends BasePrefixCommand {
public readonly data: CommandData = {
Expand All @@ -34,13 +36,17 @@ export default class DeleteMsgCommand extends BasePrefixCommand {
return;
}

const hub = await fetchHub(originalMsg.hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(originalMsg.hubId);
if (
!hub ||
!isStaffOrHubMod(message.author.id, hub) ||
originalMsg.authorId !== message.author.id
!hub || // Check if the hub exists
!isStaffOrHubMod(message.author.id, hub) || // Check if the user is a staff or hub mod
originalMsg.authorId !== message.author.id // Only then check if the user is the author of the message
) {
await message.channel.send('You do not have permission to use this command on that message.');
const embed = new EmbedBuilder()
.setColor('Red')
.setDescription(`${emojis.no} You do not have permission to use this command.`);
await message.reply({ embeds: [embed] });
return;
}

Expand Down
21 changes: 14 additions & 7 deletions src/commands/prefix/modpanel.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import BasePrefixCommand, { CommandData } from '#main/core/BasePrefixCommand.js';
import { buildModPanel } from '#main/interactions/ModPanel.js';
import { fetchHub, isStaffOrHubMod } from '#main/utils/hub/utils.js';
import { HubService } from '#main/services/HubService.js';
import { emojis } from '#main/utils/Constants.js';
import db from '#main/utils/Db.js';
import { isStaffOrHubMod } from '#main/utils/hub/utils.js';
import {
findOriginalMessage,
getMessageIdFromStr,
getOriginalMessage,
} from '#main/utils/network/messageUtils.js';
import { Message } from 'discord.js';
import { EmbedBuilder, Message } from 'discord.js';

export default class BlacklistPrefixCommand extends BasePrefixCommand {
public readonly data: CommandData = {
name: 'modpanel',
description: 'Blacklist a user or server from using the bot',
category: 'Moderation',
usage: 'modpanel user ID or server ID `',
usage: 'modpanel ` messageId | messageLink `',
examples: ['mp 123456789012345678', 'mod 123456789012345678'],
aliases: ['bl', 'mp', 'moderate', 'modactions', 'modpanel', 'mod'],
dbPermission: false,
requiredArgs: 1,
requiredArgs: 0,
};

protected async run(message: Message<true>, args: string[]) {
Expand All @@ -27,13 +30,17 @@ export default class BlacklistPrefixCommand extends BasePrefixCommand {
: null;

if (!originalMessage) {
await message.channel.send('Please provide a valid message ID or link.');
await message.reply('Please provide a valid message ID or link.');
return;
}

const hub = await fetchHub(originalMessage.hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(originalMessage.hubId);
if (!hub || !isStaffOrHubMod(message.author.id, hub)) {
await message.channel.send('You do not have permission to use this command.');
const embed = new EmbedBuilder()
.setColor('Red')
.setDescription(`${emojis.no} You do not have permission to use this command.`);
await message.reply({ embeds: [embed] });
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/prefix/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class ReportPrefixCommand extends BasePrefixCommand {
name: 'report',
description: 'Report a message',
category: 'Utility',
usage: 'report ` [message ID or link] ` ` reason ` ',
usage: 'report ` [messageId | messageLink] ` ` reason ` ',
examples: [
'report 123456789012345678',
'report https://discord.com/channels/123456789012345678/123456789012345678/123456789012345678',
Expand Down
6 changes: 4 additions & 2 deletions src/commands/slash/Main/hub/announce.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { emojis } from '#utils/Constants.js';
import db from '#main/utils/Db.js';
import { fetchHub, isHubManager, sendToHub } from '#main/utils/hub/utils.js';
import { isHubManager, sendToHub } from '#main/utils/hub/utils.js';
import {
ActionRowBuilder,
ChatInputCommandInteraction,
Expand All @@ -13,6 +13,7 @@ import {
import HubCommand from './index.js';
import { CustomID } from '#main/utils/CustomID.js';
import { RegisterInteractionHandler } from '#main/decorators/RegisterInteractionHandler.js';
import { HubService } from '#main/services/HubService.js';

export default class AnnounceCommand extends HubCommand {
readonly cooldown = 1 * 60 * 1000;
Expand Down Expand Up @@ -51,7 +52,8 @@ export default class AnnounceCommand extends HubCommand {
await interaction.reply(`${emojis.loading} Sending announcement to all connected servers...`);
const [hubId] = CustomID.parseCustomId(interaction.customId).args;
const announcement = interaction.fields.getTextInputValue('announcement');
const hub = await fetchHub(hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(hubId);

await sendToHub(hubId, {
avatarURL: hub?.iconUrl,
Expand Down
7 changes: 4 additions & 3 deletions src/commands/slash/Main/hub/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { getHubConnections } from '#main/utils/ConnectedListUtils.js';
import { CustomID } from '#main/utils/CustomID.js';
import db from '#main/utils/Db.js';
import { InfoEmbed } from '#main/utils/EmbedUtils.js';
import { fetchHub } from '#main/utils/hub/utils.js';
import { calculateRating, getStars } from '#main/utils/Utils.js';
import { connectedList, Hub } from '@prisma/client';
import { stripIndents } from 'common-tags';
Expand All @@ -21,6 +20,7 @@ import {
EmbedField,
time,
} from 'discord.js';
import { HubService } from '#main/services/HubService.js';

export default class BrowseCommand extends HubCommand {
async execute(interaction: ChatInputCommandInteraction) {
Expand Down Expand Up @@ -71,14 +71,15 @@ export default class BrowseCommand extends HubCommand {
const customId = CustomID.parseCustomId(interaction.customId);
const [hubId] = customId.args;

if (!interaction.memberPermissions.has('ManageMessages')) {
if (!interaction.memberPermissions.has('ManageMessages', true)) {
await interaction.deferUpdate();
return;
}

await interaction.deferReply();

const hub = await fetchHub(hubId);
const hubService = new HubService(db);
const hub = await hubService.fetchHub(hubId);
if (!hub) {
await interaction.reply({ content: 'Hub not found.', ephemeral: true });
return;
Expand Down
Loading

0 comments on commit 6b52799

Please sign in to comment.