Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Moderation): rewrite backend and commands #2595

Merged
merged 21 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
"main": "./dist/Skyra.js",
"type": "module",
"imports": {
"#lib/database": "./dist/lib/database/index.js",
"#utils/common": "./dist/lib/util/common/index.js",
"#utils/functions": "./dist/lib/util/functions/index.js",
"#utils/resolvers": "./dist/lib/util/resolvers/index.js",
"#utils/*": "./dist/lib/util/*.js",
"#lib/database/entities": "./dist/lib/database/entities/index.js",
"#lib/database/keys": "./dist/lib/database/keys/index.js",
"#lib/database/settings": "./dist/lib/database/settings/index.js",
"#lib/database": "./dist/lib/database/index.js",
"#lib/discord": "./dist/lib/discord/index.js",
"#lib/moderation": "./dist/lib/moderation/index.js",
"#lib/moderation/actions": "./dist/lib/moderation/actions/index.js",
"#lib/moderation/common": "./dist/lib/moderation/common/index.js",
"#lib/moderation/managers": "./dist/lib/moderation/managers/index.js",
"#lib/moderation/workers": "./dist/lib/moderation/workers/index.js",
"#lib/structures": "./dist/lib/structures/index.js",
"#lib/moderation": "./dist/lib/moderation/index.js",
"#lib/structures/data": "./dist/lib/structures/data/index.js",
"#lib/structures/managers": "./dist/lib/structures/managers/index.js",
"#lib/structures": "./dist/lib/structures/index.js",
"#lib/setup": "./dist/lib/setup/index.js",
"#lib/types": "./dist/lib/types/index.js",
"#lib/i18n/languageKeys": "./dist/lib/i18n/languageKeys/index.js",
"#lib/*": "./dist/lib/*.js",
"#languages": "./dist/languages/index.js",
"#utils/common": "./dist/lib/util/common/index.js",
"#utils/functions": "./dist/lib/util/functions/index.js",
"#utils/*": "./dist/lib/util/*.js",
"#root/*": "./dist/*.js"
},
"scripts": {
Expand Down
15 changes: 4 additions & 11 deletions src/arguments/case.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { LanguageKeys } from '#lib/i18n/languageKeys';
import { getModeration } from '#utils/functions';
import { Argument, Resolvers } from '@sapphire/framework';

const minimum = 0;
const maximum = 2_147_483_647; // Maximum value for int32
import { resolveCaseId } from '#utils/resolvers';
import { Argument } from '@sapphire/framework';

export class UserArgument extends Argument<number> {
public async run(parameter: string, context: Argument.Context) {
const latest = context.args.t(LanguageKeys.Arguments.CaseLatestOptions);
if (latest.includes(parameter)) return this.ok(await getModeration(context.message.guild!).getCurrentId());

return Resolvers.resolveInteger(parameter, { minimum, maximum }) //
.mapErrInto((identifier) => this.error({ parameter, identifier, context }));
return (await resolveCaseId(parameter, context.args.t, context.message.guild!)) //
.mapErrInto((error) => this.error({ parameter, identifier: error.identifier, context: { ...(error.context as object), ...context } }));
}
}
34 changes: 3 additions & 31 deletions src/arguments/timespan.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import { LanguageKeys } from '#lib/i18n/languageKeys';
import { seconds } from '#utils/common';
import { resolveTimeSpan } from '#utils/resolvers';
import { Argument } from '@sapphire/framework';
import { Duration } from '@sapphire/time-utilities';

export class UserArgument extends Argument<number> {
public run(parameter: string, context: Argument.Context) {
const duration = this.parseParameter(parameter);

if (!Number.isSafeInteger(duration)) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpan, context });
}

if (typeof context.minimum === 'number' && duration < context.minimum) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooSmall, context });
}

if (typeof context.maximum === 'number' && duration > context.maximum) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooBig, context });
}

return this.ok(duration);
}

private parseParameter(parameter: string): number {
const number = Number(parameter);
if (!Number.isNaN(number)) return seconds(number);

const duration = new Duration(parameter).offset;
if (!Number.isNaN(duration)) return duration;

const date = Date.parse(parameter);
if (!Number.isNaN(date)) return date - Date.now();

return NaN;
return resolveTimeSpan(parameter, { minimum: context.minimum, maximum: context.maximum }) //
.mapErrInto((identifier) => this.error({ parameter, identifier, context }));
}
}
5 changes: 3 additions & 2 deletions src/commands/Management/create-mute.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GuildSettings, writeSettings } from '#lib/database';
import { LanguageKeys } from '#lib/i18n/languageKeys';
import { ModerationActions } from '#lib/moderation';
import { SkyraCommand } from '#lib/structures';
import { PermissionLevels, type GuildMessage } from '#lib/types';
import { minutes } from '#utils/common';
import { Emojis } from '#utils/constants';
import { getEmojiReactionFormat, getSecurity, promptConfirmation, promptForMessage, type SerializedEmoji } from '#utils/functions';
import { getEmojiReactionFormat, promptConfirmation, promptForMessage, type SerializedEmoji } from '#utils/functions';
import { ApplyOptions } from '@sapphire/decorators';
import { canReact } from '@sapphire/discord.js-utilities';
import { Argument, CommandOptionsRunTypeEnum, Result, UserError } from '@sapphire/framework';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class UserCommand extends SkyraCommand {
return send(message, content);
}
} else if (await promptConfirmation(message, t(LanguageKeys.Commands.Moderation.ActionSharedRoleSetupNew))) {
await getSecurity(message.guild).actions.muteSetup(message);
await ModerationActions.mute.setup(message);

const content = t(LanguageKeys.Commands.Moderation.Success);
await send(message, content);
Expand Down
144 changes: 0 additions & 144 deletions src/commands/Moderation/Management/history.ts

This file was deleted.

Loading