forked from proxikal/Frostbyte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (135 loc) · 4.09 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
var bot Object
var start int64
var err error
func main() {
start = time.Now().UnixNano()
// Load the config.json file.
io, err := ioutil.ReadFile("config.json")
if err != nil {
fmt.Println(err)
}
// Load json into struct: Object
json.Unmarshal(io, &bot)
// Login to discord. You can use a token or email, password arguments.
dg, err := discordgo.New("Bot " + bot.Token)
if err != nil {
fmt.Println(err)
return
}
// Register new server
dg.AddHandler(bot.Initiate)
// Frostbyte Command handler.
dg.AddHandler(bot.CommandHandler)
// Greet Message & Autorole.
dg.AddHandler(bot.GuildMemberAdd)
// Bye Message.
dg.AddHandler(bot.GuildMemberRemove)
// Handle status messages and the interval.
go bot.StatusHandler(dg, "2m")
// Save the database every x minutes (Default is 5m)
go bot.Save()
dg.Open()
go bot.Intro(dg)
// Simple way to keep program running until any key press.
var input string
fmt.Scanln(&input)
return
}
// CommandHandler - Handle the commands and Auto Response System
// bot: Main Object with all your settings.
// s: The Current Session between the bot and discord
// m: The Message Object sent back from Discord.
func (bot *Object) CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if bot.System == nil {
return
}
var prefix = bot.System.Prefix
// Check user for Manage Server permission.
if IsManager(s, bot.Guild, m.Author.ID) == true {
bot.AddARS(s, m, prefix) // Listen for .auto command
bot.DeleteARS(s, m, prefix) // Listen for .delauto command
bot.AutoRoleCommand(s, m, prefix) // listen for .autorole command
bot.GreetCommand(s, m, prefix) // Listen for .greet command
bot.ChangeAvatar(s, m, prefix) // Listen for .avatar command
bot.InfoCommand(s, m, prefix) // Listen for .info command
bot.ViewARS(s, m, prefix) // Listen for .viewauto command
bot.InspectARS(s, m, prefix) // Listen for .inspect command
bot.AddStatusCommand(s, m, prefix) // Listen for .addstatus command
bot.DelStatusCommand(s, m, prefix) // Listen for .delstatus command
bot.ViewStatusCommand(s, m, prefix) // Listen for .viewstatus command
}
// Execute Auto Response System.
if m.Author.ID != s.State.User.ID && m.Author.Bot == false {
bot.Listen(s, m, prefix)
}
}
// Listen - Listens for A.R.S Commands.
// bot: Main Object with all your settings.
// s: The Current Session between the bot and discord
// m: The Message Object sent back from Discord.
func (bot *Object) Listen(s *discordgo.Session, m *discordgo.MessageCreate, prefix string) {
if strings.Contains(m.Content, prefix+"auto ") == false && strings.Contains(m.Content, prefix+"delauto ") == false {
var ars map[string]string
if _, err := os.Stat("autoresponse.json"); err != nil {
return
}
io, err := ioutil.ReadFile("autoresponse.json")
if err != nil {
fmt.Println(err)
return
}
json.Unmarshal(io, &ars)
for t, r := range ars {
if strings.Contains(t, "&") {
// Using the Contains system.
if strings.Contains(m.Content, t) {
content := bot.ParseServer(s, m, t, r)
bot.ParseDirection(s, m, t, content)
}
} else {
// Just a basic trigger.
if m.Content == t {
content := bot.ParseServer(s, m, t, r)
bot.ParseDirection(s, m, t, content)
}
}
}
}
}
// StatusHandler - (if set) iterates through your status messages
// bot: Main Object with all your settings.
// s: The Current Session between the bot and discord
// m: The Message Object sent back from Discord.
func (bot *Object) StatusHandler(s *discordgo.Session, duration string) {
if strings.Contains(duration, "m") == false {
duration = strings.Replace(duration, "s", "m", -1)
}
p, err := time.ParseDuration(duration)
if err != nil {
fmt.Println(err)
return
}
for {
if bot.System != nil {
if len(bot.System.Status) > 0 {
r := Random(0, len(bot.System.Status))
data := bot.System.Status[r]
err = s.UpdateStatus(0, data)
if err != nil {
fmt.Println(err)
}
}
}
<-time.After(p)
}
}