/
quoteplugin.go
163 lines (145 loc) · 3.23 KB
/
quoteplugin.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
package quoteplugin
import (
"io/ioutil"
"math/rand"
"strconv"
"strings"
"sync"
"fmt"
"os"
"encoding/json"
"github.com/Krognol/dgofw"
"github.com/google/go-github/github"
)
type Server struct {
ID string `json:"id"`
Quotes []string `json:"quotes"`
}
type Quotes struct {
sync.RWMutex
Servers []*Server `json:"servers"`
}
func NewQuotePlugin() *Quotes {
plugin := &Quotes{Servers: make([]*Server, 0)}
err := plugin.Load()
if err != nil {
fmt.Println(err.Error())
plugin.Save()
}
return plugin
}
var QuotesHelp = []string{
"quote -- returns a random quote",
"quote [index] -- returns a quote",
"quote add [quote] -- adds a quote",
"quote del [id] -- deletes a quote with the given id, e.g. 163",
"quote list -- creates a gist with every quote",
}
func (q *Quotes) getServer(id string) *Server {
for _, s := range q.Servers {
if s.ID == id {
return s
}
}
return nil
}
func (q *Quotes) addQuote(m *dgofw.DiscordMessage) {
if quote := m.Arg("arg2"); quote != "" {
s := q.getServer(m.GuildID())
if s == nil {
s = &Server{
ID: m.GuildID(),
Quotes: make([]string, 0),
}
}
q.Lock()
s.Quotes = append(s.Quotes, quote)
q.Servers = append(q.Servers, s)
q.Unlock()
q.Save()
m.Reply(fmt.Sprintf("Added quote #%d", len(s.Quotes)))
}
}
func (q *Quotes) delQuote(m *dgofw.DiscordMessage, index int) {
if s := q.getServer(m.GuildID()); s != nil {
if index > 0 && index < len(s.Quotes) {
if len(s.Quotes) == 1 {
s.Quotes = s.Quotes[1:]
return
}
q.Lock()
s.Quotes = append(s.Quotes[:index], s.Quotes[index+1:]...)
q.Unlock()
q.Save()
}
}
}
func (q *Quotes) sendList(m *dgofw.DiscordMessage) {
if s := q.getServer(m.GuildID()); s != nil {
content := strings.Join(s.Quotes, "\n")
gc := github.NewClient(nil)
id := "quotes"
pub := true
files := make(map[github.GistFilename]github.GistFile)
files[github.GistFilename("quotes.txt")] = github.GistFile{
Content: &content,
}
gist := &github.Gist{
ID: &id,
Public: &pub,
Files: files,
}
g, _, err := gc.Gists.Create(nil, gist)
if err != nil {
m.Reply("Failed to create gist.\n" + err.Error())
return
}
m.Reply(*g.HTMLURL)
}
}
func (q *Quotes) OnMessage(m *dgofw.DiscordMessage) {
arg1 := m.Arg("arg1")
switch arg1 {
case "add":
q.addQuote(m)
case "del":
index := m.Arg("arg2")
if i, err := strconv.ParseInt(index, 10, 64); err == nil {
q.delQuote(m, int(i))
} else {
m.Reply("Invalid number")
}
case "list":
q.sendList(m)
case "":
if s := q.getServer(m.GuildID()); s != nil {
index := rand.Intn(len(s.Quotes))
m.Reply(fmt.Sprintf("%d. %s", index, s.Quotes[index]))
} else {
m.Reply("There are no quotes!")
}
default:
if i, err := strconv.ParseInt(m.Arg("arg2"), 10, 64); err == nil {
if s := q.getServer(m.GuildID()); s != nil {
if i > 0 && int(i) < len(s.Quotes) {
m.Reply(s.Quotes[i])
}
}
}
}
}
func (q *Quotes) Save() (err error) {
var f *os.File
defer f.Close()
if f, err = os.Create("./quotesstate.json"); err == nil {
return json.NewEncoder(f).Encode(q)
}
return
}
func (p *Quotes) Load() (err error) {
var b []byte
if b, err = ioutil.ReadFile("./quotesstate.json"); err == nil {
return json.Unmarshal(b, p)
}
return
}