-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (156 loc) · 6.45 KB
/
main.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
164
165
166
167
168
169
170
171
172
/*
* go-robot: Discord Bot tracks how many Subreddits mentioned, rewritten in go
* Copyright (C) 2021 Sir Fredrick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"regexp"
"strconv"
"syscall"
"time"
"strings"
"github.com/bwmarrin/discordgo"
)
type numberSlice []int
type session interface {
ChannelMessageSend(channelID string, content string) (*discordgo.Message, error)
AddHandler(handler interface{}) func()
Open() error
Close() error
}
var messages []string = []string{"You've brought up Reddit %v times. Don't you think that's a bit much?", "Users have been an intellectual %v times", "%v SubReddits mentioned.", "This server's love of Reddit is at least %v", "r/%vsubreddits", "Talk all you want, because SubReddit #%v will be your last!", "%v SubReddits. You went too far, you fool.", "%v scenarios saved.", "The princess is now a permanent guest at one of my %v Koopa hotels.", "Lamp oil? Rope? Bombs? You want it, it's yours, my friend, as long as you have %v rubies.", "I found %v pecha berries. Me too! I found a healing potion, but it won't be enough!", "Don't let a single one get away! We'll each need to take down about %v. Stow your fear. It's now or never!", "Jacob now has %v less knees.", "Back in my heyday, 5 hits would have been enough to knock that guy out. But today it took more than %v mighty blows.", "IQ's %v, but you can leave it all to me!", "C%v18", "%v reasons why I'm here", "Heh heh heh heh ha ha ha. You fool. I have %v alternative accounts!", "Ha ha, ha. %v!", "Fallout %v Battle Royale", "Error %v, could not generate new error message", "OwO, you have UwU'd exactwy %v times todaw my fwendo", "1 + 1 = %v", "Space, the final frontier. These are the voyages of the startship Enterprise. Its continuing mission: to explore strange new worlds, to seek out new life and new civilizations, to boldy go where %v people have gone before.", "It has been %v femtoseconds since the last bot related accident.", "Hey guys, have you prepared for Area %v Raid yet?", "The %v Precepts of Zote", "I am %v parallel dimensions ahead of you!", "There's %v days of summer vacation...", "Sir, the possibility of successfully navigating an asteroid field is approximately %v to 1.", "You have been given %v patridges in a pear tree.", "Wait, are you just doing this to see if there are any more messages? Because that is one of the worst reasons to call this bot %v times.", "You have procrastinated on going to your grandfathers funeral by clicking %v objects.", "Welp, that's %v more titles that Ethan has.", "Error. %v is less than infinity.", "Still not convinced? Okay, okay, I'll cut you a deal... The game's available for %v dollars, and that's a great price.", "There are %v lights!", "Somehow, you have rolled a %v ... what was your modifier again?", "Self destruct sequence initiated. You have %v seconds until detonation."}
var numbers numberSlice = make([]int, 0)
var count int = 0
var index int = 0
func main() {
bs, err := ioutil.ReadFile(".secret")
if err != nil {
fmt.Println("Error: ", err)
}
dg, err := discordgo.New("Bot " + strings.Trim(string(bs), "\n"))
run(dg, err, false)
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
sendMessage(s, m, s.State.User.ID)
}
func sendMessage(s session, m *discordgo.MessageCreate, botID string) {
if m.Author.ID == botID {
return
}
regexp1 := regexp.MustCompile(`(^[^a-zA-Z0-9_]*)(r\/[a-zA-Z0-9_]+)|([^a-zA-Z0-9_]+)(r\/[a-zA-Z0-9_]+)\w+`)
regexp2 := regexp.MustCompile(`(^[^a-zA-Z0-9_]*)(r\/[a-zA-Z0-9_]+)|([^a-zA-Z0-9_]+)(r\/[a-zA-Z0-9_]+)\w+`)
if regexp1.Find([]byte(m.Content)) != nil || regexp2.Find([]byte(m.Content)) != nil {
count++
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(messages[numbers[index]], count))
if numbers[index] == 38 {
for i := count - 1; i > count-6; i-- {
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(i))
}
s.ChannelMessageSend(m.ChannelID, "...")
s.ChannelMessageSend(m.ChannelID, "Taylor defused the bomb!")
}
index++
if index >= len(numbers) {
index = 0
numbers.shuffle()
}
}
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
t := 0
/*
* Update Name with a link to your source code if modified. See LICENSE
* file for details.
*/
a := discordgo.Activity{
Name: "for Subreddits https://github.com/sirfredrick/go-r-obot",
Type: discordgo.ActivityTypeListening,
URL: "",
}
as := make([]*discordgo.Activity, 1)
as[0] = &a
afk := false
stat := "online"
usd := discordgo.UpdateStatusData{
IdleSince: &t,
Activities: as,
AFK: afk,
Status: stat,
}
s.UpdateStatusComplex(usd)
}
func run(dg session, err error, debug bool) {
if err != nil {
fmt.Println("Error creating Discord session: ", err)
return
}
count = readCount("data.txt")
numbers.setup()
numbers.shuffle()
dg.AddHandler(ready)
dg.AddHandler(messageCreate)
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection: ", err)
return
}
fmt.Println("Bot is now running, Press Ctrl-C to exit.")
if !debug {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
writeCount("data.txt", count)
}
dg.Close()
}
func (ns *numberSlice) setup() {
for i := range messages {
*ns = append(*ns, i)
}
}
func (ns numberSlice) shuffle() {
src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src)
for i := range ns {
j := r.Intn(len(ns) - 1)
ns[i], ns[j] = ns[j], ns[i]
}
}
func readCount(p string) int {
bs, err := ioutil.ReadFile(p)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
writeCount(p, 0)
return 0
}
fmt.Println("Error: ", err)
return 0
}
c, err := strconv.Atoi(string(bs))
if err != nil {
fmt.Println("Error: ", err)
}
return c
}
func writeCount(p string, c int) error {
return ioutil.WriteFile(p, []byte(fmt.Sprint(c)), 0666)
}