-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
281 lines (236 loc) · 10.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
_ "github.com/go-sql-driver/mysql"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var Config struct {
Token string `json:"Token"`
Prefix string `json:"Prefix"`
Owner_id string `json:"Owner_id"`
database_host string `json:"database_host"`
database_port string `json:"database_port"`
database_connect string `json:"database_connect"`
database_socket string `json:"database_socket"`
database_user string `json:"database_user"`
database_password string `json:"database_password"`
database_table string `json:"database_table"`
}
type Channel struct {
id int
name string
owner_id string
category_id string
voicechannel_id string
textchannel_id string
settingmessage_id string
options string
}
var DB *sql.DB
func main() {
// Logo
//goland:noinspection GoPrintFunctions to make my IDE not shit itself
fmt.Println(" _______ _____ _ \n" +
" |__ __| / ____| | \n" +
" | | ___ _ __ ___ _ __ | | | |__ __ _ _ __ \n" +
" | |/ _ \\ '_ ` _ \\| '_ \\| | | '_ \\ / _` | '_ \\ \n" +
" | | __/ | | | | | |_) | |____| | | | (_| | | | | \n" +
" |_|\\___|_| |_| |_| .__/ \\_____|_| |_|\\__,_|_| |_| \n" +
" | | \n" +
" |_| \n")
//Read config file
fmt.Println("INIT: Config file")
configFile, err := os.Open("config.json")
if err != nil {
fmt.Println("Unable to read the config file!")
panic(err)
}
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&Config); err != nil {
fmt.Println("Unable to parse Json.")
panic(err)
}
fmt.Println("Config read successfully.")
//Init Database
//TODO: implement Unix socket connections
fmt.Println("INIT: Database in TCP mode")
//TODO:Need to use the json file because for some fucking reason it doesnt want to work and error out.
//db, err := sql.Open("mysql", Config.database_user + ":" + Config.database_password + "@tcp(" + Config.database_host + ":" + Config.database_port + ")/" + Config.database_table)
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/TempChan")
if err != nil {
panic(err)
} else {
fmt.Println("Databast init success!")
}
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
//Setting global variable...
DB = db
//New Discord session
fmt.Println("INIT: Discord Connection")
dg, err := discordgo.New("Bot " + Config.Token)
if err != nil {
fmt.Println("Error creating discord session. Discord down Lulz", err)
return
} else {
fmt.Println("Discord session created.")
}
//Register Handlers
fmt.Println("Registering handlers...")
dg.AddHandler(MessageCreate)
dg.AddHandler(MessageReactions)
fmt.Println("Setting Intents...")
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection. ", err)
return
}
fmt.Println("Done. TempChan is running. Logged in as: " + dg.State.User.Username + " Prefix set to: " + Config.Prefix)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
db.Close()
}
func MessageReactions(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
//Prevent bot reactions to trigger itself.
if r.UserID == s.State.User.ID {
return
}
//Check the message where the reaction is made, if someone reacted to a message not from the bot, just return it and dont do a SQL query.
OrigMessage, err := s.ChannelMessage(r.ChannelID, r.MessageID)
if OrigMessage.Author.ID != s.State.User.ID {
return
}
var channel = Channel{}
_ = DB.QueryRow("SELECT * from channels WHERE settingmessage_id = " + r.MessageID).Scan(&channel.id, &channel.name, &channel.owner_id, &channel.category_id, &channel.voicechannel_id, &channel.textchannel_id, &channel.settingmessage_id, &channel.options)
if r.MessageReaction.Emoji.Name == "❌" && r.MessageReaction.UserID == "218310787289186304" && r.MessageID == channel.settingmessage_id {
s.ChannelDelete(channel.voicechannel_id)
s.ChannelDelete(channel.textchannel_id)
s.ChannelDelete(channel.category_id)
stmt, _ := DB.Prepare("DELETE from channels where settingmessage_id = ?")
_, _ = stmt.Exec(r.MessageID)
}
if r.MessageReaction.Emoji.Name == "🔞" && r.MessageReaction.UserID != s.State.User.ID && r.MessageID == channel.settingmessage_id {
s.ChannelEditComplex(channel.textchannel_id, &discordgo.ChannelEdit{NSFW: true})
s.MessageReactionRemove(channel.textchannel_id, channel.settingmessage_id, "🔞", r.UserID);
s.MessageReactionRemove(channel.textchannel_id, channel.settingmessage_id, "🔞", s.State.User.ID)
}
if err != nil {
fmt.Println(err)
}
}
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
if strings.Contains(m.Content, "iOS") {
_, err := s.ChannelMessageSend(m.ChannelID, "> " + m.Content + "\n" + m.Author.Mention() + " Its `Ios` Not `iOS` :)")
if err != nil {
fmt.Println("Error: ", err)
}
}
// Show a simple help list.
if m.Content == Config.Prefix + "help" {
s.ChannelMessageSend(m.ChannelID, "To create Channels: ]cc <name>\n" +
"Setting a Limit ]climit <number of people>")
}
if m.Content == Config.Prefix + "channel delete all" && m.Author.ID == "218310787289186304" {
s.ChannelMessageSend(m.ChannelID, "Deleting all channels currently in database.")
channelrows, err := DB.Query("SELECT * from channels")
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Error! Was unable to get data from database.")
fmt.Println(err)
}
var channel = Channel{}
for channelrows.Next() {
_ = channelrows.Scan(&channel.id, &channel.name, &channel.owner_id, &channel.category_id, &channel.voicechannel_id, &channel.textchannel_id, &channel.settingmessage_id, &channel.options)
fmt.Println("Deleting: " + channel.name)
s.ChannelDelete(channel.voicechannel_id)
s.ChannelDelete(channel.textchannel_id)
s.ChannelDelete(channel.category_id)
stmt, _ := DB.Prepare("DELETE from channels where id = ?")
_, _ = stmt.Exec(channel.id)
}
}
if m.Content == Config.Prefix + "exit" && m.Author.ID == "218310787289186304" {
s.ChannelMessageSend(m.ChannelID, "Shutting down.")
fmt.Println("Got exit call from discord command.")
s.Close()
DB.Close()
os.Exit(0)
} else if m.Content == Config.Prefix + "exit" && m.Author.ID != "218310787289186304" {
s.ChannelMessageSend(m.ChannelID, "Watch this: <https://www.youtube.com/watch?v=dQw4w9WgXcQ>")
}
if m.Content == Config.Prefix + "cc" {
s.ChannelMessageSend(m.ChannelID, "Error missing channel name!")
}
//todo: make a command framework?
if strings.HasPrefix(m.Content, Config.Prefix + "cc ") {
tempchan, err := s.ChannelMessageSend(m.ChannelID, "Creating temporay channels for you...")
//Create the first category first.
channelCategory, err := s.GuildChannelCreate(m.GuildID, strings.Trim(m.Content, Config.Prefix + "cc "), discordgo.ChannelTypeGuildCategory)
channelText, err := s.GuildChannelCreateComplex(m.GuildID, discordgo.GuildChannelCreateData{
Name: strings.Trim(m.Content, Config.Prefix + "cc "),
Type: discordgo.ChannelTypeGuildText,
Topic: "Created by: " + m.Author.Username + ". This is a temporary channel.",
Position: 0,
ParentID: channelCategory.ID,
NSFW: false,
})
if err != nil {
s.ChannelMessageSend(m.ChannelID, "⚠ Sorry, I was unable to create the text channel. <@218310787289186304> Logged error to console.")
fmt.Println("Error: ", err)
}
// Create the text channel
channelVoice, err := s.GuildChannelCreateComplex(m.GuildID, discordgo.GuildChannelCreateData{
Name: strings.Trim(m.Content, Config.Prefix + "cc "),
Type: discordgo.ChannelTypeGuildVoice,
ParentID: channelCategory.ID,
})
if err != nil {
s.ChannelMessageSend(m.ChannelID, "⚠ Sorry, I was unable to create the voice channel. <@218310787289186304> Logged error to console.")
fmt.Println("Error: ", err)
}
channelSettings, err := s.ChannelMessageSend(channelText.ID, "```*** Channel Settings ***```\nThis is your newly created channel.\n" +
"The channel owner is: " + m.Author.Mention() + "\n" +
"The Channel owner and staff are able to change the settings via the emojis. You are also still required to obey the server rules!\n\n" +
"❌ = Delete the channel\n" +
"🔒 = Disable this Chat channel.\n" +
"🔞 = Set Chat as NSFW\n\n" +
"For more commands enter ]channel help\n\n\n" +
"⚠ Note: Staff of this server is also able to change every setting.")
//Saving TO DB
fmt.Println("Saving to DB...")
stmt, err := DB.Prepare("INSERT INTO `channels` (`name`, `owner_id`, `category_id`, `voicechannel_id`, `textchannel_id`, `settingmessage_id`, `options`) VALUES (?, ?, ?, ?, ?, ?, ?)")
_, err = stmt.Exec(strings.Trim(m.Content, Config.Prefix + "cc "), m.Author.ID, channelCategory.ID, channelVoice.ID, channelText.ID, channelSettings.ID, nil)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "⚠ Sorry, I was unable to create the channel. <@218310787289186304> Logged error to console.")
fmt.Println("Error: ", err)
} else {
s.ChannelMessageDelete(m.ChannelID, tempchan.ID)
s.ChannelMessageSend(m.ChannelID, "Channel created. please join the channel within 30 seconds, or it will be deleted")
fmt.Println("Created a new temporary channel to watch on... ")
}
fmt.Println("Adding reactions...")
err = s.ChannelMessagePin(channelText.ID, channelSettings.ID)
err = s.MessageReactionAdd(channelText.ID, channelSettings.ID, "❌")
err = s.MessageReactionAdd(channelText.ID, channelSettings.ID, "🔒")
err = s.MessageReactionAdd(channelText.ID, channelSettings.ID, "🔞")
if err != nil {
fmt.Println("Unable to add a reaction...")
}
fmt.Println("Channel creation Done.")
}
}