forked from Vaultpls/Twitch-IRC-Bot
/
commands.go
129 lines (118 loc) · 3.57 KB
/
commands.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
// commands
package main
import (
"fmt"
"github.com/shkh/lastfm-go/lastfm"
"io/ioutil"
"net/http"
"strings"
)
func (bot *Bot) CmdInterpreter(username string, usermessage string) {
message := strings.ToLower(usermessage)
tempstr := strings.Split(message, " ")
for _, str := range tempstr {
if strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://") {
go bot.Message("^ " + webTitle(str))
} else if isWebsite(str) {
go bot.Message("^ " + webTitle("http://"+str))
}
}
if strings.HasPrefix(message, "!help") {
bot.Message("For help on the bot please go to http://commandanddemand.com/bot.html")
} else if strings.HasPrefix(message, "!quote") {
bot.Message(bot.getQuote())
} else if strings.HasPrefix(message, "!addquote ") {
stringpls := strings.Replace(message, "!addquote ", "", 1)
if bot.isMod(username) {
bot.quotes[stringpls] = username
bot.writeQuoteDB()
bot.Message("Quote added!")
} else {
bot.Message(username + " you are not a mod!")
}
} else if strings.HasPrefix(message, "!timeout ") {
stringpls := strings.Replace(message, "!timeout ", "", 1)
temp1 := strings.Split(stringpls, " ")
temp2 := strings.Replace(stringpls, temp1[0], "", 1)
if temp2 == "" {
temp2 = "no reason"
}
if bot.isMod(username) {
bot.timeout(temp1[0], temp2)
} else {
bot.Message(username + " you are not a mod!")
}
} else if strings.HasPrefix(message, "!ban ") {
stringpls := strings.Replace(message, "!ban ", "", 1)
temp1 := strings.Split(stringpls, " ")
temp2 := strings.Replace(stringpls, temp1[0], "", 1)
if temp2 == "" {
temp2 = "no reason"
}
if bot.isMod(username) {
bot.ban(temp1[0], temp2)
} else {
bot.Message(username + " you are not a mod!")
}
} else if message == "!song" {
api := lastfm.New("e6563970017df6d5966edfa836e12835", "dcc462ffd8a371fee5a5b49c248a2371")
temp, _ := api.User.GetRecentTracks(lastfm.P{"user": bot.lastfm})
var inserthere string
if temp.Tracks[0].Date.Date != "" {
inserthere = ". It was played on: " + temp.Tracks[0].Date.Date
}
bot.Message("Song: " + temp.Tracks[0].Artist.Name + " - " + temp.Tracks[0].Name + inserthere)
}
}
//Website stuff
func webTitle(website string) string {
response, err := http.Get(website)
if err != nil {
return "Error reading website"
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return "Error reading website"
}
if strings.Contains(string(contents), "<title>") && strings.Contains(string(contents), "</title>") {
derp := strings.Split(string(contents), "<title>")
derpz := strings.Split(derp[1], "</title>")
return derpz[0]
}
return "No title"
}
}
func isWebsite(website string) bool {
domains := []string{".com", ".net", ".org", ".info", ".fm", ".gg", ".tv"}
for _, domain := range domains {
if strings.Contains(website, domain) {
return true
}
}
return false
}
//End website stuff
//Mod stuff
func (bot *Bot) isMod(username string) bool {
temp := strings.Replace(bot.channel, "#", "", 1)
if bot.mods[username] == true || temp == username || username == "vaultpls" {
return true
}
return false
}
func (bot *Bot) timeout(username string, reason string) {
if isMod(username) {
return
}
fmt.Fprintf(bot.conn, "PRIVMSG "+bot.channel+" :/timeout "+username+"\r\n")
bot.Message(username + " was timed out(" + reason + ")!")
}
func (bot *Bot) ban(username string, reason string) {
if isMod(username) {
return
}
fmt.Fprintf(bot.conn, "PRIVMSG "+bot.channel+" :/ban "+username+"\r\n")
bot.Message(username + " was banned(" + reason + ")!")
}
//End mod stuff