|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + _ "embed" |
| 7 | + "log/slog" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "syscall" |
| 11 | + |
| 12 | + "github.com/disgoorg/snowflake/v2" |
| 13 | + |
| 14 | + "github.com/disgoorg/disgo" |
| 15 | + "github.com/disgoorg/disgo/bot" |
| 16 | + "github.com/disgoorg/disgo/discord" |
| 17 | + "github.com/disgoorg/disgo/events" |
| 18 | + "github.com/disgoorg/disgo/handler" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + token = os.Getenv("disgo_token") |
| 23 | + 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 | + } |
| 41 | +) |
| 42 | + |
| 43 | +func main() { |
| 44 | + slog.Info("starting example...") |
| 45 | + slog.Info("disgo version", slog.String("version", disgo.Version)) |
| 46 | + slog.SetLogLoggerLevel(slog.LevelDebug) |
| 47 | + |
| 48 | + client, err := disgo.New(token, |
| 49 | + bot.WithDefaultGateway(), |
| 50 | + bot.WithEventListenerFunc(onCommand), |
| 51 | + ) |
| 52 | + if err != nil { |
| 53 | + slog.Error("error while building bot", slog.Any("err", err)) |
| 54 | + return |
| 55 | + } |
| 56 | + defer client.Close(context.TODO()) |
| 57 | + |
| 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 | + |
| 62 | + if err = client.OpenGateway(context.TODO()); err != nil { |
| 63 | + slog.Error("error while connecting to gateway", slog.Any("err", err)) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + slog.Info("example is now running. Press CTRL-C to exit.") |
| 68 | + s := make(chan os.Signal, 1) |
| 69 | + signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) |
| 70 | + <-s |
| 71 | +} |
| 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 | +} |
0 commit comments