mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-05 06:26:08 +00:00
Methods for AutoModeration (#90)
* feat(core): AutoModeration prototypes * feat(core): Guild fetch mod rules
This commit is contained in:
parent
fd2b3bc8ce
commit
18bcc32868
@ -1494,6 +1494,8 @@ export interface DiscordAutoModerationRuleTriggerMetadata {
|
|||||||
keyword_filter?: string[];
|
keyword_filter?: string[];
|
||||||
/** The pre-defined lists of words to match from. Only present when TriggerType.KeywordPreset */
|
/** The pre-defined lists of words to match from. Only present when TriggerType.KeywordPreset */
|
||||||
presets?: DiscordAutoModerationRuleTriggerMetadataPresets[];
|
presets?: DiscordAutoModerationRuleTriggerMetadataPresets[];
|
||||||
|
/** substrings which will be exempt from triggering the preset trigger type */
|
||||||
|
allow_list?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum DiscordAutoModerationRuleTriggerMetadataPresets {
|
export enum DiscordAutoModerationRuleTriggerMetadataPresets {
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
import type { Model } from './base';
|
import { Model } from './base';
|
||||||
import type { Session } from '../biscuit';
|
import { Session } from '../biscuit';
|
||||||
import type { Snowflake } from '../snowflakes';
|
import { Snowflake } from '../snowflakes';
|
||||||
import type {
|
import {
|
||||||
AutoModerationActionType,
|
AutoModerationActionType,
|
||||||
AutoModerationEventTypes,
|
AutoModerationEventTypes,
|
||||||
AutoModerationTriggerTypes,
|
AutoModerationTriggerTypes,
|
||||||
DiscordAutoModerationRule,
|
DiscordAutoModerationRule,
|
||||||
DiscordAutoModerationRuleTriggerMetadataPresets,
|
DiscordAutoModerationRuleTriggerMetadataPresets,
|
||||||
DiscordAutoModerationActionExecution,
|
DiscordAutoModerationActionExecution,
|
||||||
|
AUTO_MODERATION_RULES
|
||||||
} from '@biscuitland/api-types';
|
} from '@biscuitland/api-types';
|
||||||
|
|
||||||
export interface AutoModerationRuleTriggerMetadata {
|
export interface AutoModerationRuleTriggerMetadata {
|
||||||
keywordFilter?: string[];
|
keywordFilter?: string[];
|
||||||
presets?: DiscordAutoModerationRuleTriggerMetadataPresets[];
|
presets?: DiscordAutoModerationRuleTriggerMetadataPresets[];
|
||||||
|
allowList?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionMetadata {
|
export interface ActionMetadata {
|
||||||
@ -25,6 +27,18 @@ export interface AutoModerationAction {
|
|||||||
metadata: ActionMetadata;
|
metadata: ActionMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**@link https://discord.com/developers/docs/resources/auto-moderation#create-auto-moderation-rule-json-params */
|
||||||
|
export interface CreateAutoModerationRule {
|
||||||
|
name: string;
|
||||||
|
eventType: 1;
|
||||||
|
triggerType: AutoModerationTriggerTypes;
|
||||||
|
triggerMetadata?: AutoModerationRuleTriggerMetadata;
|
||||||
|
actions: AutoModerationAction[];
|
||||||
|
enabled?: boolean;
|
||||||
|
exemptRoles?: Snowflake[];
|
||||||
|
exemptChannels?: Snowflake[];
|
||||||
|
}
|
||||||
|
|
||||||
export class AutoModerationRule implements Model {
|
export class AutoModerationRule implements Model {
|
||||||
constructor(session: Session, data: DiscordAutoModerationRule) {
|
constructor(session: Session, data: DiscordAutoModerationRule) {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
@ -37,14 +51,15 @@ export class AutoModerationRule implements Model {
|
|||||||
this.triggerMetadata = {
|
this.triggerMetadata = {
|
||||||
keywordFilter: data.trigger_metadata.keyword_filter,
|
keywordFilter: data.trigger_metadata.keyword_filter,
|
||||||
presets: data.trigger_metadata.presets,
|
presets: data.trigger_metadata.presets,
|
||||||
|
allowList: data.trigger_metadata.allow_list
|
||||||
};
|
};
|
||||||
this.actions = data.actions.map(action =>
|
this.actions = data.actions.map(action =>
|
||||||
Object.create({
|
Object.create({
|
||||||
type: action.type,
|
type: action.type,
|
||||||
metadata: {
|
metadata: {
|
||||||
channelId: action.metadata.channel_id,
|
channelId: action.metadata.channel_id,
|
||||||
durationSeconds: action.metadata.duration_seconds,
|
durationSeconds: action.metadata.duration_seconds
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
this.enabled = !!data.enabled;
|
this.enabled = !!data.enabled;
|
||||||
@ -64,19 +79,102 @@ export class AutoModerationRule implements Model {
|
|||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
exemptRoles: Snowflake[];
|
exemptRoles: Snowflake[];
|
||||||
exemptChannels: Snowflake[];
|
exemptChannels: Snowflake[];
|
||||||
|
|
||||||
|
async getRules(
|
||||||
|
ruleId?: Snowflake
|
||||||
|
): Promise<AutoModerationRule | AutoModerationRule[]> {
|
||||||
|
const request = await this.session.rest.get<
|
||||||
|
DiscordAutoModerationRule | DiscordAutoModerationRule[]
|
||||||
|
>(AUTO_MODERATION_RULES(this.guildId, ruleId));
|
||||||
|
if (Array.isArray(request))
|
||||||
|
return request.map(
|
||||||
|
amr => new AutoModerationRule(this.session, amr)
|
||||||
|
);
|
||||||
|
return new AutoModerationRule(this.session, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createRule(options: CreateAutoModerationRule) {
|
||||||
|
const request = await this.session.rest.post<DiscordAutoModerationRule>(
|
||||||
|
AUTO_MODERATION_RULES(this.guildId),
|
||||||
|
{
|
||||||
|
name: options.name,
|
||||||
|
event_type: options.eventType,
|
||||||
|
trigger_type: options.triggerType,
|
||||||
|
trigger_metadata: options.triggerMetadata,
|
||||||
|
actions: options.actions
|
||||||
|
? options.actions.map(x =>
|
||||||
|
Object.assign(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
type: x.type,
|
||||||
|
metadata: {
|
||||||
|
channel_id: x.metadata.channelId,
|
||||||
|
duration_seconds:
|
||||||
|
x.metadata.durationSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: undefined,
|
||||||
|
enabled: !!options.enabled,
|
||||||
|
exempt_roles: options.exemptRoles,
|
||||||
|
exempt_channels: options.exemptChannels
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return new AutoModerationRule(this.session, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
async editRule(
|
||||||
|
ruleId = this.id,
|
||||||
|
options: Partial<CreateAutoModerationRule>
|
||||||
|
) {
|
||||||
|
const request = await this.session.rest.patch<
|
||||||
|
DiscordAutoModerationRule
|
||||||
|
>(AUTO_MODERATION_RULES(this.guildId, ruleId), {
|
||||||
|
name: options.name,
|
||||||
|
event_type: options.eventType,
|
||||||
|
trigger_type: options.triggerType,
|
||||||
|
trigger_metadata: options.triggerMetadata,
|
||||||
|
actions: options.actions
|
||||||
|
? options.actions.map(x =>
|
||||||
|
Object.assign(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
type: x.type,
|
||||||
|
metadata: {
|
||||||
|
channel_id: x.metadata.channelId,
|
||||||
|
duration_seconds: x.metadata.durationSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
: undefined,
|
||||||
|
enabled: !!options.enabled,
|
||||||
|
exempt_roles: options.exemptRoles,
|
||||||
|
exempt_channels: options.exemptChannels
|
||||||
|
});
|
||||||
|
return new AutoModerationRule(this.session, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteRule(ruleId = this.id): Promise<void> {
|
||||||
|
await this.session.rest.delete(
|
||||||
|
AUTO_MODERATION_RULES(this.guildId, ruleId)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AutoModerationExecution {
|
export class AutoModerationExecution {
|
||||||
constructor(session: Session, data: DiscordAutoModerationActionExecution) {
|
constructor(session: Session, data: DiscordAutoModerationActionExecution) {
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.guildId = data.guild_id;
|
this.guildId = data.guild_id;
|
||||||
this.action = Object.create({
|
this.action = {
|
||||||
type: data.action.type,
|
type: data.action.type,
|
||||||
metadata: {
|
metadata: {
|
||||||
channelId: data.action.metadata.channel_id,
|
channelId: data.action.metadata.channel_id as string,
|
||||||
durationSeconds: data.action.metadata.duration_seconds,
|
durationSeconds: data.action.metadata.duration_seconds as number
|
||||||
},
|
}
|
||||||
});
|
};
|
||||||
this.ruleId = data.rule_id;
|
this.ruleId = data.rule_id;
|
||||||
this.ruleTriggerType = data.rule_trigger_type;
|
this.ruleTriggerType = data.rule_trigger_type;
|
||||||
this.userId = data.user_id;
|
this.userId = data.user_id;
|
||||||
|
@ -68,6 +68,7 @@ import { User } from './user';
|
|||||||
import { Widget } from './widget';
|
import { Widget } from './widget';
|
||||||
import { Sticker } from './sticker';
|
import { Sticker } from './sticker';
|
||||||
import { WelcomeScreen } from './welcome';
|
import { WelcomeScreen } from './welcome';
|
||||||
|
import { AutoModerationRule } from './automod';
|
||||||
|
|
||||||
/** BaseGuild */
|
/** BaseGuild */
|
||||||
/**
|
/**
|
||||||
@ -1119,6 +1120,16 @@ export class Guild extends BaseGuild implements Model {
|
|||||||
return new Guild(this.session, guild);
|
return new Guild(this.session, guild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the auto moderation rules for the guild.
|
||||||
|
* @see {@link AutoModerationRule#getRules} sames
|
||||||
|
* @param ruleId The optional rule id
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
fetchAutoModerationRules(ruleId?: Snowflake): Promise<AutoModerationRule | AutoModerationRule[]> {
|
||||||
|
return AutoModerationRule.prototype.getRules.call({session: this.session, guildId: this.id}, ruleId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the voice regions available for the guild.
|
* gets the voice regions available for the guild.
|
||||||
* @see {@link DiscordVoiceRegion}
|
* @see {@link DiscordVoiceRegion}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user