forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote.go
49 lines (45 loc) · 1.33 KB
/
quote.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
package quote
import (
"fmt"
"github.com/Seventy-Two/Cara"
"math/rand"
"time"
"os"
"bufio"
"strings"
"regexp"
"github.com/kardianos/osext"
)
func random(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
func quote(command *bot.Cmd, matches []string) (msg string, err error) {
dir, _ := osext.ExecutableFolder()
quotePath := fmt.Sprintf("%s\\logs\\%s.log", dir, strings.Replace(strings.Replace(command.Channel, "/", "–", -1), "#", "", 1))
reg, err := regexp.Compile("\\[.*\\]")
file, err := os.Open(quotePath)
if err != nil {
return "Error getting logs", nil
}
defer file.Close()
var choices []string
user := "<" + matches[1] + ">"
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.HasPrefix(strings.TrimSpace(reg.ReplaceAllString(scanner.Text(), "")), user) && !strings.HasPrefix(strings.TrimSpace(reg.ReplaceAllString(scanner.Text(), "")), user + " .") && (strings.Count(scanner.Text(), " ") > 2 || (strings.Count(scanner.Text(), " ") > 2)) {
choices = append(choices, strings.TrimSpace(reg.ReplaceAllString(scanner.Text(), "")))
}
}
if len(choices) > 0 {
chosen := random(0, len(choices))
msg = fmt.Sprintf("%s", choices[chosen])
return msg, nil
}
return "No quotes found", nil
}
func init() {
bot.RegisterCommand(
"^quote (\\S+)$",
quote)
}