-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.py
212 lines (196 loc) · 10 KB
/
Rules.py
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
# ------------------------------------------------------------------------------
# MidnightStarshineBot - a multipurpose Discord bot
# Copyright (C) 2020 T. Duke Perry
#
# 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/>.
# ------------------------------------------------------------------------------
import discord
from Constants import *
from Utilities import *
RULE_GET_COMMAND = "getrule"
RULE_SET_COMMAND = "setrule"
RULE_EDIT_COMMAND = "editrule"
RULE_DELETE_COMMAND = "deleterule"
RULE_GET_ALL_COMMAND = "getallrules"
RULE_GET_BACKUP_COMMAND = "getrulebackup"
RULE_CHANNEL_SET_COMMAND = "setrulechannel"
RULE_CHANNEL_CLEAR_COMMAND = "clearrulechannel"
async def setRule(message, commandArgs):
if message.author.permissions_in(message.channel).manage_guild:
server_id = message.guild.id
conn = await getConnection()
try:
await conn.execute('INSERT INTO tbl_rules (server, content) VALUES ($1, $2)', server_id, commandArgs)
ruleData = await conn.fetchrow('SELECT COUNT(id) FROM tbl_rules WHERE server = $1', server_id)
await message.channel.send("Rule #" + str(ruleData[0]) + " has been set to: " + commandArgs)
finally:
await returnConnection(conn)
await updateRuleChannel(message)
async def getRule(message, commandArgs):
if commandArgs.isdigit():
conn = await getConnection()
try:
ruleData = await conn.fetch('SELECT content FROM tbl_rules WHERE server = $1 ORDER BY id', message.guild.id)
finally:
await returnConnection(conn)
rule_num = int(commandArgs)
if rule_num <= len(ruleData) and rule_num > 0:
await message.channel.send("Rule #" + commandArgs + ": " + ruleData[rule_num - 1][0])
else:
await message.channel.send("There is no rule #" + commandArgs)
else:
await message.channel.send("Please provide a valid number.")
async def getRuleId(server_id, rule_num):
conn = await getConnection()
try:
ruleData = await conn.fetch('SELECT id FROM tbl_rules WHERE server = $1 ORDER BY id', server_id)
finally:
await returnConnection(conn)
if rule_num <= len(ruleData) and rule_num > 0:
returnValue = ruleData[rule_num - 1][0]
else:
returnValue = None
return returnValue
async def editRule(message, commandArgs):
if message.author.permissions_in(message.channel).manage_guild:
parsing = commandArgs.partition(" ")
if parsing[0].isdigit():
rule_id = await getRuleId(message.guild.id, int(parsing[0]))
if rule_id is not None:
conn = await getConnection()
try:
await conn.execute('UPDATE tbl_rules SET content = $1 WHERE id = $2', parsing[2], rule_id)
await message.channel.send("Rule #" + parsing[0] + " is now: " + parsing[2])
finally:
await returnConnection(conn)
await updateRuleChannel(message)
else:
await message.channel.send("There is no rule #" + parsing[0])
else:
await message.channel.send("Please provide a valid number.")
async def deleteRule(message, commandArgs):
if message.author.permissions_in(message.channel).manage_guild:
if commandArgs.isdigit():
rule_id = await getRuleId(message.guild.id, int(commandArgs))
if rule_id is not None:
conn = await getConnection()
try:
await conn.execute('DELETE FROM tbl_rules WHERE id = $1', rule_id)
await message.channel.send("Rule #" + commandArgs + " has been deleted")
finally:
await returnConnection(conn)
await updateRuleChannel(message)
else:
await message.channel.send("There is no rule #" + commandArgs)
else:
await message.channel.send("Please provide a valid number.")
async def getAllRules(message):
conn = await getConnection()
try:
ruleData = await conn.fetch('SELECT content FROM tbl_rules WHERE server = $1 ORDER BY id', message.guild.id)
finally:
await returnConnection(conn)
count = len(ruleData)
if count > 0:
output = "**SERVER RULES** for " + message.guild.name + ":"
for i in range(count):
# TODO: add some kind of length control
output += "\nRule #" + str(i + 1) + ": " + ruleData[i][0]
await message.author.create_dm()
await message.author.dm_channel.send(output)
await message.channel.send(message.author.mention + "! A copy of the complete server rules have been sent to your DMs.")
async def getRuleBackup(message):
if message.author.permissions_in(message.channel).manage_guild:
conn = await getConnection()
try:
ruleData = await conn.fetch('SELECT content FROM tbl_rules WHERE server = $1 ORDER BY id', message.guild.id)
finally:
await returnConnection(conn)
count = len(ruleData)
if count > 0:
output = "**BACKUP OF SERVER RULES** for " + message.guild.name + ": \n```"
for i in range(count):
# TODO: add some kind of length control
output += "\n" + getPrefix(message.guild) + RULE_SET_COMMAND + " " + ruleData[i][0]
output += "```"
await message.author.create_dm()
await message.author.dm_channel.send(output)
await message.channel.send(message.author.mention + "! A backup of the complete server rules has been sent to your DMs.")
async def setRuleChannel(message, commandArgs):
if message.author.permissions_in(message.channel).manage_guild:
if commandArgs == "":
await message.channel.send("Please specify a rule channel.")
elif len(message.channel_mentions) == 0:
await message.channel.send("Please specify a valid rule channel.")
else:
ruleChannel = message.channel_mentions[0]
server_id = message.guild.id
conn = await getConnection()
try:
oldData = await conn.fetchrow('SELECT channel FROM tbl_rule_posting WHERE server = $1', server_id)
if oldData is not None and oldData[0] == ruleChannel.id:
await message.channel.send("The rules are already posted in that channel.")
else:
rulesData = await conn.fetch('SELECT content FROM tbl_rules WHERE server = $1 ORDER BY id', server_id)
count = len(rulesData)
ruleOutput = "**RULES:**"
for i in range(count):
# TODO: add some kind of length control
ruleOutput += "\n\n" + str(i + 1) + ": " + rulesData[i][0]
ruleMessage = await ruleChannel.send(ruleOutput)
if oldData is None:
await conn.execute('INSERT INTO tbl_rule_posting (server, channel, message) VALUES ($1, $2, $3)', server_id, ruleChannel.id, ruleMessage.id)
else:
await conn.execute('UPDATE tbl_rule_posting SET channel = $1, message = $2 WHERE server = $3', ruleChannel.id, ruleMessage.id, server_id)
await message.channel.send("Rules now posted in " + ruleChannel.mention + ".")
finally:
await returnConnection(conn)
async def clearRuleChannel(message):
if message.author.permissions_in(message.channel).manage_guild:
server_id = message.guild.id
conn = await getConnection()
try:
channelData = await conn.fetchrow('SELECT channel, message FROM tbl_rule_posting WHERE server = $1', server_id)
if channelData is None:
await message.channel.send("No rule postings recorded.")
else:
ruleChannel = message.guild.get_channel(channelData[0])
try:
ruleMessage = await ruleChannel.fetch_message(channelData[1])
await ruleMessage.delete()
except discord.errors.NotFound:
pass
await conn.execute('DELETE FROM tbl_rule_posting WHERE server = $1', server_id)
await message.channel.send("Rule posting in " + ruleChannel.mention + " has been deleted.")
finally:
await returnConnection(conn)
async def updateRuleChannel(message):
server_id = message.guild.id
conn = await getConnection()
try:
messageData = await conn.fetchrow('SELECT channel, message FROM tbl_rule_posting WHERE server = $1', server_id)
if messageData is not None:
try:
ruleMessage = await message.guild.get_channel(messageData[0]).fetch_message(messageData[1])
rulesData = await conn.fetch('SELECT content FROM tbl_rules WHERE server = $1 ORDER BY id', server_id)
count = len(rulesData)
ruleOutput = "**RULES:**"
for i in range(count):
# TODO: add some kind of length control
ruleOutput += "\n\n" + str(i + 1) + ": " + rulesData[i][0]
await ruleMessage.edit(content=ruleOutput)
except discord.errors.NotFound:
await message.channel.send("Error encountered updating rule posting. Post has likely been deleted.")
finally:
await returnConnection(conn)