-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (87 loc) · 2.1 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
package main
import (
"discord_go_chat/pkg/commands"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
Token string
commandChan = make(chan commands.Command)
)
func init() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
if Token == "" {
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
Token = os.Getenv("DISCORD_TOKEN")
}
}
func main() {
// Create a new discord session
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error createing Discord session", err)
return
}
go commands.CommandHandler(dg, commandChan)
dg.AddHandler(handleMessage)
dg.AddHandler(ready)
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
defer dg.Close()
// Wait here until CTRL-C or other term signal is received.
fmt.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)
<-sc
}
// handleMessage checks whether the written message is a command and parses it
// in order to create a Command struct which will be send to
// command channel to invoke actions
func handleMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Do not parse bot messages
if m.Author.ID == s.State.User.ID {
return
}
// Check if the message is a command
if !strings.HasPrefix(m.Content, "!") {
return
}
args := strings.Fields(m.Content)
commandID := commands.ParseCommand(args[0])
command := commands.Command{
CommandID: commandID,
Args: args,
Message: m,
}
commandChan <- command
}
func UserInVoiceChannel(s *discordgo.Session, guildID, userID string) (result bool, err error) {
voiceState, err := s.State.VoiceState(guildID, userID)
if err != nil {
return false, err
} else {
if voiceState != nil {
return true, nil
}
fmt.Println("User not in channel")
return false, nil
}
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
// Set the playing status.
s.UpdateGameStatus(0, "!play")
}