Skip to content

Commit ad0b7ad

Browse files
committed
Organized database tables to prevent table spam and added back username in osu command
Signed-off-by: ColaIan <colaian.yt@gmail.com>
1 parent 7a7d545 commit ad0b7ad

File tree

4 files changed

+71
-57
lines changed

4 files changed

+71
-57
lines changed

Teapot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
|_|\\___|\\__,_| .__/ \\___/ \\__|
1717
by ColaIan |_| & RedTea
1818
19-
Running Teapot.pt {teapot.version()}
19+
Running Teapot.py {teapot.version()}
2020
""")
2121

2222
load_dotenv(join(dirname(__file__), '.env'))
@@ -36,11 +36,11 @@
3636
db.execute('CREATE TABLE IF NOT EXISTS `guilds` (`guild_id` BIGINT, `guild_name` TINYTEXT)')
3737
db.execute('CREATE TABLE IF NOT EXISTS `channels` (`channel_id` BIGINT, `channel_name` TINYTEXT)')
3838
db.execute("CREATE TABLE IF NOT EXISTS `users` (`user_id` BIGINT, `user_name` TINYTEXT, `user_discriminator` INT)")
39-
db.execute("CREATE TABLE IF NOT EXISTS `logs` (`timestamp` TEXT, `type` TINYTEXT, `class` TINYTEXT, `message` "
39+
db.execute("CREATE TABLE IF NOT EXISTS `bot_logs` (`timestamp` TEXT, `type` TINYTEXT, `class` TINYTEXT, `message` "
4040
"MEDIUMTEXT)")
4141
print(f"Connected to database ({teapot.config.db_host()}) in {round(time.perf_counter() - time_start, 2)}s")
4242

43-
db.execute("INSERT INTO `logs`(timestamp, type, class, message) VALUES(%s, %s, %s, %s)",
43+
db.execute("INSERT INTO `bot_logs`(timestamp, type, class, message) VALUES(%s, %s, %s, %s)",
4444
(teapot.time(), "BOT_START", __name__, "Initialized bot"))
4545
database.commit()
4646

@@ -61,8 +61,8 @@ async def on_ready():
6161
if teapot.config.storage_type() == "mysql":
6262
for guild in bot.guilds:
6363
teapot.managers.database.create_guild_table(guild)
64-
elif teapot.config.storage_type() == "flatfile":
65-
print("[!] Flatfile storage has not been implemented yet. MySQL database is recommended")
64+
elif teapot.config.storage_type() == "sqlite":
65+
print("[!] SQLite storage has not been implemented yet. MySQL database is recommended")
6666
print(f"Registered commands and events in {round(time.perf_counter() - time_start, 2)}s")
6767
await bot.change_presence(status=discord.Status.online, activity=discord.Game(teapot.config.bot_status()))
6868

@@ -74,5 +74,5 @@ async def on_ready():
7474
except Exception as e:
7575
print(f"[/!\\] Failed to connect to DiscordAPI. Please check your bot token!\n{e}")
7676
if teapot.config.storage_type() == "mysql":
77-
db.execute("INSERT INTO `logs`(timestamp, type, class, message) VALUES(%s, %s, %s, %s)",
77+
db.execute("INSERT INTO `bot_logs`(timestamp, type, class, message) VALUES(%s, %s, %s, %s)",
7878
(teapot.time(), "ERROR", __name__, e))

teapot/cogs/osu.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class OsuPlayer:
1111
def __init__(self, player):
1212
self.id = player["user_id"]
1313
self.username = player["username"]
14+
self.join_date = (player["join_date"].split(" "))[0]
1415
self.c300 = player["count300"]
1516
self.c100 = player["count100"]
1617
self.c50 = player["count50"]
@@ -28,20 +29,19 @@ def __init__(self, player):
2829
self.pp_country_rank = player["pp_country_rank"]
2930

3031
def display(self, author):
31-
title = self.username
32-
desc = self.country.upper()
33-
url = 'https://osu.ppy.sh/u/' + self.username
3432
em = dmbd.newembed()
33+
em.set_author(name=f"{self.country.upper()} | {self.username}", url=f"https://osu.ppy.sh/u/{self.username}")
3534
em.add_field(name='Performance', value=self.pp_raw + 'pp')
3635
em.add_field(name='Accuracy', value="{0:.2f}%".format(float(self.accuracy)))
3736
lvl = int(float(self.level))
3837
percent = int((float(self.level) - lvl) * 100)
39-
em.add_field(name='Level', value="{0} ({1}%)".format(lvl, percent))
38+
em.add_field(name='Level', value=f"{lvl} ({percent}%)")
4039
em.add_field(name='Rank', value=self.pp)
4140
em.add_field(name='Country Rank', value=self.pp_country_rank)
4241
em.add_field(name='Playcount', value=self.playcount)
4342
em.add_field(name='Total Score', value=self.total)
4443
em.add_field(name='Ranked Score', value=self.ranked)
44+
em.add_field(name='Registered At', value=self.join_date)
4545
return em
4646

4747

@@ -53,12 +53,7 @@ def __init__(self, bot):
5353
async def osu(self, ctx, *, args: str):
5454
"""
5555
Look up an osu player:
56-
Usage: osu [UserID/Username] [Optional:Mode(default Osu!)]
57-
Modes:
58-
0 - Default Osu!
59-
1 - Taiko
60-
2 - ChitogeBot
61-
3 - Osu!mania
56+
Usage: osu [UserID/Username]
6257
"""
6358

6459
args_array = args.split(' ')
@@ -69,20 +64,16 @@ async def osu(self, ctx, *, args: str):
6964
peppy = args_array[0]
7065
type = "0"
7166
else:
72-
await ctx.send('Wrong Syntax!')
67+
await ctx.send('Invalid Syntax!')
7368
return
7469
cookiezi = '05b43eb66b2977d4f2c9148b00e3853688d515cf'
75-
link = ('http://osu.ppy.sh/api/get_user?k=' + cookiezi + '&u=' + peppy
76-
+ '&m=' + type)
77-
r = requests.get(link)
70+
r = requests.get('http://osu.ppy.sh/api/get_user?k=' + cookiezi + '&u=' + peppy + '&m=' + type)
7871

7972
if r.status_code != 200:
8073
print('Unable to fetch osu statistics!')
8174
return
82-
brainpower = json.loads(r.text)
8375

84-
hvick = OsuPlayer(brainpower[0])
85-
await ctx.send(embed=hvick.display(ctx.message.author))
76+
await ctx.send(embed=OsuPlayer(json.loads(r.text)[0]).display(ctx.message.author))
8677

8778

8879
def setup(bot):

teapot/events.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ async def on_member_join(member):
2121
try:
2222
database = teapot.database.__init__()
2323
db = teapot.database.db(database)
24-
db.execute("INSERT INTO " + str(
25-
member.guild.id) + "_logs" + "(timestamp, guild_id, channel_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s)",
26-
(teapot.time(), member.guild.id, member.channel.id, member.id, "MEMBER_JOIN"))
24+
db.execute(
25+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s)",
26+
(teapot.time(), member.guild.id, member.channel.id, member.id, "MEMBER_JOIN"))
2727
database.commit()
2828
except Exception as e:
2929
print(e)
@@ -36,9 +36,9 @@ async def on_member_remove(member):
3636
try:
3737
database = teapot.database.__init__()
3838
db = teapot.database.db(database)
39-
db.execute("INSERT INTO " + str(
40-
member.guild.id) + "_logs" + "(timestamp, guild_id, channel_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s)",
41-
(teapot.time(), member.guild.id, member.channel.id, member.id, "MEMBER_REMOVE"))
39+
db.execute(
40+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s)",
41+
(teapot.time(), member.guild.id, member.channel.id, member.id, "MEMBER_REMOVE"))
4242
database.commit()
4343
except Exception as e:
4444
print(e)
@@ -70,10 +70,10 @@ async def on_message(message):
7070
db.execute("INSERT INTO `channels`(channel_id, channel_name) VALUES(%s, %s)",
7171
(message.channel.id, message.channel.name))
7272
database.commit()
73-
db.execute("INSERT INTO `" + str(
74-
guild.id) + "_logs" + "`(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
75-
(teapot.time(), message.guild.id, message.channel.id, message.id, message.author.id,
76-
"MESSAGE_SEND", message.content))
73+
db.execute(
74+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
75+
(teapot.time(), message.guild.id, message.channel.id, message.id, message.author.id,
76+
"MESSAGE_SEND", message.content))
7777
database.commit()
7878
except Exception as e:
7979
print(e)
@@ -93,28 +93,24 @@ async def on_raw_message_edit(ctx):
9393
try:
9494
database = teapot.database.__init__()
9595
db = teapot.database.db(database)
96-
db.execute("INSERT INTO " + str(
97-
guild_id) + "_logs" + "(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
98-
(teapot.time(), guild_id, channel_id, message_id, author_id, "MESSAGE_EDIT", content))
96+
db.execute(
97+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
98+
(teapot.time(), guild_id, channel_id, message_id, author_id, "MESSAGE_EDIT", content))
9999
database.commit()
100100
except Exception as e:
101101
print(e)
102-
elif teapot.config.storage_type() == "flatfile":
103-
pass
104102
except:
105103
content = str(json.loads(json.dumps(ctx.data))['embeds'])
106104
if teapot.config.storage_type() == "mysql":
107105
try:
108106
database = teapot.database.__init__()
109107
db = teapot.database.db(database)
110-
db.execute("INSERT INTO " + str(
111-
guild_id) + "_logs" + "(timestamp, guild_id, channel_id, message_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s)",
112-
(teapot.time(), guild_id, channel_id, message_id, "MESSAGE_EDIT", content))
108+
db.execute(
109+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, message_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s)",
110+
(teapot.time(), guild_id, channel_id, message_id, "MESSAGE_EDIT", content))
113111
database.commit()
114112
except Exception as e:
115113
print(e)
116-
elif teapot.config.storage_type() == "flatfile":
117-
pass
118114

119115

120116
def message_delete(bot):
@@ -124,14 +120,12 @@ async def on_message_delete(ctx):
124120
try:
125121
database = teapot.database.__init__()
126122
db = teapot.database.db(database)
127-
db.execute("INSERT INTO " + str(
128-
ctx.guild.id) + "_logs" + "(timestamp, guild_id, channel_id, message_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s, %s)",
129-
(teapot.time(), ctx.guild.id, ctx.channel.id, ctx.id, ctx.author.id, "MESSAGE_DELETE"))
123+
db.execute(
124+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, message_id, user_id, action_type) VALUES(%s, %s, %s, %s, %s, %s)",
125+
(teapot.time(), ctx.guild.id, ctx.channel.id, ctx.id, ctx.author.id, "MESSAGE_DELETE"))
130126
database.commit()
131127
except Exception as e:
132128
print(e)
133-
elif teapot.config.storage_type() == "flatfile":
134-
pass
135129

136130

137131
def on_command_error(bot):
@@ -141,10 +135,10 @@ async def on_command_error(ctx, error):
141135
try:
142136
database = teapot.database.__init__()
143137
db = teapot.database.db(database)
144-
db.execute("INSERT INTO " + str(
145-
ctx.guild.id) + "_logs" + "(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
146-
(teapot.time(), ctx.guild.id, ctx.message.channel.id, ctx.message.id, ctx.message.author.id,
147-
"CMD_ERROR", str(error)))
138+
db.execute(
139+
"INSERT INTO `guild_logs`(timestamp, guild_id, channel_id, message_id, user_id, action_type, message) VALUES(%s, %s, %s, %s, %s, %s, %s)",
140+
(teapot.time(), ctx.guild.id, ctx.message.channel.id, ctx.message.id, ctx.message.author.id,
141+
"CMD_ERROR", str(error)))
148142
database.commit()
149143
except Exception as e:
150144
print(e)

teapot/managers/database.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,43 @@ def db(database):
4444
quit()
4545

4646

47+
def create_table(stmt):
48+
database = teapot.managers.database.__init__()
49+
db = teapot.managers.database.db(database)
50+
51+
db.execute(stmt)
52+
db.close()
53+
del db
54+
55+
56+
def insert(stmt, var):
57+
database = teapot.managers.database.__init__()
58+
db = teapot.managers.database.db(database)
59+
60+
db.execute(stmt, var)
61+
database.commit()
62+
63+
db.close()
64+
del db
65+
66+
67+
def insert_if_not_exists(stmt):
68+
database = teapot.managers.database.__init__()
69+
db = teapot.managers.database.db(database)
70+
71+
db.execute(stmt)
72+
database.commit()
73+
74+
db.close()
75+
del db
76+
77+
4778
def create_guild_table(guild):
4879
database = teapot.managers.database.__init__()
4980
db = teapot.managers.database.db(database)
5081

5182
db.execute("SELECT * FROM `guilds` WHERE guild_id = '" + str(guild.id) + "'")
5283
if db.rowcount == 0:
53-
db.execute("INSERT INTO `guilds`(guild_id, guild_name) VALUES(%s, %s)", (guild.id, guild.name))
54-
database.commit()
55-
db.execute("CREATE TABLE IF NOT EXISTS `" + str(
56-
guild.id) + "_logs" + "` (`timestamp` TEXT, `guild_id` BIGINT, `channel_id` BIGINT, `message_id` "
57-
"BIGINT, `user_id` BIGINT, `action_type` TINYTEXT, `message` MEDIUMTEXT)")
84+
insert("INSERT INTO `guilds`(guild_id, guild_name) VALUES(%s, %s)", (guild.id, guild.name))
85+
create_table("CREATE TABLE IF NOT EXISTS `guild_logs` (`timestamp` TEXT, `guild_id` BIGINT, `channel_id` BIGINT, "
86+
"`message_id` BIGINT, `user_id` BIGINT, `action_type` TINYTEXT, `message` MEDIUMTEXT)")

0 commit comments

Comments
 (0)