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

Add initial implementation for automoderator api #444

Merged
merged 11 commits into from
Aug 6, 2024
72 changes: 72 additions & 0 deletions api/automoderation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package api

import (
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/httputil"
)

// Get a list of all rules currently configured for the guild. Returns a list of auto moderation rule objects for the given guild.
diamondburned marked this conversation as resolved.
Show resolved Hide resolved
//
// This endpoint requires the MANAGE_GUILD permission.
func (c *Client) ListAutoModerationRules(guildID discord.GuildID) ([]discord.AutoModerationRule, error) {
diamondburned marked this conversation as resolved.
Show resolved Hide resolved
var rules []discord.AutoModerationRule
return rules, c.RequestJSON(
&rules, "GET",
EndpointGuilds+guildID.String()+"/auto-moderation/rules",
)
}

// Get a single rule. Returns an auto moderation rule object.
//
// This endpoint requires the MANAGE_GUILD permission.
func (c *Client) GetAutoModerationRule(guildID discord.GuildID, ruleID discord.AutoModerationRuleID) (discord.AutoModerationRule, error) {
var rule discord.AutoModerationRule
return rule, c.RequestJSON(
&rule, "GET",
EndpointGuilds+guildID.String()+"/auto-moderation/rules/"+ruleID.String(),
)
}

// Create a new rule. Returns an auto moderation rule on success. Fires an Auto Moderation Rule Create Gateway event.
//
// This endpoint requires the MANAGE_GUILD permission.
//
// This endpoint supports the X-Audit-Log-Reason header.
func (c *Client) CreateAutoModerationRule(guildID discord.GuildID, rule discord.AutoModerationRule) (*discord.AutoModerationRule, error) {
var ret *discord.AutoModerationRule
return ret, c.RequestJSON(&ret, "POST", EndpointGuilds+guildID.String()+"/auto-moderation/rules",
httputil.WithJSONBody(rule),
)
}

type ModifyAutoModerationRuleData struct {
AuditLogReason
}

// Modify an existing rule. Returns an auto moderation rule on success. Fires an Auto Moderation Rule Update Gateway event.
//
// Requires MANAGE_GUILD permissions.
//
// All parameters for this endpoint are optional.
//
// This endpoint supports the X-Audit-Log-Reason header.
func (c *Client) ModifyAutoModerationRule(guildID discord.GuildID, ruleID discord.AutoModerationRuleID, rule discord.AutoModerationRule, data ModifyAutoModerationRuleData) (*discord.AutoModerationRule, error) {
diamondburned marked this conversation as resolved.
Show resolved Hide resolved
var ret *discord.AutoModerationRule
return ret, c.RequestJSON(&ret, "PATCH", EndpointGuilds+guildID.String()+"/auto-moderation/rules/"+ruleID.String(),
httputil.WithJSONBody(rule),
httputil.WithHeaders(data.Header()),
)
}

type DeleteAutoModerationRuleData struct {
AuditLogReason
}

// Delete a rule. Returns a 204 on success. Fires an Auto Moderation Rule Delete Gateway event.
//
// This endpoint requires the MANAGE_GUILD permission.
//
// This endpoint supports the X-Audit-Log-Reason header.
func (c *Client) DeleteAutoModerationRule(guildID discord.GuildID, ruleID discord.AutoModerationRuleID, data DeleteAutoModerationRuleData) error {
diamondburned marked this conversation as resolved.
Show resolved Hide resolved
return c.FastRequest("DELETE", EndpointGuilds+guildID.String()+"/auto-moderation/rules/"+ruleID.String(), httputil.WithHeaders(data.Header()))
}
93 changes: 93 additions & 0 deletions discord/automoderation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package discord

type AutoModerationEventTypes uint32

const (
AutoModerationMessageSend AutoModerationEventTypes = 1 + iota
AutoModerationMemberUpdate
)

type AutoModerationTriggerTypes uint32

const (
AutoModerationKeyword AutoModerationTriggerTypes = 1 + iota
AutoModerationSpam
AutoModerationKeywordPreset
AutoModerationMentionSpam
AutoModerationMemberProfile
)

type AutoModerationKeywordPresetTypes uint32

const (
AutoModeratorProfanity = 1 + iota
AutoModeratorSexualContent
AutoModeratorSlurs
)

type AutoModerationTriggerMetadata struct {
// substrings which will be searched for in content (Maximum of 1000)
KeywordFilter []string `json:"keyword_filter"`
// regular expression patterns which will be matched against content (Maximum of 10)
RegexPatterns []string `json:"regex_patterns"`
// the internally pre-defined wordsets which will be searched for in content
Presets []AutoModerationKeywordPresetTypes `json:"presets"`
// substrings which should not trigger the rule (Maximum of 100 or 1000)
AllowList []string `json:"allow_list"`
// total number of unique role and user mentions allowed per message (Maximum of 50)
MentionTotalLimit int `json:"mention_total_limit"`
// whether to automatically detect mention raids
MentionRaidProtectionEnabled bool `json:"mention_raid_protection_enabled"`
}

type AutoModerationActionMetadata struct {
// channel to which user content should be logged
ChannelID ChannelID `json:"channel_id"`
// timeout duration in seconds
// maximum of 2419200 seconds (4 weeks)
DurationSeconds int `json:"duration_seconds"`
// additional explanation that will be shown to members whenever their message is blocked
// maximum of 150 characters
CustomMessage string `json:"custom_message,omitempty"`
}

type AutoModerationActionTypes uint32

const (
AutoModerationBlockMessage = 1 + iota
AutoModerationSendAlertMessage
AutoModerationTimeout
AutoModerationBlockMemberInteraction
)

type AutoModerationAction struct {
// the type of action
Type AutoModerationActionTypes `json:"type"`
// additional metadata needed during execution for this specific action type
Metadata AutoModerationActionMetadata `json:"metadata,omitempty"`
}

type AutoModerationRule struct {
// the id of this rule
ID AutoModerationRuleID `json:"id"`
// the id of the guild which this rule belongs to
GuildID GuildID `json:"guild_id"`
// the rule name
Name string `json:"name"`
// the user which first created this rule
CreatorID UserID `json:"creator_id,omitempty"`
// the rule event type
EventType AutoModerationEventTypes `json:"event_type"`
// the rule trigger type
TriggerType AutoModerationTriggerTypes
// the rule trigger metadata
TriggerMetadata AutoModerationTriggerMetadata `json:"trigger_metadata,omitempty"`
// the actions which will execute when the rule is triggered
Actions []AutoModerationAction `json:"actions"`
// whether the rule is enabled
Enabled bool `json:"enabled,omitempty"`
// the role ids that should not be affected by the rule (Maximum of 20)
ExemptRoles []RoleID `json:"exempt_roles,omitempty"`
// the channel ids that should not be affected by the rule (Maximum of 50)
ExemptChannels []ChannelID `json:"exempt_channels,omitempty"`
}
2 changes: 1 addition & 1 deletion discord/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func DurationSinceEpoch(t time.Time) time.Duration {
return time.Duration(t.UnixNano()) - Epoch
}

//go:generate go run ../utils/cmd/gensnowflake -o snowflake_types.go AppID AttachmentID AuditLogEntryID ChannelID CommandID EmojiID GuildID IntegrationID InteractionID MessageID RoleID StageID StickerID StickerPackID TagID TeamID UserID WebhookID EventID EntityID
//go:generate go run ../utils/cmd/gensnowflake -o snowflake_types.go AppID AttachmentID AuditLogEntryID ChannelID CommandID EmojiID GuildID IntegrationID InteractionID MessageID RoleID StageID StickerID StickerPackID TagID TeamID UserID WebhookID EventID EntityID AutoModerationRuleID

// Mention generates the mention syntax for this channel ID.
func (s ChannelID) Mention() string { return "<#" + s.String() + ">" }
Expand Down
24 changes: 24 additions & 0 deletions discord/snowflake_types.go

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

Loading