Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 31a239c

Browse files
committed
Prepared for first release
1 parent a7ab711 commit 31a239c

2 files changed

Lines changed: 51 additions & 23 deletions

File tree

main.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var (
2525
conf = &config{}
2626
emojiRegex = regexp.MustCompile("<:(.*?):(.*?)>")
2727
loginTime time.Time
28+
errorLog *log.Logger
29+
infoLog *log.Logger
30+
logF *os.File
2831
)
2932

3033
func createConfig() error {
@@ -55,11 +58,9 @@ func createConfig() error {
5558
}
5659

5760
func inputToken() error {
58-
fmt.Println("\nFirst, I'll need your user token. To do that, follow these instructions:")
59-
fmt.Println("1. Type Ctrl-Shift-i\n2. Click on the tab labelled 'Application'\n3. Click 'Local Storage', and then https://discordapp.com")
60-
fmt.Println("4. Then copy paste the long string of random characters here, but WITHOUT THE QUOTATION MARKS! Thats very important")
61+
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")
62+
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: ")
6163

62-
fmt.Print("\nPaste your token here: ")
6364
fmt.Scanln(&conf.Token)
6465
err := testLogin()
6566
if err != nil {
@@ -71,13 +72,14 @@ func inputToken() error {
7172
}
7273

7374
func inputPrefix() {
74-
fmt.Println("\nNext up, I'll need a prefix of your preference! This will be used to call your commands.")
75+
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")
7576
fmt.Println("Example: prefix => ||\n||help => shows the help menu")
7677
fmt.Print("\nType your chosen prefix here: ")
78+
7779
fmt.Scanln(&conf.Prefix)
78-
fmt.Print("Do you want a space at the end of your prefix? (y/n) ")
7980
var ws string
8081
for {
82+
fmt.Print("Do you want a space at the end of your prefix? (y/n) ")
8183
fmt.Scanln(&ws)
8284
switch strings.ToLower(ws) {
8385
case "y":
@@ -86,14 +88,14 @@ func inputPrefix() {
8688
case "n":
8789
return
8890
default:
89-
fmt.Println("Please type y or n")
91+
fmt.Println("\nPlease type y or n")
9092
}
9193
}
92-
9394
}
9495

9596
func testLogin() error {
96-
fmt.Println("Trying to login... ")
97+
fmt.Println("Trying to login...")
98+
infoLog.Println("Trying to login...")
9799

98100
var err error
99101
dg, err = discordgo.New(conf.Token)
@@ -107,6 +109,8 @@ func testLogin() error {
107109
}
108110

109111
fmt.Println("\rToken is valid!")
112+
infoLog.Println("\rToken is valid!")
113+
110114
return nil
111115
}
112116

@@ -124,28 +128,50 @@ func loadConfig() error {
124128
return nil
125129
}
126130

131+
func openLog() *os.File {
132+
f, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
133+
if err != nil {
134+
fmt.Println(err)
135+
os.Exit(2)
136+
}
137+
return f
138+
}
139+
127140
func main() {
141+
logF = openLog()
142+
defer logF.Close()
143+
144+
log.SetOutput(logF)
145+
146+
infoLog = log.New(logF, "INFO: ", log.Ldate|log.Ltime)
147+
errorLog = log.New(logF, "ERROR: ", log.Ldate|log.Ltime)
128148

149+
infoLog.Println("log opened")
150+
129151
_, err := toml.DecodeFile("config.toml", &conf)
130152
if os.IsNotExist(err) {
131153
if err = createConfig(); err != nil {
132-
log.Fatalln(err)
154+
fmt.Println(err)
155+
errorLog.Fatalln(err)
133156
}
134157
} else {
135158
if err = loadConfig(); err != nil || conf.Prefix == "" || conf.Token == "" {
136-
log.Fatalln(err)
159+
fmt.Println(err)
160+
errorLog.Fatalln(err)
137161
}
138162
}
139163

140164
if dg == nil {
141165
if err = testLogin(); err != nil {
142-
log.Fatalln(err)
166+
fmt.Println(err)
167+
errorLog.Fatalln(err)
143168
}
144169
}
145170

146171
loginTime = time.Now()
147172
if err = dg.Open(); err != nil {
148-
log.Fatalln(err)
173+
fmt.Println(err)
174+
errorLog.Fatalln(err)
149175
}
150176

151177
dg.AddHandlerOnce(ready)
@@ -162,6 +188,10 @@ func main() {
162188
func ready(s *discordgo.Session, m *discordgo.Ready) {
163189
fmt.Printf("Log-in successful! Log-in time: %.2f\n", time.Since(loginTime).Seconds())
164190
fmt.Printf("Joined %d guilds\n", len(m.Guilds))
191+
fmt.Println("Type Ctrl+C to quit 2Bot2Go")
192+
193+
infoLog.Printf("Log-in successful! Log-in time: %.2f\n", time.Since(loginTime).Seconds())
194+
infoLog.Printf("Joined %d guilds\n", len(m.Guilds))
165195
}
166196

167197
func message(s *discordgo.Session, m *discordgo.MessageCreate) {

msgEmoji.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,33 @@ func emojiFile(s string) string {
3131

3232
func msgEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
3333
submatch := emojiRegex.FindStringSubmatch(msglist[0])
34-
fmt.Println(submatch)
34+
3535
if len(submatch) == 3 {
3636
emojiID := submatch[2]
3737

3838
resp, err := http.Get(fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.png", emojiID))
3939
if err != nil {
40-
fmt.Println("Custom emoji err:", err.Error())
40+
errorLog.Println("Custom emoji err:", err.Error())
4141
return
4242
}
4343
defer resp.Body.Close()
4444

4545
s.ChannelFileSend(m.ChannelID, "emoji.png", resp.Body)
4646

47-
if m != nil {
48-
s.ChannelMessageDelete(m.ChannelID, m.ID)
49-
}
47+
s.ChannelMessageDelete(m.ChannelID, m.ID)
5048
} else {
5149
emoji := emojiFile(msglist[0])
5250
if emoji != "" {
5351
file, err := os.Open(fmt.Sprintf("emoji/%s.png", emoji))
5452
if err != nil {
55-
fmt.Println("Twemoji emoji err:", err.Error())
53+
errorLog.Println("Twemoji emoji err:", err.Error())
5654
return
5755
}
5856
defer file.Close()
5957

6058
s.ChannelFileSend(m.ChannelID, "emoji.png", file)
6159

62-
if m != nil {
63-
s.ChannelMessageDelete(m.ChannelID, m.ID)
64-
}
60+
s.ChannelMessageDelete(m.ChannelID, m.ID)
6561
}
6662
}
6763
return
@@ -120,6 +116,8 @@ func msgFindEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []st
120116
Fields: emojisEmbed,
121117
})
122118
if err != nil {
123-
fmt.Println(err)
119+
errorLog.Println(err)
124120
}
121+
122+
s.ChannelMessageDelete(m.ChannelID, m.Message.ID)
125123
}

0 commit comments

Comments
 (0)