This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
144 lines (121 loc) · 2.7 KB
/
run.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
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/legolord208/stdutil"
"github.com/legolord208/timeouts"
)
var ownID string
var timeout timeouts.Timeout
const delayPassive = 3
const delayAgressive = 5
func doRun(args []string) {
if len(args) < 1 {
stdutil.PrintErr("No token supplied in arguments", nil)
return
}
token := args[0]
loadRules()
timeout = timeouts.NewTimeout()
fmt.Println("Starting...")
if !strings.HasPrefix(token, "Bot ") {
stdutil.PrintErr("Selfbots are against the T.O.S. and will get you banned.", nil)
stdutil.PrintErr("If this is a bot token, prefix it with `Bot `.", nil)
return
}
session, err := discordgo.New(token)
if err != nil {
stdutil.PrintErr("Couldn't initialize bot", err)
return
}
me, err := session.User("@me")
if err != nil {
stdutil.PrintErr("Couldn't fetch @me", err)
return
}
ownID = me.ID
session.AddHandler(messageCreate)
err = session.Open()
if err != nil {
stdutil.PrintErr("Couldn't start bot", err)
return
}
fmt.Println("Started!")
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
fmt.Println("\nClosing!")
session.Close()
}
func messageCreate(session *discordgo.Session, e *discordgo.MessageCreate) {
message(session, e.Message)
}
func messageUpdate(session *discordgo.Session, e *discordgo.MessageUpdate) {
message(session, e.Message)
}
func message(session *discordgo.Session, e *discordgo.Message) {
if e.Author == nil {
return
}
if e.Author.ID == ownID {
return
}
if timeout.InTimeout(e.Author.ID) {
timeout.SetTimeout(e.Author.ID, time.Duration(delayAgressive)*time.Second)
return
}
timeout.SetTimeout(e.Author.ID, time.Duration(delayPassive)*time.Second)
content := strings.ToLower(strings.TrimSpace(e.Content))
rules:
for _, rule := range rules {
if rule.Exact && content != rule.Msg {
continue
} else if !rule.Exact && !strings.Contains(content, rule.Msg) {
continue
}
// Channel checking
for _, c := range rule.NotInChannel {
if c == e.ChannelID {
continue rules
}
}
if len(rule.InChannel) > 0 {
found := false
for _, c := range rule.InChannel {
if c == e.ChannelID {
found = true
}
}
if !found {
continue
}
}
// User checking
for _, from := range rule.NotFrom {
if from == e.Author.ID {
continue rules
}
}
if len(rule.From) > 0 {
found := false
for _, from := range rule.From {
if from == e.Author.ID {
found = true
}
}
if !found {
continue
}
}
_, err := session.ChannelMessageSend(e.ChannelID, rule.Reply)
if err != nil {
stdutil.PrintErr("Could not send message", err)
}
break
}
}