Skip to content
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
63 changes: 63 additions & 0 deletions _examples/default_allowed_mentions/example.go
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a dumb example, IDK
I mainly included this to help reviewers test

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/rest"
)

func main() {
slog.Info("starting example...")
slog.Info("disgo version", slog.String("version", disgo.Version))

client, err := disgo.New(os.Getenv("disgo_token"),
bot.WithGatewayConfigOpts(
gateway.WithAutoReconnect(true),
gateway.WithIntents(
gateway.IntentGuildMessages,
gateway.IntentMessageContent,
),
),
bot.WithRestConfigOpts(
rest.WithDefaultAllowedMentions(discord.AllowedMentions{ }),
),
bot.WithEventListenerFunc(onMessageCreate),
)
if err != nil {
slog.Error("error while building disgo", slog.Any("err", err))
return
}

defer client.Close(context.TODO())

if err = client.OpenGateway(context.TODO()); err != nil {
slog.Error("errors while connecting to gateway", slog.Any("err", err))
return
}

slog.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}

func onMessageCreate(event *events.MessageCreate) {
if event.Message.Author.Bot {
return
}
if event.Message.Content == "!ping" {
_, err := event.Client().Rest.CreateMessage(event.ChannelID, discord.MessageCreate{Content: "@everyone"})
if err != nil {
slog.Error("error", slog.Any("err", err))
}
}
}
16 changes: 12 additions & 4 deletions bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ type config struct {
Logger *slog.Logger

RestClient rest.Client
RestClientConfigOpts []rest.ConfigOpt
RestClientConfigOpts []rest.ClientConfigOpt
Rest rest.Rest
RestConfigOpts []rest.ConfigOpt

EventManager EventManager
EventManagerConfigOpts []EventManagerConfigOpt
Expand Down Expand Up @@ -77,7 +78,7 @@ func WithRestClient(restClient rest.Client) ConfigOpt {
}

// WithRestClientConfigOpts let's you configure the default rest.Client.
func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt {
func WithRestClientConfigOpts(opts ...rest.ClientConfigOpt) ConfigOpt {
return func(config *config) {
config.RestClientConfigOpts = append(config.RestClientConfigOpts, opts...)
}
Expand All @@ -90,6 +91,13 @@ func WithRest(rest rest.Rest) ConfigOpt {
}
}

// WithRestConfigOpts lets you configure the default rest.Rest.
func WithRestConfigOpts(opts ...rest.ConfigOpt) ConfigOpt {
return func(config *config) {
config.RestConfigOpts = append(config.RestConfigOpts, opts...)
}
}

// WithEventManager lets you inject your own EventManager.
func WithEventManager(eventManager EventManager) ConfigOpt {
return func(config *config) {
Expand Down Expand Up @@ -244,7 +252,7 @@ func BuildClient(

if cfg.RestClient == nil {
// prepend standard user-agent. this can be overridden as it's appended to the front of the slice
cfg.RestClientConfigOpts = append([]rest.ConfigOpt{
cfg.RestClientConfigOpts = append([]rest.ClientConfigOpt{
rest.WithUserAgent(fmt.Sprintf("DiscordBot (%s, %s)", github, version)),
rest.WithLogger(client.Logger),
rest.WithDefaultRateLimiterConfigOpts(
Expand All @@ -256,7 +264,7 @@ func BuildClient(
}

if cfg.Rest == nil {
cfg.Rest = rest.New(cfg.RestClient)
cfg.Rest = rest.New(cfg.RestClient, cfg.RestConfigOpts...)
}
client.Rest = cfg.Rest

Expand Down
8 changes: 0 additions & 8 deletions discord/allowed_mentions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package discord

import "github.com/disgoorg/snowflake/v2"

// DefaultAllowedMentions gives you the default AllowedMentions for a Message
var DefaultAllowedMentions = AllowedMentions{
Parse: []AllowedMentionType{AllowedMentionTypeUsers, AllowedMentionTypeRoles, AllowedMentionTypeEveryone},
Roles: []snowflake.ID{},
Users: []snowflake.ID{},
RepliedUser: true,
}

// AllowedMentions are used for avoiding mentioning users in Message and Interaction
type AllowedMentions struct {
Parse []AllowedMentionType `json:"parse"`
Expand Down
6 changes: 1 addition & 5 deletions discord/message_create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ type MessageCreateBuilder struct {

// NewMessageCreateBuilder creates a new MessageCreateBuilder to be built later
func NewMessageCreateBuilder() *MessageCreateBuilder {
return &MessageCreateBuilder{
MessageCreate: MessageCreate{
AllowedMentions: &DefaultAllowedMentions,
},
}
return &MessageCreateBuilder{}
}

// SetContent sets the content of the Message
Expand Down
6 changes: 1 addition & 5 deletions discord/webhook_message_create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ type WebhookMessageCreateBuilder struct {

// NewWebhookMessageCreateBuilder creates a new WebhookMessageCreateBuilder to be built later
func NewWebhookMessageCreateBuilder() *WebhookMessageCreateBuilder {
return &WebhookMessageCreateBuilder{
WebhookMessageCreate: WebhookMessageCreate{
AllowedMentions: &DefaultAllowedMentions,
},
}
return &WebhookMessageCreateBuilder{}
}

// SetContent sets content of the Message
Expand Down
6 changes: 1 addition & 5 deletions discord/webhook_message_update_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ type WebhookMessageUpdateBuilder struct {

// NewWebhookMessageUpdateBuilder creates a new WebhookMessageUpdateBuilder to be built later
func NewWebhookMessageUpdateBuilder() *WebhookMessageUpdateBuilder {
return &WebhookMessageUpdateBuilder{
WebhookMessageUpdate: WebhookMessageUpdate{
AllowedMentions: &DefaultAllowedMentions,
},
}
return &WebhookMessageUpdateBuilder{}
}

// SetContent sets content of the Message
Expand Down
6 changes: 3 additions & 3 deletions oauth2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func defaultConfig() config {
type config struct {
Logger *slog.Logger
RestClient rest.Client
RestClientConfigOpts []rest.ConfigOpt
RestClientConfigOpts []rest.ClientConfigOpt
OAuth2 rest.OAuth2
StateController StateController
StateControllerConfigOpts []StateControllerConfigOpt
Expand All @@ -30,7 +30,7 @@ func (c *config) apply(opts []ConfigOpt) {
}
c.Logger = c.Logger.With(slog.String("name", "oauth2"))
if c.RestClient == nil {
c.RestClient = rest.NewClient("", append([]rest.ConfigOpt{rest.WithLogger(c.Logger)}, c.RestClientConfigOpts...)...)
c.RestClient = rest.NewClient("", append([]rest.ClientConfigOpt{rest.WithLogger(c.Logger)}, c.RestClientConfigOpts...)...)
}
if c.OAuth2 == nil {
c.OAuth2 = rest.NewOAuth2(c.RestClient)
Expand All @@ -55,7 +55,7 @@ func WithRestClient(restClient rest.Client) ConfigOpt {
}

// WithRestClientConfigOpts applies rest.ConfigOpt for the rest.Client to the OAuth2 client
func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt {
func WithRestClientConfigOpts(opts ...rest.ClientConfigOpt) ConfigOpt {
return func(config *config) {
config.RestClientConfigOpts = append(config.RestClientConfigOpts, opts...)
}
Expand Down
13 changes: 10 additions & 3 deletions rest/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

var _ Channels = (*channelImpl)(nil)

func NewChannels(client Client) Channels {
return &channelImpl{client: client}
func NewChannels(client Client, defaultAllowedMentions discord.AllowedMentions) Channels {
return &channelImpl{client: client, defaultAllowedMentions: defaultAllowedMentions}
}

type Channels interface {
Expand Down Expand Up @@ -58,7 +58,8 @@ type Channels interface {
}

type channelImpl struct {
client Client
client Client
defaultAllowedMentions discord.AllowedMentions
}

func (s *channelImpl) GetChannel(channelID snowflake.ID, opts ...RequestOpt) (channel discord.Channel, err error) {
Expand Down Expand Up @@ -148,6 +149,9 @@ func (s *channelImpl) GetMessagesPage(channelID snowflake.ID, startID snowflake.
}

func (s *channelImpl) CreateMessage(channelID snowflake.ID, messageCreate discord.MessageCreate, opts ...RequestOpt) (message *discord.Message, err error) {
if messageCreate.AllowedMentions == nil {
messageCreate.AllowedMentions = &s.defaultAllowedMentions
}
body, err := messageCreate.ToBody()
if err != nil {
return
Expand All @@ -157,6 +161,9 @@ func (s *channelImpl) CreateMessage(channelID snowflake.ID, messageCreate discor
}

func (s *channelImpl) UpdateMessage(channelID snowflake.ID, messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...RequestOpt) (message *discord.Message, err error) {
if messageUpdate.AllowedMentions == nil && (messageUpdate.Content != nil || messageUpdate.Flags.Has(discord.MessageFlagIsComponentsV2)) {
messageUpdate.AllowedMentions = &s.defaultAllowedMentions
}
body, err := messageUpdate.ToBody()
if err != nil {
return
Expand Down
20 changes: 17 additions & 3 deletions rest/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

var _ Interactions = (*interactionImpl)(nil)

func NewInteractions(client Client) Interactions {
return &interactionImpl{client: client}
func NewInteractions(client Client, defaultAllowedMentions discord.AllowedMentions) Interactions {
return &interactionImpl{client: client, defaultAllowedMentions: defaultAllowedMentions}
}

type Interactions interface {
Expand All @@ -26,7 +26,8 @@ type Interactions interface {
}

type interactionImpl struct {
client Client
client Client
defaultAllowedMentions discord.AllowedMentions
}

func (s *interactionImpl) GetInteractionResponse(interactionID snowflake.ID, interactionToken string, opts ...RequestOpt) (message *discord.Message, err error) {
Expand All @@ -37,6 +38,11 @@ func (s *interactionImpl) GetInteractionResponse(interactionID snowflake.ID, int
// CreateInteractionResponse responds to the interaction without returning the callback.
// If you need the callback, use CreateInteractionResponseWithCallback.
func (s *interactionImpl) CreateInteractionResponse(interactionID snowflake.ID, interactionToken string, interactionResponse discord.InteractionResponse, opts ...RequestOpt) error {
messageCreate, ok := interactionResponse.Data.(*discord.MessageCreate)
if ok && messageCreate.AllowedMentions == nil {
messageCreate.AllowedMentions = &s.defaultAllowedMentions
}

body, err := interactionResponse.ToBody()
if err != nil {
return err
Expand All @@ -58,6 +64,10 @@ func (s *interactionImpl) CreateInteractionResponseWithCallback(interactionID sn
}

func (s *interactionImpl) UpdateInteractionResponse(applicationID snowflake.ID, interactionToken string, messageUpdate discord.MessageUpdate, opts ...RequestOpt) (message *discord.Message, err error) {
if messageUpdate.AllowedMentions == nil && (messageUpdate.Content != nil && messageUpdate.Flags.Has(discord.MessageFlagIsComponentsV2)) {
messageUpdate.AllowedMentions = &s.defaultAllowedMentions
}

body, err := messageUpdate.ToBody()
if err != nil {
return
Expand Down Expand Up @@ -87,6 +97,10 @@ func (s *interactionImpl) CreateFollowupMessage(applicationID snowflake.ID, inte
}

func (s *interactionImpl) UpdateFollowupMessage(applicationID snowflake.ID, interactionToken string, messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...RequestOpt) (message *discord.Message, err error) {
if messageUpdate.AllowedMentions == nil && (messageUpdate.Content != nil && messageUpdate.Flags.Has(discord.MessageFlagIsComponentsV2)) {
messageUpdate.AllowedMentions = &s.defaultAllowedMentions
}

body, err := messageUpdate.ToBody()
if err != nil {
return
Expand Down
11 changes: 7 additions & 4 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type Rest interface {
var _ Rest = (*restImpl)(nil)

// New returns a new default Rest
func New(client Client) Rest {
func New(client Client, opts ...ConfigOpt) Rest {
cfg := defaultConfig()
cfg.apply(opts)

return &restImpl{
Client: client,
Applications: NewApplications(client),
Expand All @@ -38,14 +41,14 @@ func New(client Client) Rest {
Guilds: NewGuilds(client),
AutoModeration: NewAutoModeration(client),
Members: NewMembers(client),
Channels: NewChannels(client),
Channels: NewChannels(client, cfg.DefaultAllowedMentions),
Threads: NewThreads(client),
Interactions: NewInteractions(client),
Interactions: NewInteractions(client, cfg.DefaultAllowedMentions),
Invites: NewInvites(client),
GuildTemplates: NewGuildTemplates(client),
Users: NewUsers(client),
Voice: NewVoice(client),
Webhooks: NewWebhooks(client),
Webhooks: NewWebhooks(client, cfg.DefaultAllowedMentions),
SoundboardSounds: NewSoundboardSounds(client),
StageInstances: NewStageInstances(client),
Emojis: NewEmojis(client),
Expand Down
6 changes: 3 additions & 3 deletions rest/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

// NewClient constructs a new Client with the given config struct
func NewClient(botToken string, opts ...ConfigOpt) Client {
cfg := defaultConfig()
func NewClient(botToken string, opts ...ClientConfigOpt) Client {
cfg := defaultClientConfig()
cfg.apply(opts)

return &clientImpl{
Expand All @@ -43,7 +43,7 @@ type Client interface {

type clientImpl struct {
botToken string
config config
config clientConfig
}

func (c *clientImpl) Close(ctx context.Context) {
Expand Down
Loading
Loading