Skip to content

Commit 2f1c300

Browse files
authored
74 refactor split up file into dedicated files (#112)
* spilt jsonhandle.py in two files
1 parent 2d8dc4a commit 2f1c300

18 files changed

Lines changed: 849 additions & 801 deletions

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A clear and concise description of what you expected to happen.
2424
If applicable, add screenshots to help explain your problem.
2525

2626
**Bot Version:**
27-
Eg. `1.4.0`
27+
Eg. `2.3.1`
2828

2929

3030
**Additional context**

.github/ISSUE_TEMPLATE/set-up.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ Describe what are the issues you are having while setting up the bot.
1414
Some problems may result from other configs or parts of the set-up process. Tell us what have you done so far.
1515

1616
**What version of the bot**
17-
There may be differences in the set-up process depending on the bot version. State the version. Eg. `1.4.0`
17+
There may be differences in the set-up process depending on the bot version. State the version. Eg. `2.3.1`

cogs/commandgiverole.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
isNotInChannelOrDMCommand,
1313
)
1414
from helpfunctions.utils import Utils
15-
from datahandler.jsonhandle import Jsonhandle
15+
from datahandler.configHandle import ConfigHandle
16+
from datahandler.userHandle import UserHandle
1617

1718

1819
class Commandgiverole(commands.Cog):
@@ -55,8 +56,9 @@ class Commandgiverole(commands.Cog):
5556

5657
def __init__(self, bot):
5758
self.bot = bot
58-
self.jh = Jsonhandle()
59-
self.utils = Utils(bot, jh=self.jh)
59+
self.ch = ConfigHandle()
60+
self.uh = UserHandle()
61+
self.utils = Utils(bot, ch=self.ch, uh=self.uh)
6062
self.datapath = (
6163
str(os.path.dirname(os.path.dirname(__file__))) + "/data/giveroles/"
6264
)
@@ -203,7 +205,7 @@ async def rm(self, ctx, table_name):
203205
for role in remove
204206
]
205207
# Also sorts out users, which discord.Reactions.users() can return.
206-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
208+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
207209
remove_from_member = [
208210
member
209211
for reaction in message.reactions
@@ -358,7 +360,7 @@ async def get_message(self, ctx, channel_id, message_id):
358360
Otherwise returns a Discord message object.
359361
"""
360362
table_name = ctx.args[-1]
361-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
363+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
362364
channel = guild.get_channel(channel_id)
363365
if not channel:
364366
message_to_send = (
@@ -450,7 +452,7 @@ def check_content_table(self, content):
450452
for l in tupel:
451453
for role in l:
452454
roles.add(role)
453-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
455+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
454456
guild_roles = {role.name for role in guild.roles}.union(
455457
{role.id for role in guild.roles}, {str(role.id) for role in guild.roles}
456458
)

cogs/commandlistener.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from helpfunctions.utils import Utils
88
from helpfunctions.xpfunk import Xpfunk
9-
from datahandler.jsonhandle import Jsonhandle
9+
from datahandler.configHandle import ConfigHandle
10+
from datahandler.userHandle import UserHandle
1011
from datahandler.textban import Textban
1112

1213

@@ -17,8 +18,9 @@ class Commandlistener(commands.Cog):
1718

1819
def __init__(self, bot):
1920
self.bot = bot
20-
self.jh = Jsonhandle()
21-
self.utils = Utils(bot, jh=self.jh)
21+
self.ch = ConfigHandle()
22+
self.uh = UserHandle()
23+
self.utils = Utils(bot, ch=self.ch, uh=self.uh)
2224
self.xpf = Xpfunk()
2325

2426
@commands.Cog.listener()
@@ -48,13 +50,13 @@ async def on_ready(self):
4850
# Sends message to mods, when bot is online
4951
print("Now Online")
5052
await self.utils.sendModsMessage(
51-
"Bot is now online.\nVersion:\tDiscordBot DataBot v2.3.0"
53+
"Bot is now online.\nVersion:\tDiscordBot DataBot v2.3.1"
5254
)
5355
# Sets the bot's presence to "Online" or "Do not Disturb" to indicate if it's logging or not.
54-
if self.jh.getFromConfig("log") == "True":
55-
await self.bot.change_presence(status=discord.Status.online, activity=discord.Game(str(self.jh.getFromConfig("command_prefix")) + "help"))
56+
if self.ch.getFromConfig("log") == "True":
57+
await self.bot.change_presence(status=discord.Status.online, activity=discord.Game(str(self.ch.getFromConfig("command_prefix")) + "help"))
5658
else:
57-
await self.bot.change_presence(status=discord.Status.dnd, activity=discord.Game(str(self.jh.getFromConfig("command_prefix")) + "help"))
59+
await self.bot.change_presence(status=discord.Status.dnd, activity=discord.Game(str(self.ch.getFromConfig("command_prefix")) + "help"))
5860

5961
# When a member joins a guild
6062
@commands.Cog.listener()
@@ -64,14 +66,14 @@ async def on_member_join(self, member):
6466
6567
Creates a welcome message in the log channel
6668
"""
67-
channel = self.bot.get_channel(int(self.jh.getFromConfig("logchannel")))
68-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
69+
channel = self.bot.get_channel(int(self.ch.getFromConfig("logchannel")))
70+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
6971
await channel.send(f"Hey **{member.mention}**, welcome to {guild}")
7072

7173
"""
7274
@commands.Cog.listener()
7375
async def on_disconnect(self):
74-
owner = self.bot.get_user(int(self.jh.getFromConfig("owner")))
76+
owner = self.bot.get_user(int(self.ch.getFromConfig("owner")))
7577
await self.utils.sendOwnerMessage("Bot is offline.")
7678
"""
7779

@@ -83,16 +85,16 @@ async def on_member_remove(self, member):
8385
8486
Sends a goodbye message in the log channel
8587
"""
86-
channel = self.bot.get_channel(int(self.jh.getFromConfig("logchannel")))
87-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
88+
channel = self.bot.get_channel(int(self.ch.getFromConfig("logchannel")))
89+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
8890
await channel.send(
8991
f"**{member.name}** has left {guild}. Press F to pay respect."
9092
)
9193
"""
9294
#Hash user data
93-
voice = jh.getUserVoice(member.id)
94-
text = jh.getUserText(member.id)
95-
textCount = jh.getUserTextCount(member.id)
95+
voice = uh.getUserVoice(member.id)
96+
text = uh.getUserText(member.id)
97+
textCount = uh.getUserTextCount(member.id)
9698
[hash, code] = self.utils.hashData(voice, text, textCount, member.id)
9799
#Send user data
98100
embed = discord.Embed(title=f"{member.nick} ({member.name})", color=12008408)
@@ -105,7 +107,7 @@ async def on_member_remove(self, member):
105107
user = await bot.fetch_user(member.id)
106108
await user.send(content=f"**User related data from {server.name}**", embed=embed)
107109
await user.send(f"If you would like to join the Server again type this command to gain back your data **after** rejoining the server.\n```+reclaim {voice} {text} {textCount} {code} {hash}```\nhttps://discord.gg/3Fk4gnQ2Jz")
108-
jh.removeUserFromData(member.id)
110+
uh.removeUserFromData(member.id)
109111
"""
110112

111113
# When a reacting is added
@@ -205,14 +207,14 @@ async def on_raw_reaction_add(self, payload):
205207
else:
206208
# Give reaction XP
207209
channel = self.bot.get_channel(payload.channel_id)
208-
if self.jh.isInWhitelist(payload.channel_id):
210+
if self.ch.isInWhitelist(payload.channel_id):
209211
message = await channel.fetch_message(payload.message_id)
210212
if (
211213
not (message.author.bot or payload.member.bot)
212-
and self.jh.getFromConfig("log") == "True"
214+
and self.ch.getFromConfig("log") == "True"
213215
):
214-
self.jh.addReactionXP(payload.user_id, self.xpf.randomRange(1, 5))
215-
self.jh.saveData()
216+
self.uh.addReactionXP(payload.user_id, self.xpf.randomRange(1, 5))
217+
self.uh.saveData()
216218

217219
# When a user changes his voice state
218220
@commands.Cog.listener()
@@ -265,14 +267,14 @@ async def on_voice_state_update(self, member, before, after):
265267
)
266268

267269
# Removes channel from blacklist if necessary
268-
self.jh.removeFromBalcklist(lastChannel.id)
270+
self.ch.removeFromBalcklist(lastChannel.id)
269271

270272
await lastChannel.delete()
271273

272274
# User left channel, which is not the first channel. So it will be deleted
273275
else:
274276
# Removes channel from blacklist if necessary
275-
self.jh.removeFromBalcklist(before.channel.id)
277+
self.ch.removeFromBalcklist(before.channel.id)
276278
await before.channel.delete()
277279

278280
"""
@@ -332,8 +334,8 @@ async def on_voice_state_update(self, member, before, after):
332334
# Creates channel and gets it
333335
newChannel = await channelWithNumberBefore.clone(name=newChannelName)
334336

335-
if self.jh.isInBlacklist(after.channel.id):
336-
self.jh.writeToBalcklist(newChannel.id)
337+
if self.ch.isInBlacklist(after.channel.id):
338+
self.ch.writeToBalcklist(newChannel.id)
337339

338340
# Move channel after channelWithNumberBefore
339341
await newChannel.move(after=channelWithNumberBefore)
@@ -359,37 +361,37 @@ async def on_message(self, message):
359361
a = "" + message.content
360362

361363
# Stops user from writting in levelchannel none command messages
362-
if str(message.channel.id) == str(self.jh.getFromConfig("levelchannel")) and a[
364+
if str(message.channel.id) == str(self.ch.getFromConfig("levelchannel")) and a[
363365
0
364-
] != self.jh.getFromConfig("command_prefix"):
366+
] != self.ch.getFromConfig("command_prefix"):
365367
await message.delete()
366368
return
367369

368370
# Checks if message contains a picture
369-
if len(message.attachments) > 0 and self.jh.getFromConfig("log") == "True":
371+
if len(message.attachments) > 0 and self.ch.getFromConfig("log") == "True":
370372
attachments = message.attachments
371373
userID = message.author.id
372374
for attachment in attachments:
373375
name = attachment.filename
374376
if name.endswith("jpg") or name.endswith("png"):
375377
# Gives XP when picture is in message
376-
self.jh.addTextXP(userID, self.xpf.randomRange(20, 40))
377-
self.jh.saveData()
378+
self.uh.addTextXP(userID, self.xpf.randomRange(20, 40))
379+
self.uh.saveData()
378380
return
379381

380382
# When Message is a String
381383
if (
382384
a != ""
383-
and a[0] != self.jh.getFromConfig("command_prefix")
384-
and self.jh.getFromConfig("log") == "True"
385+
and a[0] != self.ch.getFromConfig("command_prefix")
386+
and self.ch.getFromConfig("log") == "True"
385387
):
386388
# Give XP when message is not a command
387-
if self.jh.isInWhitelist(message.channel.id):
388-
self.jh.addTextXP(message.author.id, self.xpf.textXP(a))
389-
self.jh.saveData()
389+
if self.ch.isInWhitelist(message.channel.id):
390+
self.uh.addTextXP(message.author.id, self.xpf.textXP(a))
391+
self.uh.saveData()
390392

391393
# Sends BotOwner commands, which are triggering the bot
392-
if len(a) > 0 and a[0] == self.jh.getFromConfig("command_prefix"):
394+
if len(a) > 0 and a[0] == self.ch.getFromConfig("command_prefix"):
393395
channelName = "DM"
394396
try:
395397
channelName = message.channel.name

cogs/commandmod.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from helpfunctions.decorators import isBotModCommand
55
from helpfunctions.xpfunk import Xpfunk
66
from helpfunctions.utils import Utils
7-
from datahandler.jsonhandle import Jsonhandle
7+
from datahandler.configHandle import ConfigHandle
8+
from datahandler.userHandle import UserHandle
89

910

1011
class Commandmod(commands.Cog, name="Bot Mod Commands"):
@@ -28,8 +29,9 @@ class Commandmod(commands.Cog, name="Bot Mod Commands"):
2829
def __init__(self, bot):
2930
super(Commandmod, self).__init__()
3031
self.bot = bot
31-
self.jh = Jsonhandle()
32-
self.utils = Utils(bot, jh=self.jh)
32+
self.ch = ConfigHandle()
33+
self.uh = UserHandle()
34+
self.utils = Utils(bot, ch=self.ch, uh=self.uh)
3335
self.xpf = Xpfunk()
3436

3537
"""
@@ -100,7 +102,7 @@ async def addtextwhitelist(self, ctx, channelID=None):
100102
101103
Adds channel to whitelist, so users can get XP in the channel.
102104
"""
103-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
105+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
104106
# This does not include threads
105107
channels = self.bot.guilds[0].text_channels
106108
# When channelID is not given, use ctx.channel.id.
@@ -115,7 +117,7 @@ async def addtextwhitelist(self, ctx, channelID=None):
115117
message = f"Channel is not in the server {guild.name}"
116118
await self.utils.log(f"{message} from user {ctx.author}", 2)
117119

118-
elif self.jh.writeToWhitelist(channelID):
120+
elif self.ch.writeToWhitelist(channelID):
119121
# Try to write in whitelist
120122
channelName = channel_to_add.name
121123
message = (
@@ -151,7 +153,7 @@ async def removetextwhitelist(self, ctx, channelID=None):
151153
if not channelID:
152154
channelID = ctx.channel.id
153155
# Try to remove from whitelist
154-
if self.jh.removeFromWhitelist(channelID):
156+
if self.ch.removeFromWhitelist(channelID):
155157
channelName = str(self.bot.get_channel(int(channelID)))
156158
message = (
157159
"Removed "
@@ -229,12 +231,12 @@ async def addblacklist(self, ctx, channelID):
229231
230232
Adds channel to whitelist, so users can not get XP in the channel.
231233
"""
232-
guild = self.bot.get_guild(int(self.jh.getFromConfig("guild")))
234+
guild = self.bot.get_guild(int(self.ch.getFromConfig("guild")))
233235
channels = self.bot.guilds[0].voice_channels
234236
# Test if channel is in Server
235237
if str(channelID) in [str(channel.id) for channel in channels]:
236238
# Try to write in Blacklist
237-
if self.jh.writeToBalcklist(channelID):
239+
if self.ch.writeToBalcklist(channelID):
238240
channelName = str(self.bot.get_channel(int(channelID)))
239241
message = (
240242
"Added "
@@ -269,7 +271,7 @@ async def removeblacklist(self, ctx, channelID=None):
269271
# When channelID is not given, use ctx.channel.id.
270272
if not channelID:
271273
channelID = ctx.channel.id
272-
if self.jh.removeFromBalcklist(channelID):
274+
if self.ch.removeFromBalcklist(channelID):
273275
channelName = str(self.bot.get_channel(int(channelID)))
274276
message = (
275277
"Removed "
@@ -313,20 +315,20 @@ async def printData(self, ctx):
313315
TextXP: int,
314316
Messages: int.
315317
"""
316-
guild = str(self.bot.get_guild(int(self.jh.getFromConfig("guild"))))
318+
guild = str(self.bot.get_guild(int(self.ch.getFromConfig("guild"))))
317319
message = f"Printing data of server {guild}:\n"
318320
# Sorts user by their usernames
319321
sortedData = sorted(
320-
self.jh.data,
322+
self.uh.data,
321323
key=lambda id: "a" + str(self.bot.get_user(int(id)).name).lower()
322324
if self.bot.get_user(int(id)) != None
323325
else "no user",
324326
)
325327
for userID in sortedData:
326-
level = self.jh.getUserLevel(userID)
327-
voiceXP = self.jh.getUserVoice(userID)
328-
textXP = self.jh.getUserText(userID)
329-
textCount = self.jh.getUserTextCount(userID)
328+
level = self.uh.getUserLevel(userID)
329+
voiceXP = self.uh.getUserVoice(userID)
330+
textXP = self.uh.getUserText(userID)
331+
textCount = self.uh.getUserTextCount(userID)
330332
user = self.bot.get_user(int(userID))
331333
username = "No User"
332334
# Handel not existing User IDs

cogs/commandmodserver.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from helpfunctions.utils import Utils
44
from datahandler.textban import Textban
5-
from datahandler.jsonhandle import Jsonhandle
5+
from datahandler.configHandle import ConfigHandle
6+
from datahandler.userHandle import UserHandle
67

78

89
def hasAnyRole(*items):
@@ -37,10 +38,10 @@ class Commandmodserver(commands.Cog, name="Server Mod Commands"):
3738
def __init__(self, bot):
3839
super(Commandmodserver, self).__init__()
3940
self.bot = bot
40-
self.jh = Jsonhandle()
41+
self.ch = ConfigHandle()
4142
self.tban = Textban()
42-
self.jh = Jsonhandle
43-
self.utils = Utils(bot, jh=self.jh)
43+
self.uh = UserHandle()
44+
self.utils = Utils(bot, ch=self.ch, uh=self.uh)
4445
Commandmodserver.utils = self.utils
4546

4647

0 commit comments

Comments
 (0)