This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
213 lines (174 loc) · 4.87 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
)
type config struct {
Token string `toml:"token"`
Prefix string `toml:"prefix"`
}
var (
// Zero width whitespace to replace message content
content = ""
conf = new(config)
emojiRegex = regexp.MustCompile("<(a)?:.*?:(.*?)>")
loginTime time.Time
dg *discordgo.Session
errorLog *log.Logger
infoLog *log.Logger
logF *os.File
is2Cloud *bool
)
func init() {
is2Cloud = flag.Bool("c", false, "if set, the program will exit if no config is found or is invalid")
flag.Parse()
}
func createConfig() error {
fmt.Println("Welcome and thanks for downloading 2Bot2Go!")
fmt.Println("As I couldn't find a file called config.toml, I'll assume this is your first time starting the bot, so lets get it setup!")
if err := inputToken(); err != nil {
return err
}
inputPrefix()
file, err := os.OpenFile("config/config.toml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
fmt.Println("Error creating config\n", err)
return err
}
defer file.Close()
err = toml.NewEncoder(file).Encode(conf)
if err != nil {
fmt.Println("Error creating config\n", err)
return err
}
return nil
}
func inputToken() error {
fmt.Println("\nFirst, I'll need your user token. To do that, follow these instructions:\n1. Type Ctrl-Shift-i\n2. Click on the tab labelled 'Application'\n3. Click 'Local Storage', and then https://discordapp.com")
fmt.Print("4. Then copy paste the long string of random characters here, but WITHOUT THE QUOTATION MARKS! Thats very important\nPaste your token here: ")
fmt.Scanln(&conf.Token)
if err := testLogin(); err != nil {
fmt.Println("There was an issue logging you in. Check your token and try again")
return err
}
return nil
}
func inputPrefix() {
fmt.Println("\nNext up, I'll need a prefix of your preference! This will be used to call your commands.\nYou can choose to have a space between your prefix and command after you input your prefix")
fmt.Println("Example: prefix => ||\n||help => shows the help menu")
fmt.Print("\nType your chosen prefix here: ")
fmt.Scanln(&conf.Prefix)
var ws string
for {
fmt.Print("Do you want a space at the end of your prefix? (y/n) ")
fmt.Scanln(&ws)
switch strings.ToLower(ws) {
case "y":
conf.Prefix += " "
return
case "n":
return
default:
fmt.Println("\nPlease type y or n")
}
}
}
func testLogin() error {
fmt.Println("Trying to login...")
infoLog.Println("Trying to login...")
var err error
dg, err = discordgo.New(conf.Token)
if err != nil {
return err
}
if _, err = dg.User("@me"); err != nil {
return err
}
fmt.Println("\rToken is valid!")
infoLog.Println("\rToken is valid!")
return nil
}
func loadConfig() error {
if _, err := toml.DecodeFile("config/config.toml", conf); err != nil {
return err
}
return nil
}
func openLog() *os.File {
f, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
return f
}
func main() {
logF = openLog()
defer logF.Close()
log.SetOutput(logF)
infoLog = log.New(logF, "INFO: ", log.Ldate|log.Ltime)
errorLog = log.New(logF, "ERROR: ", log.Ldate|log.Ltime)
infoLog.Println("log opened")
err := loadConfig()
switch {
case os.IsNotExist(err):
if !*is2Cloud {
if err = createConfig(); err != nil {
fmt.Println(err)
errorLog.Fatalln(err)
}
} else {
fmt.Println("config doesnt exist")
errorLog.Fatalln("config doesnt exist")
}
case err != nil:
fmt.Println(err)
errorLog.Fatalln(err)
}
if conf.Prefix == "" || conf.Token == "" {
fmt.Println("bad config")
errorLog.Fatalln("bad config")
}
fmt.Println("Prefix is " + conf.Prefix)
//infoLog.Println("Prefix is " + conf.Prefix)
if dg == nil {
if err := testLogin(); err != nil {
fmt.Println(err)
errorLog.Fatalln(err)
}
}
loginTime = time.Now()
if err := dg.Open(); err != nil {
fmt.Println(err)
errorLog.Fatalln(err)
}
defer dg.Close()
prepareCommands()
dg.AddHandlerOnce(ready)
dg.AddHandler(message)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
func ready(s *discordgo.Session, m *discordgo.Ready) {
fmt.Printf("Log-in successful! Log-in time: %.2f\n", time.Since(loginTime).Seconds())
fmt.Printf("Joined %d guilds\n", len(m.Guilds))
fmt.Println("Type Ctrl+C to quit 2Bot2Go")
infoLog.Printf("Log-in successful! Log-in time: %.2f\n", time.Since(loginTime).Seconds())
infoLog.Printf("Joined %d guilds\n", len(m.Guilds))
}
func message(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID != s.State.User.ID || !strings.HasPrefix(m.Content, conf.Prefix) || len(strings.TrimPrefix(m.Content, conf.Prefix)) == 0 {
return
}
parseCommand(s, m, strings.TrimPrefix(m.Content, conf.Prefix))
}