Skip to content

Commit

Permalink
fix(blacklist duration): Use hmdsformat instead
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Sep 30, 2023
1 parent 065829c commit 4036abd
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 345 deletions.
14 changes: 10 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "interchat",
"version": "3.13.0",
"description": "A growing Discord bot which provides inter-server chat!",
"main": "build/src/index.js",
"main": "build/index.js",
"maintainers": [
{
"name": "Supreme1707"
Expand All @@ -22,7 +22,7 @@
"start": "node .",
"build": "tsc --build",
"dev": "tsc-watch --outDir ./build --onSuccess \"node .\"",
"deploy": "node build/src/Utils/deploy-commands.js",
"deploy": "node build/Utils/deploy-commands.js",
"release": "standard-version",
"lint": "eslint --cache --fix .",
"prepare": "husky install"
Expand All @@ -41,12 +41,13 @@
"dotenv": "^16.3.1",
"lodash": "^4.17.21",
"node-schedule": "^2.1.1",
"parse-duration": "^1.1.0",
"winston": "^3.10.0"
},
"devDependencies": {
"@types/common-tags": "^1.8.2",
"@types/lodash": "^4.14.199",
"@types/node": "^20.7.1",
"@types/node": "^20.8.0",
"@types/node-schedule": "^2.1.1",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
Expand Down
49 changes: 16 additions & 33 deletions src/Commands/Apps/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getDb } from '../../Utils/utils';
import { captureException } from '@sentry/node';
import { addServerBlacklist, addUserBlacklist, notifyBlacklist, scheduleUnblacklist } from '../../Utils/blacklist';
import emojis from '../../Utils/JSON/emoji.json';
import parse from 'parse-duration';

export default {
description: 'Blacklist the user or server that sent the message from the hub.',
Expand All @@ -23,11 +24,18 @@ export default {
},
});

if (!messageInDb) return interaction.reply({ content: 'This message was not sent in the network or has expired.', ephemeral: true });
if (!messageInDb) {
interaction.reply({
content:
'This message was not sent in a network, has expired or you lack required permissions to perform this action.',
ephemeral: true,
});
return;
}

const embed = new EmbedBuilder()
.setTitle('Blacklist')
.setDescription('Blacklist a user or server from this hub. This will prevent them from sending messages in this hub.')
.setDescription('Blacklist this server or user from this hub, preventing their messages from being sent.')
.setColor('Blurple');

const buttons = new ActionRowBuilder<ButtonBuilder>()
Expand Down Expand Up @@ -64,29 +72,11 @@ export default {
),
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setCustomId('minutes')
.setLabel('Minutes')
.setPlaceholder('How many minutes should this blacklist last?')
.setCustomId('duration')
.setLabel('Duration')
.setPlaceholder('Duration of the blacklist. Eg. 1d 2h 3m')
.setStyle(TextInputStyle.Short)
.setMaxLength(2)
.setRequired(false),
),
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setCustomId('hours')
.setLabel('Hours')
.setPlaceholder('How many hours should this blacklist last?')
.setStyle(TextInputStyle.Short)
.setMaxLength(2)
.setRequired(false),
),
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setCustomId('days')
.setLabel('Days')
.setPlaceholder('How many days should this blacklist last?')
.setStyle(TextInputStyle.Short)
.setMaxLength(2)
.setMinLength(2)
.setRequired(false),
),
);
Expand All @@ -104,15 +94,8 @@ export default {
await modalResp.deferUpdate();

const reason = modalResp.fields.getTextInputValue('reason');
const mins = parseInt(modalResp.fields.getTextInputValue('minutes')) || 0;
const hours = parseInt(modalResp.fields.getTextInputValue('hours')) || 0;
const days = parseInt(modalResp.fields.getTextInputValue('days')) || 0;

let expires = undefined;
if (mins || hours || days) expires = new Date();
if (mins) expires?.setMinutes(expires.getMinutes() + mins);
if (hours) expires?.setHours(expires.getHours() + hours);
if (days) expires?.setDate(expires.getDate() + days);
const duration = parse(modalResp.fields.getTextInputValue('duration'));
const expires = duration ? new Date(Date.now() + duration) : undefined;

const successEmbed = new EmbedBuilder()
.setColor('Green')
Expand Down
60 changes: 27 additions & 33 deletions src/Commands/Network/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ export default {
.setName('reason')
.setDescription('The reason for blacklisting the user.')
.setRequired(true))
.addNumberOption(option => option
.setName('minutes')
.setDescription('The number of minutes the user will be blakclisted for.')
.setMinValue(1))
.addNumberOption(option => option
.setName('hours')
.setDescription('The number of hours the user will be blacklisted for.'))
.addNumberOption(option => option
.setName('days')
.setDescription('The number of hours the user will be blacklisted for.')),
.addStringOption(option => option
.setName('duration')
.setDescription('The duration of the blacklist.')
.setMinLength(2)),
)
.addSubcommand(subcommand =>
subcommand
Expand All @@ -66,16 +60,10 @@ export default {
.setDescription('The reason for blacklisting the server.')
.setRequired(true),
)
.addNumberOption(option => option
.setName('minutes')
.setDescription('The number of minutes the user will be blakclisted for.')
.setMinValue(1))
.addNumberOption(option => option
.setName('hours')
.setDescription('The number of hours the user will be blacklisted for.'))
.addNumberOption(option => option
.setName('days')
.setDescription('The number of hours the user will be blacklisted for.')),
.addStringOption(option => option
.setName('duration')
.setDescription('The duration of the blacklist.')
.setMinLength(2)),
),
)
.addSubcommandGroup(subcommandGroup =>
Expand Down Expand Up @@ -126,29 +114,34 @@ export default {
subcommand
.setName('list')
.setDescription('List all blacklists.')
.addStringOption(string =>
string
.setName('type')
.setDescription('The type of blacklist to list.')
.addChoices({ name: 'User', value: 'user' }, { name: 'Server', value: 'server' })
.setRequired(true),
)
.addStringOption(hubOption =>
hubOption
.setName('hub')
.setDescription('The name of the hub to blacklist the user from.')
.setDescription('The name of the hub to list from.')
.setAutocomplete(true)
.setRequired(true),
)
.addStringOption(string =>
string
.setName('type')
.setDescription('The type of blacklist to list.')
.setRequired(true)
.addChoices(
{ name: 'User', value: 'user' },
{ name: 'Server', value: 'server' }),
),
),
async execute(interaction: ChatInputCommandInteraction) {
const subCommand = interaction.options.getSubcommand();
const hub = interaction.options.getString('hub', true);
const subcommand = interaction.options.getSubcommand(true);

const db = getDb();
const hubInDb = await db.hubs.findFirst({ where: { name: hub } });
const hubInDb = await db.hubs.findFirst({ where: {
name: hub,
OR: [
{ ownerId: interaction.user.id },
{ moderators: { some: { userId: interaction.user.id } } },
],
},
});

if (!hubInDb) {
return await interaction.reply({
Expand All @@ -163,7 +156,7 @@ export default {
});
}

(await import(`../../Scripts/blacklist/${subCommand}`)).default.execute(interaction, hubInDb);
(await import(`../../Scripts/blacklist/${subcommand}`)).default.execute(interaction, hubInDb);
},

async autocomplete(interaction: AutocompleteInteraction) {
Expand Down Expand Up @@ -221,6 +214,7 @@ export default {
interaction.respond(choices);
break;
}

case 'server': {
const serverOpt = interaction.options.get('server', true);
const serverHubMod = await db.hubs.findFirst({
Expand Down
8 changes: 4 additions & 4 deletions src/Events/messageReactionAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { MessageReaction, PartialMessageReaction, PartialUser, User } from 'disc
import { getDb } from '../Utils/utils';
import updateMessageReactions from '../Scripts/reactions/updateMessage';
import { HubSettingsBitField } from '../Utils/hubSettingsBitfield';
import { findBlacklistedServer, findBlacklistedUser } from '../Utils/blacklist';
import { fetchServerBlacklist, fetchUserBlacklist } from '../Utils/blacklist';

export default {
name: 'messageReactionAdd',
async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser) { // user: User | PartialUser
async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser) {
if (user.bot || user.system) return;

const db = getDb();
Expand All @@ -22,8 +22,8 @@ export default {
!reaction.message.inGuild()
) return;

const userBlacklisted = await findBlacklistedUser(messageInDb.hubId, user.id);
const serverBlacklisted = await findBlacklistedServer(messageInDb.hubId, reaction.message.guild.id);
const userBlacklisted = await fetchUserBlacklist(messageInDb.hubId, user.id);
const serverBlacklisted = await fetchServerBlacklist(messageInDb.hubId, reaction.message.guild.id);
if (userBlacklisted || serverBlacklisted) return;

const cooldown = reaction.client.reactionCooldowns.get(user.id);
Expand Down
Loading

0 comments on commit 4036abd

Please sign in to comment.