/
discord.go
131 lines (108 loc) · 2.9 KB
/
discord.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package discord
import (
"context"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/tvanriel/ps-bot-2/internal/commands"
"github.com/tvanriel/ps-bot-2/internal/metrics"
"github.com/tvanriel/ps-bot-2/internal/player"
"github.com/tvanriel/ps-bot-2/internal/queues"
"github.com/tvanriel/ps-bot-2/internal/repositories"
"go.uber.org/fx"
"go.uber.org/zap"
)
type DiscordBot struct {
Ready bool
Conn *discordgo.Session
Log *zap.Logger
Repo *repositories.GuildRepository
Player *player.Player
Exe *commands.Executor
Queue *queues.MessageQueue
Metrics *metrics.MetricsCollector
}
type NewDiscordParams struct {
fx.In
Config *Configuration
Log *zap.Logger
Repo *repositories.GuildRepository
Player *player.Player
Exe *commands.Executor
Queue *queues.MessageQueue
Metrics *metrics.MetricsCollector
}
func NewDiscord(p NewDiscordParams, lc fx.Lifecycle) (*DiscordBot, error) {
ses, err := discordgo.New("Bot " + p.Config.BotToken)
if err != nil {
return nil, err
}
ses.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
bot := &DiscordBot{
Conn: ses,
Repo: p.Repo,
Ready: false,
Log: p.Log.Named("discord"),
Exe: p.Exe,
Player: p.Player,
Queue: p.Queue,
Metrics: p.Metrics,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
go func() {
bot.Connect()
}()
return nil
},
})
return bot, nil
}
func (d *DiscordBot) AddHandlers() error {
d.Conn.AddHandler(messagehandler(d))
d.Conn.AddHandler(ready(d))
d.Conn.AddHandler(guildCreate(d))
return nil
}
func (d *DiscordBot) ListenQueuedMessages() error {
msgs := d.Queue.Consume()
go func() {
for m := range msgs {
d.Log.Info("Send message from AMQP chan",
zap.String("channel", m.ChannelID),
zap.String("content", m.Content),
)
if m.ChannelID == "" || m.Content == "" {
d.Log.Error("Invalid message request from AMQP chan",
zap.String("channel", m.ChannelID),
zap.String("content", m.Content),
)
return
}
content := escapeDiscordMessage(m.Content)
_, err := d.Conn.ChannelMessageSend(m.ChannelID, content)
if err != nil {
d.Log.Error("error while listening to queued messages",
zap.Error(err),
zap.String("channel", m.ChannelID),
zap.String("content", m.Content),
)
}
}
}()
return nil
}
func (d *DiscordBot) Connect() error {
return d.Conn.Open()
}
func (d *DiscordBot) JoinVoiceChannels() {
d.Repo.GetVoiceChannels()
}
func (d *DiscordBot) PlayVoiceCommand(s *discordgo.Session, sound string, guildId string) {
fmt.Println(sound, guildId)
}
func escapeDiscordMessage(s string) string {
s = strings.ReplaceAll(s, "@", "")
s = strings.ReplaceAll(s, "#", "")
return s
}