Skip to content

Commit

Permalink
fix(blacklist): fix server blacklist name not showing up
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 24, 2024
1 parent 761cde6 commit 0c43fde
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
11 changes: 6 additions & 5 deletions src/commands/context-menu/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export default class Blacklist extends BaseCommand {
{ username: user?.username ?? 'Unknown User', emoji: emojis.tick },
),
);

await blacklistManager.addUserBlacklist(
originalMsg.hubId,
originalMsg.authorId,
Expand All @@ -222,8 +223,8 @@ export default class Blacklist extends BaseCommand {
})
.catch(() => null);

await logBlacklist(originalMsg.hubId, {
userOrServer: user,
await logBlacklist(originalMsg.hubId, interaction.client, {
target: user,
mod: interaction.user,
reason,
expires,
Expand All @@ -235,7 +236,7 @@ export default class Blacklist extends BaseCommand {

// server blacklist
else {
const server = interaction.client.guilds.cache.get(originalMsg.serverId);
const server = await interaction.client.fetchGuild(originalMsg.serverId);

successEmbed.setDescription(
t(
Expand Down Expand Up @@ -274,8 +275,8 @@ export default class Blacklist extends BaseCommand {
});

if (server) {
await logBlacklist(originalMsg.hubId, {
userOrServer: server,
await logBlacklist(originalMsg.hubId, interaction.client, {
target: server.id,
mod: interaction.user,
reason,
expires,
Expand Down
20 changes: 10 additions & 10 deletions src/commands/slash/Main/blacklist/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export default class UserBlacklist extends BlacklistCommand {

const blacklistManager = interaction.client.blacklistManager;
const subCommandGroup = interaction.options.getSubcommandGroup();
const serverOpt = interaction.options.getString('server', true);
const serverId = interaction.options.getString('server', true);

if (subCommandGroup === 'add') {
const reason = interaction.options.getString('reason', true);
const duration = parse(`${interaction.options.getString('duration')}`);
const expires = duration ? new Date(Date.now() + duration) : undefined;

const serverInBlacklist = await BlacklistManager.fetchServerBlacklist(hubInDb.id, serverOpt);
const serverInBlacklist = await BlacklistManager.fetchServerBlacklist(hubInDb.id, serverId);
if (serverInBlacklist) {
await interaction.followUp({
embeds: [
Expand All @@ -69,7 +69,7 @@ export default class UserBlacklist extends BlacklistCommand {
return;
}

const server = await interaction.client.guilds.fetch(serverOpt).catch(() => null);
const server = await interaction.client.fetchGuild(serverId);
if (!server) {
await interaction.followUp(
t(
Expand All @@ -82,7 +82,7 @@ export default class UserBlacklist extends BlacklistCommand {

try {
await blacklistManager.addServerBlacklist(
server.id,
serverId,
hubInDb.id,
reason,
interaction.user.id,
Expand Down Expand Up @@ -134,22 +134,22 @@ export default class UserBlacklist extends BlacklistCommand {

// notify the server that they have been blacklisted
await blacklistManager
.notifyBlacklist('server', serverOpt, { hubId: hubInDb.id, expires, reason })
.notifyBlacklist('server', serverId, { hubId: hubInDb.id, expires, reason })
.catch(() => null);

// delete all connections from db so they can't reconnect to the hub
await deleteConnections({ serverId: server.id, hubId: hubInDb.id });
await deleteConnections({ serverId, hubId: hubInDb.id });

// send log to hub's log channel
await logBlacklist(hubInDb.id, {
userOrServer: server,
await logBlacklist(hubInDb.id, interaction.client, {
target: serverId,
mod: interaction.user,
reason,
expires,
});
}
else if (subCommandGroup === 'remove') {
const result = await blacklistManager.removeBlacklist('server', hubInDb.id, serverOpt);
const result = await blacklistManager.removeBlacklist('server', hubInDb.id, serverId);
if (!result) {
await interaction.followUp(
t(
Expand All @@ -171,7 +171,7 @@ export default class UserBlacklist extends BlacklistCommand {
// send log to hub's log channel
await logUnblacklist(hubInDb.id, {
type: 'user',
userOrServerId: serverOpt,
targetId: serverId,
mod: interaction.user,
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/slash/Main/blacklist/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export default class Server extends BlacklistCommand {
await interaction.followUp({ embeds: [successEmbed] });

// send log to hub's log channel
await logBlacklist(hubInDb.id, {
userOrServer: user,
await logBlacklist(hubInDb.id, interaction.client, {
target: user,
mod: interaction.user,
reason,
expires,
Expand Down Expand Up @@ -159,7 +159,7 @@ export default class Server extends BlacklistCommand {
// send log to hub's log channel
await logUnblacklist(hubInDb.id, {
type: 'user',
userOrServerId: user.id,
targetId: user.id,
mod: interaction.user,
reason,
});
Expand Down
2 changes: 1 addition & 1 deletion src/managers/BlacklistManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class BlacklistManager {
if (!SuperClient.instance?.user) return;
await logUnblacklist(hubId, {
type,
userOrServerId: id,
targetId: id,
mod: SuperClient.instance.user,
reason: 'Blacklist duration expired.',
});
Expand Down
63 changes: 43 additions & 20 deletions src/utils/HubLogger/ModLogs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { stripIndents } from 'common-tags';
import { User, Guild, EmbedBuilder } from 'discord.js';
import { User, EmbedBuilder, Snowflake, Client } from 'discord.js';
import BlacklistManager from '../../managers/BlacklistManager.js';
import { emojis, colors } from '../Constants.js';
import { fetchHub, toTitleCase } from '../Utils.js';
import { sendLog } from './Default.js';
import SuperClient from '../../core/Client.js';

/**
* Logs the blacklisting of a user or server.
Expand All @@ -14,30 +15,53 @@ import { sendLog } from './Default.js';
*/
export const logBlacklist = async (
hubId: string,
client: Client,
opts: {
userOrServer: User | Guild;
target: User | Snowflake;
mod: User;
reason: string;
expires?: Date;
},
) => {
const { userOrServer, mod, reason, expires } = opts;
const { target: _target, mod, reason, expires } = opts;

const hub = await fetchHub(hubId);
if (!hub?.logChannels?.modLogs) return;

const name = userOrServer instanceof User ? userOrServer.username : userOrServer.name;
const iconURL =
userOrServer instanceof User
? userOrServer.displayAvatarURL()
: userOrServer.iconURL() ?? undefined;
const type = userOrServer instanceof User ? 'User' : 'Server';
let name;
let iconURL;
let type;
let target;

if (_target instanceof User) {
target = _target;
name = target.username;
iconURL = target.displayAvatarURL();
type = 'User';
}
else {
target = SuperClient.resolveEval(
await client.cluster.broadcastEval(
(c, guildId) => {
const guild = c.guilds.cache.get(guildId);
return { name: guild?.name, iconURL: guild?.iconURL() ?? undefined, id: guildId };
},
{ context: _target },
),
);

if (!target) return;

name = target.name;
iconURL = target.iconURL;
type = 'Server';
}

const embed = new EmbedBuilder()
.setAuthor({ name: `${type} ${name} blacklisted`, iconURL })
.setDescription(
stripIndents`
${emojis.dotBlue} **${type}:** ${name} (${userOrServer.id})
${emojis.dotBlue} **${type}:** ${name} (${target.id})
${emojis.dotBlue} **Moderator:** ${mod.username} (${mod.id})
${emojis.dotBlue} **Hub:** ${hub?.name}
`,
Expand All @@ -60,7 +84,7 @@ export const logUnblacklist = async (
hubId: string,
opts: {
type: 'user' | 'server';
userOrServerId: string;
targetId: string;
mod: User;
reason?: string;
},
Expand All @@ -73,25 +97,24 @@ export const logUnblacklist = async (
let originalReason: string | undefined;

if (opts.type === 'user') {
blacklisted = await BlacklistManager.fetchUserBlacklist(hub.id, opts.userOrServerId);
name =
(await opts.mod.client.users.fetch(opts.userOrServerId).catch(() => null))?.username ??
`${blacklisted?.username}`;
blacklisted = await BlacklistManager.fetchUserBlacklist(hub.id, opts.targetId);
const user = await opts.mod.client.users.fetch(opts.targetId).catch(() => null);
name = user?.username ?? `${blacklisted?.username}`;
originalReason = blacklisted?.blacklistedFrom.find((h) => h.hubId === hub.id)?.reason;
}
else {
blacklisted = await BlacklistManager.fetchServerBlacklist(hub.id, opts.userOrServerId);
blacklisted = await BlacklistManager.fetchServerBlacklist(hub.id, opts.targetId);
name = blacklisted?.serverName;
}

const embed = new EmbedBuilder()
.setAuthor({ name: `${toTitleCase(opts.type)} ${name} unblacklisted` })
.setDescription(
stripIndents`
${emojis.dotBlue} **User:** ${name} (${opts.userOrServerId})
${emojis.dotBlue} **Moderator:** ${opts.mod.username} (${opts.mod.id})
${emojis.dotBlue} **Hub:** ${hub?.name}
`,
${emojis.dotBlue} **User:** ${name} (${opts.targetId})
${emojis.dotBlue} **Moderator:** ${opts.mod.username} (${opts.mod.id})
${emojis.dotBlue} **Hub:** ${hub?.name}
`,
)
.addFields(
{
Expand Down

0 comments on commit 0c43fde

Please sign in to comment.