Skip to content

Commit

Permalink
refactor: split hub logger into multiple subclasses & send emoji thru…
Browse files Browse the repository at this point in the history
… objects instead for locales
  • Loading branch information
dev-737 committed Jan 21, 2024
1 parent 2df1795 commit 9cbd2bb
Show file tree
Hide file tree
Showing 30 changed files with 752 additions and 493 deletions.
2 changes: 1 addition & 1 deletion locales
3 changes: 1 addition & 2 deletions src/InterChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from 'disc
import { stripIndents } from 'common-tags';
import { LINKS, channels, colors, emojis } from './utils/Constants.js';
import { initI18n } from './utils/Locale.js';
import HubLogsManager from './managers/HubLogsManager.js';
import { logGuildJoin, logGuildLeave } from './scripts/guilds/goals.js';
import getWelcomeTargets from './scripts/guilds/getWelcomeTarget.js';

Expand Down Expand Up @@ -117,7 +116,7 @@ class InterChat extends SuperClient {

// send server leave log to hubs
connections.forEach((connection) =>
new HubLogsManager(connection.hubId).logServerLeave(guild),
this.joinLeaveLogger.logServerLeave(connection.hubId, guild),
);

await logGuildLeave(guild, channels.goal);
Expand Down
9 changes: 7 additions & 2 deletions src/SuperClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'discord.js';
import { ClusterClient, getInfo } from 'discord-hybrid-sharding';
import { commandsMap, interactionsMap } from './commands/BaseCommand.js';
import db from './utils/Db.js';
import Sentry from '@sentry/node';
import Scheduler from './services/SchedulerService.js';
import NSFWClient from './utils/NSFWDetection.js';
Expand All @@ -21,8 +22,8 @@ import BlacklistManager from './managers/BlacklistManager.js';
import { RemoveMethods } from './typings/index.js';
import { isDevBuild } from './utils/Constants.js';
import { ActivityType } from 'discord.js';
import { JoinLeaveLogger, ModLogsLogger, ProfanityLogger, ReportLogger } from './services/HubLoggerService.js';
import 'dotenv/config';
import db from './utils/Db.js';

export default abstract class SuperClient extends Client {
readonly description = 'The only cross-server chatting bot you\'ll ever need.';
Expand All @@ -41,6 +42,10 @@ export default abstract class SuperClient extends Client {
private readonly blacklistManager = new BlacklistManager(this.scheduler);
private readonly reactionUpdater = new ReactionUpdater(this);
private readonly nsfwDetector = new NSFWClient();
public readonly reportLogger = new ReportLogger(this);
public readonly profanityLogger = new ProfanityLogger(this);
public readonly modLogsLogger = new ModLogsLogger(this);
public readonly joinLeaveLogger = new JoinLeaveLogger(this);

private static self: SuperClient;

Expand Down Expand Up @@ -76,7 +81,7 @@ export default abstract class SuperClient extends Client {
status: 'idle',
activities: [
{
state: 'Watching over 500+ networks | /hub browse',
state: 'Watching over 100+ cross-server hubs',
name: 'custom',
type: ActivityType.Custom,
},
Expand Down
30 changes: 21 additions & 9 deletions src/commands/context-menu/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import db from '../../utils/Db.js';
import parse from 'parse-duration';
import BaseCommand from '../BaseCommand.js';
import HubLogsManager from '../../managers/HubLogsManager.js';
import { t } from '../../utils/Locale.js';
import { colors, emojis } from '../../utils/Constants.js';
import { CustomID } from '../../utils/CustomID.js';
Expand Down Expand Up @@ -97,7 +96,12 @@ export default class Blacklist extends BaseCommand {
if (interaction.user.id !== customId.args[0]) {
await interaction.reply({
embeds: [
simpleEmbed(t({ phrase: 'errors.notYourAction', locale: interaction.user.locale })),
simpleEmbed(
t(
{ phrase: 'errors.notYourAction', locale: interaction.user.locale },
{ emoji: emojis.no },
),
),
],
ephemeral: true,
});
Expand Down Expand Up @@ -157,7 +161,10 @@ export default class Blacklist extends BaseCommand {

if (!originalMsg?.hubId) {
await interaction.editReply(
t({ phrase: 'errors.networkMessageExpired', locale: interaction.user.locale }),
t(
{ phrase: 'errors.networkMessageExpired', locale: interaction.user.locale },
{ emoji: emojis.no },
),
);
return;
}
Expand Down Expand Up @@ -206,12 +213,12 @@ export default class Blacklist extends BaseCommand {
.notifyBlacklist('user', originalMsg.authorId, originalMsg.hubId, expires, reason)
.catch(() => null);

await new HubLogsManager(originalMsg.hubId).logBlacklist(
user,
interaction.user,
await interaction.client.modLogsLogger.logBlacklist(originalMsg.hubId, {
userOrServer: user,
mod: interaction.user,
reason,
expires,
);
});
}

await interaction.editReply({ embeds: [successEmbed], components: [] });
Expand Down Expand Up @@ -259,8 +266,13 @@ export default class Blacklist extends BaseCommand {
});

if (server) {
await new HubLogsManager(originalMsg.hubId)
.logBlacklist(server, interaction.user, reason, expires)
await interaction.client.modLogsLogger
.logBlacklist(originalMsg.hubId, {
userOrServer: server,
mod: interaction.user,
reason,
expires,
})
.catch(() => null);
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/context-menu/deleteMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class DeleteMessage extends BaseCommand {
t({
phrase: 'errors.unknownNetworkMessage',
locale: interaction.user.locale,
}),
}, { emoji: emojis.no }),
);
}

Expand All @@ -45,7 +45,7 @@ export default class DeleteMessage extends BaseCommand {
t({
phrase: 'errors.notMessageAuthor',
locale: interaction.user.locale,
}),
}, { emoji: emojis.no }),
);
}

Expand Down
33 changes: 24 additions & 9 deletions src/commands/context-menu/editMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { censor } from '../../utils/Profanity.js';
import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { CustomID } from '../../utils/CustomID.js';
import { t } from '../../utils/Locale.js';
import { emojis } from '../../utils/Constants.js';

export default class EditMessage extends BaseCommand {
readonly data: RESTPostAPIApplicationCommandsJSONBody = {
Expand All @@ -31,7 +32,10 @@ export default class EditMessage extends BaseCommand {

if (!checkIfStaff(interaction.user.id) && !(await hasVoted(interaction.user.id))) {
await interaction.reply({
content: t({ phrase: 'errors.mustVote', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.mustVote', locale: interaction.user.locale },
{ emoji: emojis.no },
),
ephemeral: true,
});
return;
Expand All @@ -45,18 +49,24 @@ export default class EditMessage extends BaseCommand {
if (!messageInDb?.originalMsg) {
await interaction.reply({
content: t(
t({
phrase: 'errors.unknownNetworkMessage',
locale: interaction.user.locale,
}),
t(
{
phrase: 'errors.unknownNetworkMessage',
locale: interaction.user.locale,
},
{ emoji: emojis.no },
),
),
ephemeral: true,
});
return;
}
else if (interaction.user.id != messageInDb?.originalMsg.authorId) {
await interaction.reply({
content: t({ phrase: 'errors.notMessageAuthor', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.notMessageAuthor', locale: interaction.user.locale },
{ emoji: emojis.no },
),
ephemeral: true,
});
return;
Expand Down Expand Up @@ -93,7 +103,9 @@ export default class EditMessage extends BaseCommand {

const target = await interaction.channel?.messages.fetch(messageId).catch(() => null);
if (!target) {
return await interaction.reply(t({ phrase: 'errors.unknownNetworkMessage' }));
return await interaction.reply(
t({ phrase: 'errors.unknownNetworkMessage' }, { emoji: emojis.no }),
);
}

const messageInDb = await db.broadcastedMessages.findFirst({
Expand All @@ -103,7 +115,10 @@ export default class EditMessage extends BaseCommand {

if (!messageInDb?.originalMsg.hub) {
return await interaction.reply(
t({ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale }),
t(
{ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale },
{ emoji: emojis.no },
),
);
}

Expand All @@ -123,7 +138,7 @@ export default class EditMessage extends BaseCommand {
newMessage.includes('dsc.gg'))
) {
await interaction.editReply(
t({ phrase: 'errors.inviteLinks', locale: interaction.user.locale }),
t({ phrase: 'errors.inviteLinks', locale: interaction.user.locale }, { emoji: emojis.no }),
);
return;
}
Expand Down
47 changes: 34 additions & 13 deletions src/commands/context-menu/messageInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RESTPostAPIApplicationCommandsJSONBody,
TextInputBuilder,
TextInputStyle,
time,
} from 'discord.js';
import db from '../../utils/Db.js';
import BaseCommand from '../BaseCommand.js';
Expand All @@ -23,7 +24,6 @@ import { REGEX, colors, emojis } from '../../utils/Constants.js';
import { CustomID } from '../../utils/CustomID.js';
import { RegisterInteractionHandler } from '../../decorators/Interaction.js';
import { t } from '../../utils/Locale.js';
import HubLogsManager from '../../managers/HubLogsManager.js';
import { simpleEmbed } from '../../utils/Utils.js';

export default class MessageInfo extends BaseCommand {
Expand All @@ -46,7 +46,10 @@ export default class MessageInfo extends BaseCommand {

if (!originalMsg) {
await interaction.followUp({
content: t({ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale },
{ emoji: emojis.no },
),
ephemeral: true,
});
return;
Expand All @@ -64,7 +67,7 @@ export default class MessageInfo extends BaseCommand {
server: `${server?.name}`,
messageId: target.id,
hub: `${originalMsg.hub?.name}`,
createdAt: `${Math.floor(target.createdTimestamp / 1000)}`,
createdAt: time(Math.floor(target.createdTimestamp / 1000), 'R'),
},
),
)
Expand Down Expand Up @@ -109,7 +112,10 @@ export default class MessageInfo extends BaseCommand {

if (!originalMsg) {
return await interaction.update({
content: t({ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.unknownNetworkMessage', locale: interaction.user.locale },
{ emoji: emojis.no },
),
embeds: [],
components: [],
});
Expand Down Expand Up @@ -141,7 +147,10 @@ export default class MessageInfo extends BaseCommand {
case 'serverInfo': {
if (!server) {
return await interaction.update({
content: t({ phrase: 'errors.unknownServer', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.unknownServer', locale: interaction.user.locale },
{ emoji: emojis.no },
),
embeds: [],
components: [],
});
Expand Down Expand Up @@ -173,7 +182,8 @@ export default class MessageInfo extends BaseCommand {
owner: `${owner.username}#${
owner.discriminator !== '0' ? `#${owner.discriminator}` : ''
}`,
createdAt: `${createdAt}`,
createdAt: time(createdAt, 'R'),
createdAtFull: time(createdAt, 'd'),
memberCount: `${server.memberCount}`,
invite: `${inviteString}`,
},
Expand Down Expand Up @@ -203,7 +213,8 @@ export default class MessageInfo extends BaseCommand {
{
user: author.discriminator !== '0' ? author.tag : author.username,
id: author.id,
createdAt: `${createdAt}`,
createdAt: time(createdAt, 'R'),
createdAtFull: time(createdAt, 'd'),
globalName: author.globalName || 'Not Set.',
hubsOwned: `${await db.hubs.count({ where: { ownerId: author.id } })}`,
},
Expand Down Expand Up @@ -235,7 +246,10 @@ export default class MessageInfo extends BaseCommand {

if (!message) {
await interaction.update({
content: t({ phrase: 'errors.unknownMessage', locale: interaction.user.locale }),
content: t(
{ phrase: 'errors.unknownMessage', locale: interaction.user.locale },
{ emoji: emojis.no },
),
embeds: [],
components: [],
});
Expand All @@ -252,7 +266,7 @@ export default class MessageInfo extends BaseCommand {
server: `${server?.name}`,
messageId: message.id,
hub: `${originalMsg.hub?.name}`,
createdAt: `${Math.floor(message.createdTimestamp / 1000)}`,
createdAt: time(Math.floor(message.createdTimestamp / 1000), 'R'),
},
),
)
Expand All @@ -274,7 +288,10 @@ export default class MessageInfo extends BaseCommand {
return await interaction.reply({
embeds: [
simpleEmbed(
t({ phrase: 'msgInfo.report.notEnabled', locale: interaction.user.locale }),
t(
{ phrase: 'msgInfo.report.notEnabled', locale: interaction.user.locale },
{ emoji: emojis.no },
),
),
],
ephemeral: true,
Expand Down Expand Up @@ -317,21 +334,25 @@ export default class MessageInfo extends BaseCommand {
if (!messageInDb?.originalMsg.hub?.logChannels?.reports) {
return await interaction.reply({
embeds: [
simpleEmbed(t({ phrase: 'msgInfo.report.notEnabled', locale: interaction.user.locale })),
simpleEmbed(
t(
{ phrase: 'msgInfo.report.notEnabled', locale: interaction.user.locale },
{ emoji: emojis.no },
),
),
],
ephemeral: true,
});
}

const hubLogger = new HubLogsManager(messageInDb.originalMsg.hub.id);
const { authorId, serverId } = messageInDb.originalMsg;

const reason = interaction.fields.getTextInputValue('reason');
const message = await interaction.channel?.messages.fetch(messageId).catch(() => null);
const content = message?.content || message?.embeds[0].description || undefined;
const attachmentUrl = message?.embeds[0].image?.url || content?.match(REGEX.IMAGE_URL)?.at(0);

await hubLogger.logReport({
await interaction.client.reportLogger.log(messageInDb.originalMsg.hub.id, {
userId: authorId,
serverId,
reason,
Expand Down
Loading

0 comments on commit 9cbd2bb

Please sign in to comment.