Skip to content

Commit fc08972

Browse files
authored
Merge branch 'master' into feature/webhook-events
2 parents 62b3477 + 3274c76 commit fc08972

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1832
-675
lines changed

_examples/componentsv2/example.go

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/disgoorg/disgo"
1515
"github.com/disgoorg/disgo/bot"
1616
"github.com/disgoorg/disgo/discord"
17-
"github.com/disgoorg/disgo/events"
1817
"github.com/disgoorg/disgo/handler"
1918
)
2019

@@ -36,6 +35,15 @@ var (
3635
Required: false,
3736
},
3837
},
38+
IntegrationTypes: []discord.ApplicationIntegrationType{
39+
discord.ApplicationIntegrationTypeGuildInstall,
40+
discord.ApplicationIntegrationTypeUserInstall,
41+
},
42+
Contexts: []discord.InteractionContextType{
43+
discord.InteractionContextTypeGuild,
44+
discord.InteractionContextTypeBotDM,
45+
discord.InteractionContextTypePrivateChannel,
46+
},
3947
},
4048
}
4149
)
@@ -45,9 +53,14 @@ func main() {
4553
slog.Info("disgo version", slog.String("version", disgo.Version))
4654
slog.SetLogLoggerLevel(slog.LevelDebug)
4755

56+
r := handler.New()
57+
r.SlashCommand("/test", onTest)
58+
r.SlashCommand("/modal", onModal)
59+
r.Modal("/modal", onModalSubmit)
60+
4861
client, err := disgo.New(token,
4962
bot.WithDefaultGateway(),
50-
bot.WithEventListenerFunc(onCommand),
63+
bot.WithEventListeners(r),
5164
)
5265
if err != nil {
5366
slog.Error("error while building bot", slog.Any("err", err))
@@ -70,39 +83,54 @@ func main() {
7083
<-s
7184
}
7285

73-
func onCommand(e *events.ApplicationCommandInteractionCreate) {
74-
switch data := e.Data.(type) {
75-
case discord.SlashCommandInteractionData:
76-
flags := discord.MessageFlagIsComponentsV2
77-
if ephemeral, ok := data.OptBool("ephemeral"); !ok || ephemeral {
78-
flags = flags.Add(discord.MessageFlagEphemeral)
79-
}
80-
if err := e.CreateMessage(discord.MessageCreate{
81-
Flags: flags,
82-
Components: []discord.LayoutComponent{
83-
discord.NewContainer(
84-
discord.NewSection(
85-
discord.NewTextDisplay("**Name: [Seeing Red](https://open.spotify.com/track/65qBr6ToDUjTD1RiE1H4Gl)**"),
86-
discord.NewTextDisplay("**Artist: [Architects](https://open.spotify.com/artist/3ZztVuWxHzNpl0THurTFCv)**"),
87-
discord.NewTextDisplay("**Album: [The Sky, The Earth & All Between](https://open.spotify.com/album/2W82VyyIFAXigJEiLm5TT1)**"),
88-
).WithAccessory(discord.NewThumbnail("attachment://thumbnail.png")),
89-
discord.NewTextDisplay("`0:08`/`3:40`"),
90-
discord.NewTextDisplay("[🔘▬▬▬▬▬▬▬▬▬]"),
91-
discord.NewSmallSeparator(),
92-
discord.NewActionRow(
93-
discord.NewPrimaryButton("", "/player/previous").WithEmoji(discord.ComponentEmoji{Name: "⏮"}),
94-
discord.NewPrimaryButton("", "/player/pause_play").WithEmoji(discord.ComponentEmoji{Name: "⏯"}),
95-
discord.NewPrimaryButton("", "/player/next").WithEmoji(discord.ComponentEmoji{Name: "⏭"}),
96-
discord.NewDangerButton("", "/player/stop").WithEmoji(discord.ComponentEmoji{Name: "⏹"}),
97-
discord.NewPrimaryButton("", "/player/like").WithEmoji(discord.ComponentEmoji{Name: "❤️"}),
98-
),
99-
).WithAccentColor(0x5c5fea),
100-
},
101-
Files: []*discord.File{
102-
discord.NewFile("thumbnail.png", "", bytes.NewReader(thumbnail)),
103-
},
104-
}); err != nil {
105-
slog.Error("error while sending message", slog.Any("err", err))
106-
}
86+
func onTest(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
87+
flags := discord.MessageFlagIsComponentsV2
88+
if ephemeral, ok := data.OptBool("ephemeral"); !ok || ephemeral {
89+
flags = flags.Add(discord.MessageFlagEphemeral)
10790
}
91+
92+
return e.CreateMessage(discord.MessageCreate{
93+
Flags: flags,
94+
Components: []discord.LayoutComponent{
95+
discord.NewContainer(
96+
discord.NewSection(
97+
discord.NewTextDisplay("**Name: [Seeing Red](https://open.spotify.com/track/65qBr6ToDUjTD1RiE1H4Gl)**"),
98+
discord.NewTextDisplay("**Artist: [Architects](https://open.spotify.com/artist/3ZztVuWxHzNpl0THurTFCv)**"),
99+
discord.NewTextDisplay("**Album: [The Sky, The Earth & All Between](https://open.spotify.com/album/2W82VyyIFAXigJEiLm5TT1)**"),
100+
).WithAccessory(discord.NewThumbnail("attachment://thumbnail.png")),
101+
discord.NewTextDisplay("`0:08`/`3:40`"),
102+
discord.NewTextDisplay("[🔘▬▬▬▬▬▬▬▬▬]"),
103+
discord.NewSmallSeparator(),
104+
discord.NewActionRow(
105+
discord.NewPrimaryButton("", "/player/previous").WithEmoji(discord.ComponentEmoji{Name: "⏮"}),
106+
discord.NewPrimaryButton("", "/player/pause_play").WithEmoji(discord.ComponentEmoji{Name: "⏯"}),
107+
discord.NewPrimaryButton("", "/player/next").WithEmoji(discord.ComponentEmoji{Name: "⏭"}),
108+
discord.NewDangerButton("", "/player/stop").WithEmoji(discord.ComponentEmoji{Name: "⏹"}),
109+
discord.NewPrimaryButton("", "/player/like").WithEmoji(discord.ComponentEmoji{Name: "❤️"}),
110+
),
111+
).WithAccentColor(0x5c5fea),
112+
},
113+
Files: []*discord.File{
114+
discord.NewFile("thumbnail.png", "", bytes.NewReader(thumbnail)),
115+
},
116+
})
117+
}
118+
119+
func onModal(_ discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
120+
return e.Modal(discord.ModalCreate{
121+
CustomID: "/modal",
122+
Title: "Test Modal",
123+
Components: []discord.LayoutComponent{
124+
discord.NewTextDisplay("This is a modal"),
125+
discord.NewLabel("Test Input", discord.NewShortTextInput("test_input")),
126+
discord.NewLabel("Test Select", discord.NewUserSelectMenu("test_user", "Select a user")),
127+
},
128+
})
129+
}
130+
131+
func onModalSubmit(e *handler.ModalEvent) error {
132+
return e.CreateMessage(discord.MessageCreate{
133+
Content: "You submitted the modal!",
134+
Flags: discord.MessageFlagEphemeral,
135+
})
108136
}

_examples/echo/echo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
token = os.Getenv("disgo_token")
2424
guildID = snowflake.GetEnv("disgo_guild_id")
2525
channelID = snowflake.GetEnv("disgo_channel_id")
26+
userID = snowflake.GetEnv("disgo_user_id")
2627
)
2728

2829
func main() {
@@ -76,6 +77,7 @@ func play(client *bot.Client) {
7677
if _, err := conn.UDP().Write(voice.SilenceAudioFrame); err != nil {
7778
panic("error sending silence: " + err.Error())
7879
}
80+
7981
for {
8082
packet, err := conn.UDP().ReadPacket()
8183
if err != nil {
@@ -86,6 +88,9 @@ func play(client *bot.Client) {
8688
slog.Info("error while reading from reader", slog.Any("err", err))
8789
continue
8890
}
91+
if voiceUserID := conn.UserIDBySSRC(packet.SSRC); voiceUserID != userID {
92+
continue
93+
}
8994
if _, err = conn.UDP().Write(packet.Opus); err != nil {
9095
if errors.Is(err, net.ErrClosed) {
9196
slog.Info("connection closed")

_examples/modals/example.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"log/slog"
7+
"os"
8+
"os/signal"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/disgoorg/disgo"
13+
"github.com/disgoorg/disgo/bot"
14+
"github.com/disgoorg/disgo/discord"
15+
"github.com/disgoorg/disgo/events"
16+
"github.com/disgoorg/disgo/gateway"
17+
"github.com/disgoorg/disgo/handler"
18+
)
19+
20+
var (
21+
token = os.Getenv("disgo_token")
22+
23+
commands = []discord.ApplicationCommandCreate{
24+
discord.SlashCommandCreate{
25+
Name: "modal",
26+
Description: "brings up a modal",
27+
IntegrationTypes: []discord.ApplicationIntegrationType{
28+
discord.ApplicationIntegrationTypeUserInstall,
29+
discord.ApplicationIntegrationTypeGuildInstall,
30+
},
31+
Contexts: []discord.InteractionContextType{
32+
discord.InteractionContextTypeGuild,
33+
discord.InteractionContextTypeBotDM,
34+
discord.InteractionContextTypePrivateChannel,
35+
},
36+
},
37+
}
38+
)
39+
40+
func main() {
41+
slog.Info("starting example...")
42+
slog.Info("disgo version", slog.String("version", disgo.Version))
43+
44+
client, err := disgo.New(token,
45+
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentsNone)),
46+
bot.WithEventListenerFunc(commandListener),
47+
bot.WithEventListenerFunc(modalListener),
48+
)
49+
if err != nil {
50+
log.Fatal("error while building disgo instance: ", err)
51+
return
52+
}
53+
54+
defer client.Close(context.TODO())
55+
56+
if err = handler.SyncCommands(client, commands, nil); err != nil {
57+
log.Fatal("error while registering commands: ", err)
58+
}
59+
60+
if err = client.OpenGateway(context.TODO()); err != nil {
61+
log.Fatal("error while connecting to gateway: ", err)
62+
}
63+
64+
slog.Info("example is now running. Press CTRL-C to exit.")
65+
s := make(chan os.Signal, 1)
66+
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
67+
<-s
68+
}
69+
70+
func commandListener(event *events.ApplicationCommandInteractionCreate) {
71+
data := event.SlashCommandInteractionData()
72+
if data.CommandName() == "modal" {
73+
if err := event.Modal(discord.NewModalCreateBuilder().
74+
SetTitle("Modal Title").
75+
SetCustomID("modal-id").
76+
AddLabel("short text", discord.NewShortTextInput("short-text-input")).
77+
AddLabel("paragraph text", discord.NewParagraphTextInput("paragraph-text-input")).
78+
AddLabel("select menu", discord.NewStringSelectMenu("select-menu", "select something idiot",
79+
discord.NewStringSelectMenuOption("helo", "helo"),
80+
discord.NewStringSelectMenuOption("uwu", "uwu"),
81+
discord.NewStringSelectMenuOption("owo", "owo"),
82+
).
83+
WithMinValues(0).
84+
WithMaxValues(2),
85+
).
86+
Build(),
87+
); err != nil {
88+
event.Client().Logger.Error("error creating modal", slog.Any("err", err))
89+
}
90+
}
91+
}
92+
93+
func modalListener(event *events.ModalSubmitInteractionCreate) {
94+
var content string
95+
for component := range event.Data.AllComponents() {
96+
switch c := component.(type) {
97+
case discord.TextInputComponent:
98+
content += c.CustomID + ": " + c.Value + "\n"
99+
case discord.StringSelectMenuComponent:
100+
content += c.CustomID + ": " + strings.Join(c.Values, ", ") + "\n"
101+
}
102+
}
103+
104+
if err := event.CreateMessage(discord.MessageCreate{
105+
Content: content,
106+
}); err != nil {
107+
event.Client().Logger.Error("error creating modal", slog.Any("err", err))
108+
}
109+
}

_examples/proxy/example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func main() {
5454
gateway.WithURL(gatewayURL), // set the custom gateway url
5555
gateway.WithCompress(false), // we don't want compression as that would be additional overhead
5656
),
57-
sharding.WithRateLimiter(sharding.NewNoopRateLimiter()), // disable sharding rate limiter as the proxy handles it
57+
sharding.WithIdentifyRateLimiter(gateway.NewNoopIdentifyRateLimiter()), // disable sharding rate limiter as the proxy handles it
5858
),
5959
bot.WithRestClientConfigOpts(
6060
rest.WithURL(restURL), // set the custom rest url

_examples/sharding/example.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func main() {
2525

2626
client, err := disgo.New(token,
2727
bot.WithShardManagerConfigOpts(
28-
sharding.WithShardIDs(0, 1),
29-
sharding.WithShardCount(2),
28+
sharding.WithShardIDs(0, 1), // Remove this to use discord's default recommended shard count
29+
sharding.WithShardCount(2), // Remove this to use discord's default recommended shard count
3030
sharding.WithAutoScaling(true),
3131
sharding.WithGatewayConfigOpts(
3232
gateway.WithIntents(gateway.IntentGuilds, gateway.IntentGuildMessages, gateway.IntentDirectMessages),

_examples/test/listeners.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func componentListener(event *events.ComponentInteractionCreate) {
5555
discord.TextInputComponent{
5656
CustomID: "test_input",
5757
Style: discord.TextInputStyleShort,
58-
Label: "qwq",
5958
Required: true,
6059
Placeholder: "test placeholder",
6160
Value: "uwu",

bot/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,9 @@ func BuildClient(
374374
gateway.WithDevice(name),
375375
),
376376
sharding.WithLogger(cfg.Logger),
377-
sharding.WithDefaultRateLimiterConfigOpt(
378-
sharding.WithMaxConcurrency(gatewayBotRs.SessionStartLimit.MaxConcurrency),
379-
sharding.WithRateLimiterLogger(cfg.Logger),
377+
sharding.WithDefaultIdentifyRateLimiterConfigOpt(
378+
gateway.WithIdentifyMaxConcurrency(gatewayBotRs.SessionStartLimit.MaxConcurrency),
379+
gateway.WithIdentifyRateLimiterLogger(cfg.Logger),
380380
),
381381
}, cfg.ShardManagerConfigOpts...)
382382

cache/caches.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type ChannelCache interface {
143143

144144
Channel(channelID snowflake.ID) (discord.GuildChannel, bool)
145145
Channels() iter.Seq[discord.GuildChannel]
146+
ChannelsForGuild(guildID snowflake.ID) iter.Seq[discord.GuildChannel]
146147
ChannelsLen() int
147148
AddChannel(channel discord.GuildChannel)
148149
RemoveChannel(channelID snowflake.ID) (discord.GuildChannel, bool)
@@ -171,6 +172,18 @@ func (c *channelCacheImpl) Channels() iter.Seq[discord.GuildChannel] {
171172
return c.cache.All()
172173
}
173174

175+
func (c *channelCacheImpl) ChannelsForGuild(guildID snowflake.ID) iter.Seq[discord.GuildChannel] {
176+
return func(yield func(discord.GuildChannel) bool) {
177+
for channel := range c.Channels() {
178+
if channel.GuildID() == guildID {
179+
if !yield(channel) {
180+
return
181+
}
182+
}
183+
}
184+
}
185+
}
186+
174187
func (c *channelCacheImpl) ChannelsLen() int {
175188
return c.cache.Len()
176189
}

0 commit comments

Comments
 (0)