Skip to content

Commit 90a29dd

Browse files
committed
add missing interface implementations & With methods
1 parent a4c16e4 commit 90a29dd

File tree

6 files changed

+352
-109
lines changed

6 files changed

+352
-109
lines changed

_examples/componentsv2/example.go

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
6+
_ "embed"
57
"log/slog"
68
"os"
79
"os/signal"
810
"syscall"
911

12+
"github.com/disgoorg/snowflake/v2"
13+
1014
"github.com/disgoorg/disgo"
1115
"github.com/disgoorg/disgo/bot"
1216
"github.com/disgoorg/disgo/discord"
1317
"github.com/disgoorg/disgo/events"
14-
"github.com/disgoorg/disgo/gateway"
15-
"github.com/disgoorg/snowflake/v2"
18+
"github.com/disgoorg/disgo/handler"
1619
)
1720

1821
var (
1922
token = os.Getenv("disgo_token")
2023
guildID = snowflake.GetEnv("disgo_guild_id")
24+
25+
//go:embed thumbnail.jpg
26+
thumbnail []byte
27+
28+
commands = []discord.ApplicationCommandCreate{
29+
discord.SlashCommandCreate{
30+
Name: "test",
31+
Description: "test",
32+
Options: []discord.ApplicationCommandOption{
33+
discord.ApplicationCommandOptionBool{
34+
Name: "ephemeral",
35+
Description: "if the message should be ephemeral",
36+
Required: false,
37+
},
38+
},
39+
},
40+
}
2141
)
2242

2343
func main() {
2444
slog.Info("starting example...")
2545
slog.Info("disgo version", slog.String("version", disgo.Version))
46+
slog.SetLogLoggerLevel(slog.LevelDebug)
2647

2748
client, err := disgo.New(token,
28-
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentGuilds, gateway.IntentGuildMessages, gateway.IntentDirectMessages)),
29-
bot.WithEventListenerFunc(func(event *events.MessageCreate) {
30-
if event.Message.Author.Bot || event.Message.Author.System {
31-
return
32-
}
33-
if event.Message.Content == "test" {
34-
_, _ = event.Client().Rest().CreateMessage(event.ChannelID, discord.NewMessageCreateBuilder().
35-
AddActionRow(discord.NewDangerButton("danger", "danger")).
36-
SetMessageReferenceByID(event.Message.ID).
37-
Build(),
38-
)
39-
}
40-
}),
41-
bot.WithEventListenerFunc(func(event *events.ComponentInteractionCreate) {
42-
if event.ButtonInteractionData().CustomID() == "danger" {
43-
_ = event.CreateMessage(discord.NewMessageCreateBuilder().SetEphemeral(true).SetContent("Ey that was danger").Build())
44-
}
45-
}),
49+
bot.WithDefaultGateway(),
50+
bot.WithEventListenerFunc(onCommand),
4651
)
4752
if err != nil {
4853
slog.Error("error while building bot", slog.Any("err", err))
4954
return
5055
}
5156
defer client.Close(context.TODO())
5257

58+
if err = handler.SyncCommands(client, commands, []snowflake.ID{guildID}); err != nil {
59+
slog.Error("error while syncing commands", slog.Any("err", err))
60+
}
61+
5362
if err = client.OpenGateway(context.TODO()); err != nil {
5463
slog.Error("error while connecting to gateway", slog.Any("err", err))
5564
return
@@ -60,3 +69,40 @@ func main() {
6069
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
6170
<-s
6271
}
72+
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+
}
107+
}
108+
}

_examples/componentsv2/thumbnail.jpg

35.1 KB
Loading

_examples/test/examplebot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"os/signal"
99
"syscall"
1010

11+
"github.com/disgoorg/snowflake/v2"
12+
1113
"github.com/disgoorg/disgo"
1214
"github.com/disgoorg/disgo/bot"
1315
"github.com/disgoorg/disgo/cache"
1416
"github.com/disgoorg/disgo/discord"
1517
"github.com/disgoorg/disgo/gateway"
16-
"github.com/disgoorg/snowflake/v2"
1718
)
1819

1920
var (

0 commit comments

Comments
 (0)