extending Command class

How can i make a class called ModerationCommand which automatically checks if the user is a staff member and if not then returns an error so that i dont have to implement the checking in every moderation command file
Solution:
```ts import { Command } from '@sapphire/framework'; export abstract class ModerationCommand extends Command { public constructor(context: Command.Context, options: Command.Options) {...
Jump to solution
11 Replies
Solution
Favna
Favna14mo ago
import { Command } from '@sapphire/framework';

export abstract class ModerationCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
preconditions: ['Moderator', ...options.preconditions],
...options
});
}
}
import { Command } from '@sapphire/framework';

export abstract class ModerationCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) {
super(context, {
preconditions: ['Moderator', ...options.preconditions],
...options
});
}
}
Oreo ™
Oreo ™14mo ago
@Favna How can i make an argument that converts durations like 2d 10m to ms
Favna
Favna14mo ago
use a natural language processing library there's many libs to do such a thing
Oreo ™
Oreo ™14mo ago
no i mean how can i make the argument whenever i try to make one
Spinel
Spinel14mo ago
<:_:746069730170896405> Sapphire guide results: User Guide / Creating your own arguments
Oreo ™
Oreo ™14mo ago
So this should work right
import { Argument } from '@sapphire/framework';
import { ms } from 'enhanced-ms';

export class UserArgument extends Argument {
public run(parameter: string, context: Argument.Context) {
const duration = ms(parameter);

if (!duration) {
return this.error({
parameter,
identifier: 'InvalidDuration',
message: 'Please provide a valid duration',
context,
});
}

if (typeof context.minimum === 'number' && duration < context.minimum) {
return this.error({
parameter,
identifier: 'DurationTooSmall',
message: `Duration should be greater than 0ms`,
context,
});
}

if (typeof context.maximum === 'number' && duration > context.maximum) {
return this.error({
parameter,
identifier: 'DurationTooBig',
message: `Duration should be less than ${ms(context.maximum)}`,
context,
});
}

return this.ok(duration);
}
}

declare module '@sapphire/framework' {
interface ArgType {
duration: number;
}
}
import { Argument } from '@sapphire/framework';
import { ms } from 'enhanced-ms';

export class UserArgument extends Argument {
public run(parameter: string, context: Argument.Context) {
const duration = ms(parameter);

if (!duration) {
return this.error({
parameter,
identifier: 'InvalidDuration',
message: 'Please provide a valid duration',
context,
});
}

if (typeof context.minimum === 'number' && duration < context.minimum) {
return this.error({
parameter,
identifier: 'DurationTooSmall',
message: `Duration should be greater than 0ms`,
context,
});
}

if (typeof context.maximum === 'number' && duration > context.maximum) {
return this.error({
parameter,
identifier: 'DurationTooBig',
message: `Duration should be less than ${ms(context.maximum)}`,
context,
});
}

return this.ok(duration);
}
}

declare module '@sapphire/framework' {
interface ArgType {
duration: number;
}
}
But when i try to use the argument i get
Property 'success' does not exist on type 'ResultType<number>'.
Property 'success' does not exist on type 'Ok<number>'.ts(2339)
Property 'success' does not exist on type 'ResultType<number>'.
Property 'success' does not exist on type 'Ok<number>'.ts(2339)
for
const result = await args.pickResult('duration', {
maximum: years(2), minimum: 0
});

if (result.success) return result.value
const result = await args.pickResult('duration', {
maximum: years(2), minimum: 0
});

if (result.success) return result.value
Favna
Favna14mo ago
the error is correct. That is not valid code. Hasn't been since Sapphire v3 not sure where you based it on
const result = await args.pickResult('duration', {
maximum: years(2), minimum: 0
});

if (result.isOk()) return result.unwrap();
const result = await args.pickResult('duration', {
maximum: years(2), minimum: 0
});

if (result.isOk()) return result.unwrap();
this is the proper code
Oreo ™
Oreo ™14mo ago
oh ok Also is there a way to make the ModerationCommand check the database and see if showOutput is enabled if its not enabled then dont display and output like how Dyno does it
Favna
Favna14mo ago
You're essentially asking basic JavaScript class questions. The answer is yes, you just need to be a bit incentive with your methods and their naming.
Oreo ™
Oreo ™14mo ago
I can't get it to work in the messageRun function
Favna
Favna14mo ago
Well no if you have a messageRun in the super class and in the child class then the child will overwrite the parent. This is basic class inheritance. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain You'll have to do something else to get around this. There's many ways to do this. Like I said, this is JavaScript basics. Not sapphire related at all.
Inheritance and the prototype chain - JavaScript | MDN
JavaScript is a bit confusing for developers experienced in class-based languages (like Java or C++), as it is dynamic and does not have static types.