This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (99 loc) · 3.19 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
package main
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/jokerdan/phosphorescent/commands"
)
// == Config =================================================================
type configuration struct {
BotInfo struct {
Username string `json:"username"`
Token string `json:"token"`
} `json:"botuser"`
AVAPIKey string `json:"alphaVantageAPIKey,omitempty"`
}
func loadConfig(file string) (configuration, error) {
var config configuration
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
return config, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
return config, err
}
var config configuration
// == Main ===================================================================
func main() {
fmt.Println("\nStarting Bot...")
fmt.Println("+ bwmarrin/discordgo Version:", discordgo.APIVersion)
// -- Loading Config -------------------------------------------------------
var err error
config, err = loadConfig("./config.json")
if err != nil {
fmt.Println("! Error with configuration file.", err)
os.Exit(1)
}
fmt.Println("+ Config Loaded!")
// -- Set Up Bot -----------------------------------------------------------
fmt.Println("+ Bot Username: ", config.BotInfo.Username)
disgo, err := discordgo.New("Bot " + config.BotInfo.Token)
if err != nil {
fmt.Println("! Error setting up bot.", err)
}
disgo.Open()
defer disgo.Close()
fmt.Println("+ Connection Now Open!")
// -- Register Event Handlers ----------------------------------------------
disgo.AddHandler(commandDispatcher)
// -- Hold The Program -----------------------------------------------------
fmt.Printf("Bot is now running. Press CTRL-C to exit.\n")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
// == Other ==================================================================
// Test of handling messages, will move to message/command parsing within a dir of commands
func commandDispatcher(session *discordgo.Session, message *discordgo.MessageCreate) {
msg := message
// If the message is from the bot itself, ignore it
if msg.Author.ID == session.State.User.ID {
return
}
// Command Prefix
if msg.Content[0] == ';' {
msg.Content = msg.Content[1:]
} else {
return
}
// Safety First
channel, err := session.State.Channel(message.ChannelID)
if err != nil {
return // Could not find channel
}
_, err = session.State.Guild(channel.GuildID)
if err != nil {
return // Could not find guild
}
if msg.Content == "ping" {
session.ChannelMessageSend(message.ChannelID, "Pong!")
}
if msg.Content == "pong" {
session.ChannelMessageSend(message.ChannelID, "Ping!")
}
// Commands/stocks
if strings.HasPrefix(msg.Content, "stocks") {
stocks := strings.Fields(strings.TrimPrefix(msg.Content, "stocks"))
session.ChannelMessageSendEmbed(message.ChannelID, commands.GetStock(stocks, config.AVAPIKey))
}
if strings.HasPrefix(msg.Content, "trucker") {
truckerName := strings.Fields(strings.TrimPrefix(msg.Content, "trucker"))
session.ChannelMessageSendEmbed(message.ChannelID, commands.TruckerInfo(truckerName[0]))
}
}