-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathSentence.go
65 lines (54 loc) · 1.69 KB
/
mathSentence.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
package main
import (
"database/sql"
"fmt"
"log"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
queryRandomMathSentence *sql.Stmt
insertRandomMathSentence *sql.Stmt
)
func initMathSentence(db *sql.DB) {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS math_sentence (id SERIAL PRIMARY KEY, sentence TEXT)")
if err != nil {
log.Panic(err)
}
queryRandomMathSentence = dbPrepare(db, "SELECT sentence FROM math_sentence ORDER BY random() LIMIT 1")
insertRandomMathSentence = dbPrepare(db, "INSERT INTO math_sentence (sentence) VALUES ($1)")
}
func msgStreamMathMessageHandler(session *discordgo.Session, msg *discordgo.MessageCreate) {
if len(msg.Mentions) > 0 {
for _, mention := range msg.Mentions {
if mention.ID == session.State.User.ID {
str := strings.ToLower(msg.Content)
if strings.Contains(str, "math") {
var sentence string
row := queryRandomMathSentence.QueryRow()
err := row.Scan(&sentence)
if err == sql.ErrNoRows {
sentence = "MATH IS THE WORST THING ON EARH"
}
recepient := msg.Author
if len(msg.Mentions) > 1 {
recepient = msg.Mentions[1]
}
resp := fmt.Sprintf("%s %s", recepient.Mention(), sentence)
session.ChannelMessageSend(msg.ChannelID, resp)
}
break
}
}
}
}
func addMathSentenceHandler(session *discordgo.Session, msg *discordgo.MessageCreate) {
sentence := strings.TrimPrefix(msg.Content, "!addmathsentence")
sentence = strings.TrimSpace(sentence)
if len(sentence) <= 1 {
session.ChannelMessageSend(msg.ChannelID, "Remember to include sentence in command...")
return
}
insertRandomMathSentence.Exec(sentence)
session.ChannelMessageSend(msg.ChannelID, "Added sentence to set! o7")
}