-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (131 loc) · 3.61 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/binary"
"io"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var buffer = make([][]byte, 0)
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file, please create one")
}
token := os.Getenv("DISCORD_TOKEN")
discord, err := discordgo.New("Bot " + token)
if err != nil {
log.Println(err)
}
err = loadSound()
if err != nil {
log.Println("Error loading sound: ", err)
log.Println("Please copy $GOPATH/src/github.com/bwmarrin/examples/airhorn/airhorn.dca to this directory.")
return
}
discord.AddHandler(messageCreate)
discord.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates
err = discord.Open()
if err != nil {
log.Println("error opening connection,", err)
}
log.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
discord.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
if strings.HasPrefix(m.Content, "!music") {
// Find the channel that the message came from.
c, err := s.State.Channel(m.ChannelID)
if err != nil {
// Could not find channel.
return
}
// Find the guild for that channel.
g, err := s.State.Guild(c.GuildID)
if err != nil {
// Could not find guild.
return
}
// Look for the message sender in that guild's current voice states.
for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID {
err = playSound(s, g.ID, vs.ChannelID)
if err != nil {
log.Println("Error playing sound:", err)
}
return
}
}
}
}
// playSound plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
return err
}
// Sleep for a specified amount of time before playing the sound
time.Sleep(250 * time.Millisecond)
// Start speaking.
vc.Speaking(true)
// Send the buffer data.
for _, buff := range buffer {
vc.OpusSend <- buff
}
// Stop speaking
vc.Speaking(false)
// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)
// Disconnect from the provided voice channel.
vc.Disconnect()
return nil
}
// loadSound attempts to load an encoded sound file from disk.
func loadSound() error {
file, err := os.Open("/home/wintltr/go/pkg/mod/github.com/bwmarrin/discordgo@v0.23.2/examples/airhorn/airhorn.dca")
if err != nil {
log.Println("Error opening dca file :", err)
return err
}
var opuslen int16
for {
// Read opus frame length from dca file.
err = binary.Read(file, binary.LittleEndian, &opuslen)
// If this is the end of the file, just return.
if err == io.EOF || err == io.ErrUnexpectedEOF {
err := file.Close()
if err != nil {
return err
}
return nil
}
if err != nil {
log.Println("Error reading from dca file :", err)
return err
}
// Read encoded pcm from dca file.
InBuf := make([]byte, opuslen)
err = binary.Read(file, binary.LittleEndian, &InBuf)
// Should not be any end of file errors
if err != nil {
log.Println("Error reading from dca file :", err)
return err
}
// Append encoded pcm data to the buffer.
buffer = append(buffer, InBuf)
}
}