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

feat(Automod)!: Add missing features #1556

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function (token, options) {
}

export const {
AutoModerationRule,
ApplicationCommand,
AutocompleteInteraction,
Base,
Expand Down
105 changes: 62 additions & 43 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ declare namespace Eris {
type AutoModerationEventType = Constants["AutoModerationEventTypes"][keyof Constants["AutoModerationEventTypes"]];
type AutoModerationKeywordPresetType = Constants["AutoModerationKeywordPresetTypes"][keyof Constants["AutoModerationKeywordPresetTypes"]];
type AutoModerationTriggerType = Constants["AutoModerationTriggerTypes"][keyof Constants["AutoModerationTriggerTypes"]];
type EditAutoModerationRuleOptions = Partial<CreateAutoModerationRuleOptions>;

// Cache
interface Uncached { id: string }
Expand Down Expand Up @@ -191,6 +190,7 @@ declare namespace Eris {
type SortOrderTypes = Constants["SortOrderTypes"][keyof Constants["SortOrderTypes"]];

// User
type PossiblyUncachedUser = User | Uncached;
type PremiumTypes = Constants["PremiumTypes"][keyof Constants["PremiumTypes"]];

// Voice
Expand Down Expand Up @@ -319,9 +319,9 @@ declare namespace Eris {
}

// Auto Moderation
interface AutoModerationAction {
metadata?: AutoModerationActionMetadata;
type: AutoModerationActionType;
interface AutoModerationAction<T = AutoModerationActionType> {
metadata: T extends Constants["AutoModerationActionTypes"]["BLOCK_MEMBER_INTERACTION"] ? never : AutoModerationActionMetadata<T>;
type: T;
}
interface AutoModerationActionExecution {
action: AutoModerationAction;
Expand All @@ -336,42 +336,35 @@ declare namespace Eris {
ruleTriggerType: AutoModerationTriggerType;
userID: string;
}
interface AutoModerationActionMetadata {
/** valid for SEND_ALERT_MESSAGE */
channelID?: string;
/** valid for TIMEOUT */
durationSeconds?: number;
interface AutoModerationActionMetadata<T = AutoModerationActionType> {
channelID: T extends Constants["AutoModerationActionTypes"]["SEND_ALERT_MESSAGE"] ? string : never;
customMessage: T extends Constants["AutoModerationActionTypes"]["BLOCK_MESSAGE"] ? string : never;
durationSeconds: T extends Constants["AutoModerationActionTypes"]["TIMEOUT"] ? number : never;
}
interface AutoModerationRule {
interface AutoModerationCreateOptions<T = AutoModerationTriggerType> {
actions: AutoModerationAction[];
creatorID: string;
enabled: boolean;
eventType: AutoModerationEventType;
exemptRoles: string[];
exemptUsers: string[];
guildID: string;
id: string;
name: string;
triggerMetadata: AutoModerationTriggerMetadata;
triggerType: AutoModerationTriggerType;
triggerMetadata: T extends Constants["AutoModerationTriggerTypes"]["SPAM"] ? never : AutoModerationTriggerMetadata<T>;
triggerType: T;
}
interface CreateAutoModerationRuleOptions {
actions: AutoModerationAction[];
interface AutoModerationEditOptions<T extends AutoModerationTriggerType> {
actions?: AutoModerationAction[];
enabled?: boolean;
eventType: AutoModerationActionType;
eventType?: AutoModerationEventType;
exemptChannels?: string[];
exemptRoles?: string[];
name: string;
name?: string;
reason?: string;
triggerMetadata?: AutoModerationTriggerMetadata;
triggerType: AutoModerationTriggerType;
triggerMetadata: T extends Constants["AutoModerationTriggerTypes"]["SPAM"] ? never : AutoModerationTriggerMetadata<T> | undefined;
}

interface AutoModerationTriggerMetadata {
/** valid for KEYWORD */
keywordFilter: string[];
/** valid for KEYWORD_PRESET */
presets: AutoModerationKeywordPresetType[];
interface AutoModerationTriggerMetadata<T = AutoModerationTriggerType> {
allowList: T extends Constants["AutoModerationTriggerTypes"]["KEYWORD" | "KEYWORD_PRESET" | "MEMBER_PROFILE"] ? string[] : never;
keywordFilter: T extends Constants["AutoModerationTriggerTypes"]["KEYWORD" | "MEMBER_PROFILE"] ? string[] : never;
mentionRaidProtectionEnabled: T extends Constants["AutoModerationTriggerTypes"]["MENTION_SPAM"] ? boolean : never;
mentionTotalLimit: T extends Constants["AutoModerationTriggerTypes"]["MENTION_SPAM"] ? number : never;
presets: T extends Constants["AutoModerationTriggerTypes"]["KEYWORD_PRESET"] ? AutoModerationKeywordPresetType[] : never;
regexPatterns: T extends Constants["AutoModerationTriggerTypes"]["KEYWORD" | "MEMBER_PROFILE"] ? string[] : never;
}

// Channel
Expand Down Expand Up @@ -715,6 +708,15 @@ declare namespace Eris {
}

// Events
interface OldAutoModerationRule {
actions: AutoModerationAction[];
enabled: boolean;
eventType: AutoModerationEventType;
exemptChannels: string[];
exemptRoles: string[];
name: string;
triggerMetadata: AutoModerationTriggerMetadata;
}
interface OldCall {
endedTimestamp?: number;
participants: string[];
Expand Down Expand Up @@ -884,7 +886,7 @@ declare namespace Eris {
autoModerationActionExecution: [guild: Guild, action: AutoModerationActionExecution];
autoModerationRuleCreate: [guild: Guild, rule: AutoModerationRule];
autoModerationRuleDelete: [guild: Guild, rule: AutoModerationRule];
autoModerationRuleUpdate: [guild: Guild, rule: AutoModerationRule | null, newRule: AutoModerationRule];
autoModerationRuleUpdate: [guild: Guild, rule: AutoModerationRule, oldRule: OldAutoModerationRule | null];
channelCreate: [channel: AnyGuildChannel];
channelDelete: [channel: Exclude<AnyChannel, GroupChannel>];
channelPinUpdate: [channel: TextableChannel, timestamp: number, oldTimestamp: number];
Expand Down Expand Up @@ -912,8 +914,8 @@ declare namespace Eris {
guildScheduledEventCreate: [event: GuildScheduledEvent];
guildScheduledEventDelete: [event: GuildScheduledEvent];
guildScheduledEventUpdate: [event: GuildScheduledEvent, oldEvent: OldGuildScheduledEvent | null];
guildScheduledEventUserAdd: [event: PossiblyUncachedGuildScheduledEvent, user: User | Uncached];
guildScheduledEventUserRemove: [event: PossiblyUncachedGuildScheduledEvent, user: User | Uncached];
guildScheduledEventUserAdd: [event: PossiblyUncachedGuildScheduledEvent, user: PossiblyUncachedUser];
guildScheduledEventUserRemove: [event: PossiblyUncachedGuildScheduledEvent, user: PossiblyUncachedUser];
guildSoundboardSoundCreate: [sound: SoundboardSound];
guildSoundboardSoundDelete: [sound: PossiblyUncachedGuildSoundboardSound];
guildSoundboardSoundUpdate: [sound: SoundboardSound, oldSound: OldGuildSoundboardSound | null];
Expand All @@ -928,8 +930,8 @@ declare namespace Eris {
messageCreate: [message: Message<PossiblyUncachedTextableChannel>];
messageDelete: [message: PossiblyUncachedMessage];
messageDeleteBulk: [messages: PossiblyUncachedMessage[]];
messagePollVoteAdd: [message: PossiblyUncachedMessage, user: User | Uncached, answerID: number];
messagePollVoteRemove: [message: PossiblyUncachedMessage, user: User | Uncached, answerID: number];
messagePollVoteAdd: [message: PossiblyUncachedMessage, user: PossiblyUncachedUser, answerID: number];
messagePollVoteRemove: [message: PossiblyUncachedMessage, user: PossiblyUncachedUser, answerID: number];
messageReactionAdd: [message: PossiblyUncachedMessage, emoji: PartialEmoji, reactor: Member | Uncached, burst: boolean];
messageReactionRemove: [message: PossiblyUncachedMessage, emoji: PartialEmoji, userID: string, burst: boolean];
messageReactionRemoveAll: [message: PossiblyUncachedMessage];
Expand All @@ -950,8 +952,8 @@ declare namespace Eris {
threadMembersUpdate: [channel: AnyThreadChannel, addedMembers: ThreadMember[], removedMembers: (ThreadMember | Uncached)[]];
threadMemberUpdate: [channel: AnyThreadChannel, member: ThreadMember, oldMember: OldThreadMember];
threadUpdate: [channel: AnyThreadChannel, oldChannel: OldThread | null];
typingStart: [channel: AnyGuildTextableChannel | Uncached, user: User | Uncached, member: Member]
| [channel: DMChannel | Uncached, user: User | Uncached, member: null];
typingStart: [channel: AnyGuildTextableChannel | Uncached, user: PossiblyUncachedUser, member: Member]
| [channel: DMChannel | Uncached, user: PossiblyUncachedUser, member: null];
unavailableGuildCreate: [guild: UnavailableGuild];
unknown: [packet: RawPacket, id?: number];
userUpdate: [user: User, oldUser: PartialUser | null];
Expand Down Expand Up @@ -1899,7 +1901,7 @@ declare namespace Eris {
guild: PossiblyUncachedGuild;
soundID?: string | number;
soundVolume?: number;
user: User | Uncached;
user: PossiblyUncachedUser;
}
interface VoiceConnectData {
channel_id: string;
Expand Down Expand Up @@ -2022,6 +2024,21 @@ declare namespace Eris {
}

// Classes
export class AutoModerationRule<T extends AutoModerationTriggerType = AutoModerationTriggerType> extends Base {
actions: AutoModerationAction[];
creator: PossiblyUncachedUser;
enabled: boolean;
eventType: AutoModerationEventType;
exemptChannels: string[];
exemptRoles: string[];
guild: PossiblyUncachedGuild;
name: string;
triggerMetadata: AutoModerationTriggerMetadata<T>;
triggerType: T;
constructor(data: BaseData, client: Client);
delete(reason?: string): Promise<void>;
edit(options: AutoModerationEditOptions<T>): Promise<AutoModerationRule<T>>;
}
/** Generic T is `true` if a Guild scoped command, and `false` if not */
export class ApplicationCommand<T extends boolean, U = ApplicationCommandTypes> extends Base {
applicationID: string;
Expand Down Expand Up @@ -2145,7 +2162,7 @@ declare namespace Eris {
bulkEditGuildCommands(guildID: string, commands: ApplicationCommandBulkEditOptions<true>[]): Promise<ApplicationCommand<true>[]>;
closeVoiceConnection(guildID: string): void;
connect(): Promise<void>;
createAutoModerationRule(guildID: string, rule: CreateAutoModerationRuleOptions): Promise<AutoModerationRule>;
createAutoModerationRule(guildID: string, rule: AutoModerationCreateOptions): Promise<AutoModerationRule>;
createChannel(guildID: string, name: string): Promise<TextChannel>;
createChannel<T extends GuildChannelTypes>(guildID: string, name: string, type: T, options?: CreateChannelOptions): Promise<ChannelTypeConversion<T>>;
/** @deprecated */
Expand Down Expand Up @@ -2202,7 +2219,7 @@ declare namespace Eris {
deleteWebhookMessage(webhookID: string, token: string, messageID: string): Promise<void>;
disconnect(options: { reconnect?: boolean | "auto" }): void;
editAFK(afk: boolean): void;
editAutoModerationRule(guildID: string, ruleID: string, options: EditAutoModerationRuleOptions): Promise<AutoModerationRule>;
editAutoModerationRule<T extends AutoModerationTriggerType>(guildID: string, ruleID: string, options: AutoModerationEditOptions<T>): Promise<AutoModerationRule>;
editChannel(
channelID: string,
options: EditGuildChannelOptions | EditGroupChannelOptions,
Expand Down Expand Up @@ -2626,7 +2643,7 @@ declare namespace Eris {
banMember(userID: string, deleteMessageDays?: number, reason?: string): Promise<void>;
bulkBanMembers(options: BulkBanMembersOptions): Promise<BulkBanMembersResponse>;
bulkEditCommands<T extends ApplicationCommandTypes>(commands: ApplicationCommandBulkEditOptions<true, T>[]): Promise<ApplicationCommand<true, T>[]>;
createAutoModerationRule(rule: CreateAutoModerationRuleOptions): Promise<AutoModerationRule>;
createAutoModerationRule(rule: AutoModerationCreateOptions): Promise<AutoModerationRule>;
createChannel(name: string): Promise<TextChannel>;
createChannel<T extends GuildChannelTypes>(name: string, type: T, options?: CreateChannelOptions): Promise<ChannelTypeConversion<T>>;
/** @deprecated */
Expand Down Expand Up @@ -2655,7 +2672,7 @@ declare namespace Eris {
dynamicIconURL(format?: ImageFormat, size?: number): string | null;
dynamicSplashURL(format?: ImageFormat, size?: number): string | null;
edit(options: GuildOptions, reason?: string): Promise<Guild>;
editAutoModerationRule(ruleID: string, options: EditAutoModerationRuleOptions): Promise<AutoModerationRule>;
editAutoModerationRule<T extends AutoModerationTriggerType>(ruleID: string, options: AutoModerationEditOptions<T>): Promise<AutoModerationRule>;
editChannelPositions(channelPositions: ChannelPosition[]): Promise<void>;
editCommand<T extends ApplicationCommandTypes>(commandID: string, command: ApplicationCommandEditOptions<true, T>): Promise<ApplicationCommand<true, T>>;
editCommandPermissions(permissions: ApplicationCommandPermissions[], reason?: string): Promise<GuildApplicationCommandPermissions[]>;
Expand Down Expand Up @@ -2732,6 +2749,8 @@ declare namespace Eris {
export class GuildAuditLogEntry extends Base {
actionType: number;
after: Record<string, unknown> | null;
autoModerationRuleName?: string;
autoModerationRuleTriggerType?: AutoModerationTriggerType;
before: Record<string, unknown> | null;
channel?: AnyGuildChannel | Uncached;
count?: number;
Expand All @@ -2746,7 +2765,7 @@ declare namespace Eris {
status?: string;
target?: Guild | AnyGuildChannel | Member | Role | Invite | Emoji | Sticker | Message<AnyGuildTextableChannel> | null;
targetID: string;
user: User | Uncached;
user: PossiblyUncachedUser;
constructor(data: BaseData, guild: Guild);
}

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Eris(token, options) {
return new Client(token, options);
}

Eris.AutoModerationRule = require("./lib/structures/AutoModerationRule");
Eris.ApplicationCommand = require("./lib/structures/ApplicationCommand");
Eris.AutocompleteInteraction = require("./lib/structures/AutocompleteInteraction");
Eris.Base = require("./lib/structures/Base");
Expand Down
23 changes: 13 additions & 10 deletions lib/Client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const ApplicationCommand = require("./structures/ApplicationCommand");
const AutoModerationRule = require("./structures/AutoModerationRule");
const Base = require("./structures/Base");
const Channel = require("./structures/Channel");
const Collection = require("./util/Collection");
Expand Down Expand Up @@ -527,7 +528,7 @@ class Client extends EventEmitter {
* @arg {Object} [options.triggerMetadata] The [trigger metadata](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-metadata) for the rule
* @arg {Number} options.triggerType The [trigger type](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-types) of the rule
* @arg {String} [options.reason] The reason to be displayed in audit logs
* @returns {Promise<Object>}
* @returns {Promise<AutoModerationRule>}
*/
createAutoModerationRule(guildID, options) {
return this.requestHandler.request("POST", Endpoints.AUTO_MODERATION_RULES(guildID), true, {
Expand All @@ -539,7 +540,8 @@ class Client extends EventEmitter {
name: options.name,
trigger_metadata: options.triggerMetadata,
trigger_type: options.triggerType,
});
reason: options.reason,
}).then((rule) => new AutoModerationRule(rule, this));
}

/**
Expand Down Expand Up @@ -1467,10 +1469,10 @@ class Client extends EventEmitter {
}

/**
* edit an existing auto moderation rule
* Edit an existing auto moderation rule
* @arg {String} guildID the ID of the guild to edit the rule in
* @arg {String} ruleID The ID of the rule to edit
* @arg {Object} options The rule to create
* @arg {Object} options The properties to edit
* @arg {Array<Object>} [options.actions] The [actions](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-action-object) done when the rule is violated
* @arg {Boolean} [options.enabled=false] If the rule is enabled, false by default
* @arg {Number} [options.eventType] The [event type](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-event-types) for the rule
Expand All @@ -1480,7 +1482,7 @@ class Client extends EventEmitter {
* @arg {Object} [options.triggerMetadata] The [trigger metadata](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-metadata) for the rule
* @arg {Number} [options.triggerType] The [trigger type](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-types) of the rule
* @arg {String} [options.reason] The reason to be displayed in audit logs
* @returns {Promise<Object>}
* @returns {Promise<AutoModerationRule>}
*/
editAutoModerationRule(guildID, ruleID, options) {
return this.requestHandler.request("PATCH", Endpoints.AUTO_MODERATION_RULE(guildID, ruleID), true, {
Expand All @@ -1492,7 +1494,8 @@ class Client extends EventEmitter {
name: options.name,
trigger_metadata: options.triggerMetadata,
trigger_type: options.triggerType,
});
reason: options.reason,
}).then((rule) => new AutoModerationRule(rule, this));
}

/**
Expand Down Expand Up @@ -2478,19 +2481,19 @@ class Client extends EventEmitter {
* Get an existing auto moderation rule
* @arg {String} guildID The ID of the guild to get the rule from
* @arg {String} ruleID The ID of the rule to get
* @returns {Promise<Object>}
* @returns {Promise<AutoModerationRule>}
*/
getAutoModerationRule(guildID, ruleID) {
return this.requestHandler.request("GET", Endpoints.AUTO_MODERATION_RULE(guildID, ruleID), true);
return this.requestHandler.request("GET", Endpoints.AUTO_MODERATION_RULE(guildID, ruleID), true).then((rule) => new AutoModerationRule(rule, this));
}

/**
* Get a guild's auto moderation rules
* @arg {String} guildID The ID of the guild to get the rules of
* @returns {Promise<Array<Object>>}
* @returns {Promise<Array<AutoModerationRule>>}
*/
getAutoModerationRules(guildID) {
return this.requestHandler.request("GET", Endpoints.AUTO_MODERATION_RULES(guildID), true);
return this.requestHandler.request("GET", Endpoints.AUTO_MODERATION_RULES(guildID), true).then((rules) => rules.map((rule) => new AutoModerationRule(rule, this)));
}

/**
Expand Down
Loading