-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscord.go
More file actions
76 lines (69 loc) · 2.09 KB
/
discord.go
File metadata and controls
76 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"fmt"
"log"
"github.com/bwmarrin/discordgo"
)
type DiscordBot struct {
// From the Discord Developer Portal
AppToken string
// Optional. Restricts bot to 1 server. Aka Server ID. In Discord, enable "Developer Mode", then right-click on the the server's icon
GuildID string
// From the Discord Developer Portal within an app
BotToken string
Datastore *Datastore
session *discordgo.Session
}
func (b *DiscordBot) Run(ctx context.Context) func() error {
return func() error {
var err error
b.session, err = discordgo.New("Bot " + b.BotToken)
if err != nil {
return err
}
b.session.AddHandler(b.handleReady())
b.session.AddHandler(b.handleWeight())
if _, err := b.session.ApplicationCommandBulkOverwrite(b.AppToken, b.GuildID, b.buildCommands()); err != nil {
return err
}
if err := b.session.Open(); err != nil {
return err
}
defer func() { err = b.session.Close() }()
<-ctx.Done()
log.Printf("DiscordBot received Done with Error %q. Shutting down.\n", ctx.Err())
return err
}
}
func (b *DiscordBot) buildCommands() []*discordgo.ApplicationCommand {
return []*discordgo.ApplicationCommand{
{
Name: "weight",
Description: "Get the current propane level",
// Options: []*discordgo.ApplicationCommandOption{},
},
}
}
func (b *DiscordBot) handleReady() func(*discordgo.Session, *discordgo.Ready) {
return func(s *discordgo.Session, r *discordgo.Ready) {
fmt.Printf("Bot started as: %q", r.User.String())
}
}
func (b *DiscordBot) handleWeight() func(*discordgo.Session, *discordgo.InteractionCreate) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
return
}
data := i.ApplicationCommandData()
if data.Name != "weight" {
return
}
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: b.Datastore.GetString()},
}); err != nil {
fmt.Printf("Error: Failed to send response: %s", err)
}
}
}