Skip to content

Commit

Permalink
Merge pull request #175 from MiscGuild/backend-redo
Browse files Browse the repository at this point in the history
Backend redo
  • Loading branch information
Amxgh authored Mar 21, 2024
2 parents a77254f + afbf858 commit c2bf2dc
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/func/General.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


class General:
async def weeklylb(ctx):
async def weeklylb(ctx, is_automatic=False):
# Get guild data
guild_data = await get_guild_by_name(guild_handle)

Expand All @@ -42,7 +42,7 @@ async def weeklylb(ctx):

# text = "&f&lWeekly Top&r%5Cn"
text = "**Weekly Top**\n"
text = await generate_lb_text(member_gexp, text)
text = await generate_lb_text(member_gexp, text, is_automatic)
return text
# return await get_jpg_file(f"https://fake-chat.matdoes.dev/render.png?m=custom&d={text}&t=1")

Expand Down Expand Up @@ -106,12 +106,11 @@ async def rolecheck(ctx, send_ping: bool):
continue

nick = await name_grabber(discord_member)
uuid_username = await get_db_uuid_username_from_discord_id(discord_member.id)
if not uuid_username:
uuid, username = await get_db_uuid_username_from_discord_id(discord_member.id)
if not uuid and not username:
await discord_member.remove_roles(bot.member_role, bot.ally, bot.guest, bot.active_role)
await discord_member.add_roles(bot.new_member_role)
continue
uuid, username = uuid_username
has_tag_permission = await has_tag_perms(discord_member)
await progress_message.edit(content=f"Checking {nick} - {discord_member}")

Expand Down
4 changes: 2 additions & 2 deletions src/func/Integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def giveawayreroll(self, reroll_number: int = None):
return "This giveaway hasn't ended yet!\n`To end it, use ,giveawayend`"
await roll_giveaway(self.integer, reroll_number)

async def gtop(self, ctx):
async def gtop(self, ctx, is_automatic=False):
# Check no. days requested to prevent errors
if self.integer > 6:
return discord.Embed(title="Invalid timestamp!", description="You cannot request data this old!",
Expand All @@ -67,7 +67,7 @@ async def gtop(self, ctx):
# Get image data
# text = f"&f&lDaily Top: {date}&r%5Cn"
text = f"**Daily Top: {date}**\n"
text = await generate_lb_text(member_gexp, text)
text = await generate_lb_text(member_gexp, text, is_automatic)

# for x in range(5):
# file = await get_jpg_file(f"https://fake-chat.matdoes.dev/render.png?m=custom&d={text}&t=1")
Expand Down
18 changes: 13 additions & 5 deletions src/utils/calculation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from datetime import datetime, timedelta

from src.utils.consts import ChatColor, active_req, member_req, resident_req
from src.utils.request_utils import get_player_guild, get_name_by_uuid, get_hypixel_player
from src.utils.db_utils import check_uuid_in_db, get_db_username_from_uuid
from src.utils.request_utils import get_player_guild, get_name_by_uuid


async def get_player_gexp(uuid: str, guild_data: dict = None):
Expand Down Expand Up @@ -118,17 +119,24 @@ async def get_gexp_sorted(guild_data: dict):
return member_gexp


async def generate_lb_text(member_gexp: list, text: str):
async def generate_lb_text(member_gexp: list, text: str, do_ping):
# Generate leaderboard text
count = 0
for uuid, gexp in member_gexp[:10]:
count += 1
name = await get_name_by_uuid(uuid)
rank, _ = await get_hypixel_player_rank(
await get_hypixel_player(uuid=uuid)) # Ignores value without color formatting

discord_id = await check_uuid_in_db(uuid)
username = await get_db_username_from_uuid(uuid=uuid)
if discord_id and do_ping:
name = f"<@{discord_id}>"
else:
name = await get_name_by_uuid(uuid) if not username else username

# rank, _ = await get_hypixel_player_rank(
# await get_hypixel_player(uuid=uuid)) # Ignores value without color formatting
# Add new entry to image content
##text += f"&6{count}. {rank} {name} &2{format(gexp, ',d')} Guild Experience"

text += f"> {count}. {name} - {format(gexp, ',d')} Guild Experience"
# Add new line
if count < 10:
Expand Down
14 changes: 10 additions & 4 deletions src/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,28 @@ async def get_invites(inviter_uuid):


async def get_db_uuid_username_from_discord_id(discord_id: int):
return await select_one("SELECT uuid, username from members WHERE discord_id = (?)", (discord_id,))

res = await select_one("SELECT uuid, username from members WHERE discord_id = (?)", (discord_id,))
return (res[0], res[1]) if res else (None, None)

async def get_db_username_from_uuid(uuid: str):
return (await select_one("SELECT username from members WHERE uuid = (?)", (uuid,)))[0]
username = await select_one("SELECT username from members WHERE uuid = (?)", (uuid,))
return username[0] if username else username

async def insert_new_member(discord_id: int, uuid: str, username: str):
await bot.db.execute("INSERT INTO members VALUES (?, ?, ?)", (discord_id, uuid, username))
await bot.db.commit()


async def update_member(discord_id: int, uuid: str, username: str):
discord_idExists = (await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,)))[0]
discord_idExists = await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,))
if discord_idExists:
await bot.db.execute("UPDATE members SET uuid = (?) and username = (?) WHERE discord_id = (?)",
(uuid, username, discord_id))
await bot.db.commit()
else:
await insert_new_member(discord_id, uuid, username)


async def check_uuid_in_db(uuid: str):
discord_id = (await select_one("SELECT discord_id from members WHERE uuid=(?)", (uuid,)))
return discord_id[0] if discord_id else 0
5 changes: 3 additions & 2 deletions src/utils/loop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ async def before_giveaway_check():
@tasks.loop(hours=24)
async def send_gexp_lb():
await asyncio.sleep(1)
file = await Integer(integer=1).gtop(bot.get_channel(daily_lb_channel))
file = await Integer(integer=1).gtop(bot.get_channel(daily_lb_channel), is_automatic=True)
await bot.get_channel(daily_lb_channel).send(file)
if datetime.utcnow().weekday() == 0:
await bot.get_channel(weekly_lb_channel).send(
f"__Week {int(80 + round((datetime.utcnow() - datetime.strptime('14/08/2022', '%d/%m/%Y')).days / 7))}__\n"
f"**{(datetime.utcnow() - timedelta(days=7)).strftime('%d %b %Y')} "
f"-"
f" {datetime.utcnow().strftime('%d %B %Y')}**")
await bot.get_channel(weekly_lb_channel).send(await General.weeklylb(bot.get_channel(weekly_lb_channel)))
await bot.get_channel(weekly_lb_channel).send(
await General.weeklylb(bot.get_channel(weekly_lb_channel), is_automatic=True))


@send_gexp_lb.before_loop
Expand Down
9 changes: 7 additions & 2 deletions src/utils/referral_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.utils.calculation_utils import get_player_gexp, get_gexp_sorted
from src.utils.consts import guild_handle, member_req, active_req, rank_upgrade_channel, \
rank_upgrade_winner_announcement
from src.utils.db_utils import select_one, insert_new_inviter, add_invitee
from src.utils.db_utils import select_one, insert_new_inviter, add_invitee, check_uuid_in_db
from src.utils.request_utils import get_mojang_profile, get_player_guild, get_guild_by_name, get_name_by_uuid


Expand Down Expand Up @@ -90,7 +90,12 @@ async def generate_rank_upgrade(weekly_invites : list):
weighted_entries = [uuid for uuid, weight in entries.items() for _ in range(weight)]
shuffle(weighted_entries)
winner_uuid = choice(weighted_entries)
winner = await get_name_by_uuid(winner_uuid)

discord_id = await check_uuid_in_db(winner_uuid)
if discord_id:
winner = f"<@{discord_id}>"
else:
winner = await get_name_by_uuid(winner_uuid)

winner_gexp = None
for uuid, gexp in members:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ticket_utils/gvg_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import discord

import src.utils.ui_utils as uiutils
from src.utils.calculation_utils import get_hypixel_player
from src.utils.consts import unknown_ign_embed, neutral_color, ticket_categories, neg_color, gvg_requirements, \
missing_permissions_embed
from src.utils.request_utils import get_hypixel_player


async def gvg_approve(channel: discord.TextChannel, author: discord.User, ign: str, uuid: str, embed: discord.Embed,
Expand Down

0 comments on commit c2bf2dc

Please sign in to comment.