This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
145 lines (117 loc) · 3.18 KB
/
bot.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
package main
import (
"context"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"github.com/bwmarrin/discordgo"
"github.com/gompus/snowflake"
"github.com/joho/godotenv"
"github.com/lukasl-dev/waterlink/v2"
lyrics "github.com/rhnvrm/lyric-api-go"
"github.com/zmb3/spotify/v2"
"golang.org/x/oauth2/clientcredentials"
)
var Bot Client
var BotCommands []Command = []Command{
HelpCommand,
AboutCommand,
PingCommand,
UptimeCommand,
PlayCommand,
QueueCommand,
SkipCommand,
PauseCommand,
ResumeCommand,
LeaveCommand,
ClearCommand,
JumpCommand,
RepeatCommand,
ReverseCommand,
SortCommand,
VolumeCommand,
ShuffleCommand,
NowPlayingCommand,
RemoveCommand,
UpCommingCommand,
PlayNextCommand,
MoveCommand,
KeepAliveCommand,
LyricsCommand,
SeekCommand,
}
func main() {
err := godotenv.Load()
Bot.StartTime = time.Now()
Bot.SpotifyContext = context.Background()
config := &clientcredentials.Config{
ClientID: os.Getenv("SPOTIFY_ID"),
ClientSecret: os.Getenv("SPOTIFY_SECRET"),
TokenURL: spotifyauth.TokenURL,
}
Bot.SpotifyToken, err = config.Token(Bot.SpotifyContext)
if err != nil {
log.Fatalf("Couldn't get spotify token: %v", err)
}
Bot.SpotifyHTTPClient = spotifyauth.New().Client(Bot.SpotifyContext, Bot.SpotifyToken)
Bot.SpotifyClient = spotify.New(Bot.SpotifyHTTPClient)
Bot.LyricsClient = lyrics.New()
idle, err := strconv.Atoi(os.Getenv("IDLE_TIME"))
if err != nil {
log.Fatalf("Error loading .env file")
return
}
Bot.BotConfig = Config{
Token: os.Getenv("TOKEN"),
LavalinkHost: os.Getenv("LAVALINK_HOST"),
LavalinkPass: os.Getenv("LAVALINK_PASS"),
BotIdentificator: os.Getenv("BOT_IDENTIFICATOR"),
BotID: os.Getenv("BOT_ID"),
GuildID: os.Getenv("GUILD_ID"),
ControlRole: os.Getenv("CONTROL_ROLE_ID"),
IdleTime: uint(idle),
}
if err != nil {
log.Fatal("Error loading .env file")
}
Bot.Session, Bot.Error_session = discordgo.New("Bot " + os.Getenv("TOKEN"))
if Bot.Error_session != nil {
log.Fatalln("error creating Discord session, ", err)
return
}
Bot.Session.StateEnabled = true
Bot.Session.State.TrackVoice = true
Bot.Session.State.TrackChannels = true
Bot.Session.AddHandler(Bot.Ready)
Bot.Session.AddHandler(Bot.MessageCreate)
Bot.Session.AddHandler(Bot.VoiceUpdate)
Bot.Session.AddHandler(Bot.VoiceStateUpdate)
Bot.Session.Identify.Intents = discordgo.IntentsAll
err = Bot.Session.Open()
if err != nil {
log.Fatalln("error opening connection,", err)
}
defer Bot.Session.Close()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
func setupCommands(client *Client) {
client.Commands = make(map[string]Command)
for i := 0; i < len(BotCommands); i += 1 {
client.Commands[BotCommands[i].Name] = BotCommands[i]
for j := 0; j < len(BotCommands[i].Aliases); j += 1 {
client.Commands[BotCommands[i].Aliases[j]] = BotCommands[i]
}
}
}
func (client *Client) credentials() waterlink.Credentials {
return waterlink.Credentials{
Authorization: client.BotConfig.LavalinkPass,
UserID: snowflake.MustParse(client.Session.State.User.ID),
}
}