From 461d7a382b98ad370b0810479d21b09a4f536fc0 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:25:56 -0500 Subject: [PATCH 01/30] feat: created a new db table for discord members and their uuids. --- src/utils/db_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 8a7a9704..1cf159a3 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -7,6 +7,11 @@ async def connect_db(): bot.db = await aiosqlite.connect("database.db") + # Discord Member Table: + await bot.db.execute("""CREATE TABLE IF NOT EXISTS members ( + discord_id integer PRIMARY KEY NOT NULL, + uuid text NOT NULL)""") + # DNKL table: await bot.db.execute("""CREATE TABLE IF NOT EXISTS dnkl ( message_id integer NOT NULL, @@ -119,3 +124,4 @@ async def get_invites(inviter_uuid): return (await select_one( "SELECT current_invitee_uuids, total_invites, total_valid_invites FROM invites WHERE inviter_uuid = (?)", (inviter_uuid,))) + From 27de19c2b6842474262b4a5a592e78f78501e1e9 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:31:18 -0500 Subject: [PATCH 02/30] feat: new function that fetches a user's uuid from db using their discord id --- src/utils/db_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 1cf159a3..04400fdb 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -125,3 +125,6 @@ async def get_invites(inviter_uuid): "SELECT current_invitee_uuids, total_invites, total_valid_invites FROM invites WHERE inviter_uuid = (?)", (inviter_uuid,))) + +async def get_uuid(discord_id: int): + return (await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,)))[0] From c757bd0a45adaaadc30baec55b4c90c52f9027e5 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:33:40 -0500 Subject: [PATCH 03/30] feat: new function that adds a new user's discord id and uuid to the member table --- src/utils/db_utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 04400fdb..2ed54d24 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -128,3 +128,8 @@ async def get_invites(inviter_uuid): async def get_uuid(discord_id: int): return (await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,)))[0] + + +async def insert_new_member(discord_id: int, uuid: str): + await bot.db.execute("INSERT INTO members VALUES (?, ?)", (discord_id, uuid)) + await bot.db.commit() From 8fa3a5074c495b536f20f6f4307403e766625d2c Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:41:14 -0500 Subject: [PATCH 04/30] feat: new function that updates a member's uuid/inserts new member if member not found --- src/utils/db_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 2ed54d24..41e1316c 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -133,3 +133,12 @@ async def get_uuid(discord_id: int): async def insert_new_member(discord_id: int, uuid: str): await bot.db.execute("INSERT INTO members VALUES (?, ?)", (discord_id, uuid)) await bot.db.commit() + + +async def update_member(discord_id: int, uuid: str): + discord_idExists = (await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,)))[0] + if discord_idExists: + await bot.db.execute("UPDATE members SET uuid = (?) WHERE discord_id = (?)", (uuid, discord_id)) + await bot.db.commit() + else: + await insert_new_member(discord_id, uuid) From 95daf684722a2aeec9e9b546de93a01d352aee07 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:45:33 -0500 Subject: [PATCH 05/30] feat: updated members table to include username --- src/utils/db_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 41e1316c..2d3314f7 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -10,7 +10,8 @@ async def connect_db(): # Discord Member Table: await bot.db.execute("""CREATE TABLE IF NOT EXISTS members ( discord_id integer PRIMARY KEY NOT NULL, - uuid text NOT NULL)""") + uuid text NOT NULL, + username text)""") # DNKL table: await bot.db.execute("""CREATE TABLE IF NOT EXISTS dnkl ( From ec0e70d59b31fd3bb6ed7f2a7a75744695fb347e Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:46:11 -0500 Subject: [PATCH 06/30] feat: changed functionality of get_uuid() to support the addition of username to the db --- src/utils/db_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 2d3314f7..5dd59c04 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -127,8 +127,8 @@ async def get_invites(inviter_uuid): (inviter_uuid,))) -async def get_uuid(discord_id: int): - return (await select_one("SELECT uuid from members WHERE discord_id = (?)", (discord_id,)))[0] +async def get_db_uuid_username(discord_id: int): + return (await select_one("SELECT uuid, username from members WHERE discord_id = (?)", (discord_id,)))[0] async def insert_new_member(discord_id: int, uuid: str): From 8fdca519a9b46d473323f388f5b467bc658338e7 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:46:24 -0500 Subject: [PATCH 07/30] feat: changed functionality of insert_new_member() to support the addition of username to the db --- src/utils/db_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 5dd59c04..338df065 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -131,8 +131,8 @@ async def get_db_uuid_username(discord_id: int): return (await select_one("SELECT uuid, username from members WHERE discord_id = (?)", (discord_id,)))[0] -async def insert_new_member(discord_id: int, uuid: str): - await bot.db.execute("INSERT INTO members VALUES (?, ?)", (discord_id, uuid)) +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() From e6d883aa0db793f3f1654603cf060203ac72842e Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Wed, 20 Mar 2024 23:46:52 -0500 Subject: [PATCH 08/30] feat: changed functionality of update_member() to support the addition of username to the db --- src/utils/db_utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 338df065..920040ae 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -136,10 +136,11 @@ async def insert_new_member(discord_id: int, uuid: str, username: str): await bot.db.commit() -async def update_member(discord_id: int, uuid: str): +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] if discord_idExists: - await bot.db.execute("UPDATE members SET uuid = (?) WHERE discord_id = (?)", (uuid, discord_id)) + 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) + await insert_new_member(discord_id, uuid, username) From 1301b3426274641a6d0c46d63d62133a9718d52b Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 02:03:58 -0500 Subject: [PATCH 09/30] feat: updated qotd embed format --- src/func/String.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/func/String.py b/src/func/String.py index 4a0a4683..7306d6f5 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -253,9 +253,11 @@ async def qotd(self, ctx): # 15th May 2022 was the 473rd QOTD day. It is used as a reference point to calculate the day number. day_number = 473 + (datetime.utcnow() - datetime.strptime("2022/05/15", "%Y/%m/%d")).days embed = discord.Embed( - title=f"Day {day_number}: {datetime.utcnow().day} {months[datetime.utcnow().month]} {datetime.utcnow().year}", - description=self.string, color=neutral_color) - embed.set_footer(text="Put your answers in #qotd-answer") + title=f"**{self.string}\n**", + description=f"You can respond to this qotd in: <#{qotd_ans_channel_id}>", color=neutral_color) + embed.set_author( + name=f"Day {day_number}: {datetime.utcnow().day} {months[datetime.utcnow().month]} {datetime.utcnow().year}") + embed.set_footer(text="- " + ctx.author.nick if ctx.author.nick else ctx.author.name) await bot.get_channel(qotd_channel_id).send("<@&923978802818871356>", embed=embed) await ctx.send(f"**The QOTD has been sent to <#{qotd_channel_id}>!**") From babb0c1dd21ec1a85c99c9c6696b81a665f2447a Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 02:05:06 -0500 Subject: [PATCH 10/30] refactor: changed function name to improve clarity --- src/utils/db_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 920040ae..c2d30009 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -127,7 +127,7 @@ async def get_invites(inviter_uuid): (inviter_uuid,))) -async def get_db_uuid_username(discord_id: int): +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,)))[0] From 145c61ee0a23c3416a89062742fee4eca6309670 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 02:05:28 -0500 Subject: [PATCH 11/30] feat: new func that returns the player's username from their uuid using the db --- src/utils/db_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index c2d30009..8be14bad 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -131,6 +131,9 @@ 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,)))[0] +async def get_db_username_from_uuid(uuid: str): + return (await select_one("SELECT username from members WHERE uuid = (?)", (uuid,)))[0] + 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() From 5d671efcdeae4f9ae8d1c160b9750ff9a8ff35ab Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 02:06:31 -0500 Subject: [PATCH 12/30] refactor: optimized imports --- src/cogs/general.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/cogs/general.py b/src/cogs/general.py index 5a9a9726..5f97df84 100644 --- a/src/cogs/general.py +++ b/src/cogs/general.py @@ -1,12 +1,11 @@ import discord -from discord.ext import commands, bridge from discord.commands import option +from discord.ext import commands, bridge from src.func.String import String from src.func.Union import Union - class General(commands.Cog, name="general"): """ Contains source, avatar, qotd. @@ -54,10 +53,8 @@ async def callback(self, interaction: discord.Interaction): await interaction.response.send_message("The QOTD has been sent!") await String(string=self.children[0].value).qotd(ctx) - await ctx.send_modal(modal=ModalCreator()) - def setup(bot): bot.add_cog(General(bot)) From 0d454191e861d0b4a44ad38f0b38306f82a8dd91 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 02:06:52 -0500 Subject: [PATCH 13/30] feat: set a max length for a qotd --- src/cogs/general.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cogs/general.py b/src/cogs/general.py index 5f97df84..c6e4abf9 100644 --- a/src/cogs/general.py +++ b/src/cogs/general.py @@ -46,8 +46,10 @@ class ModalCreator(discord.ui.Modal): def __init__(self) -> None: # fields = ["LABEL", "PLACEHOLDER", STYLE] super().__init__(title="QOTD Creator") - self.add_item(discord.ui.InputText(label="What is the question of the day?", placeholder="Enter the question here", style=discord.InputTextStyle.long)) - + self.add_item(discord.ui.InputText(label="What is the question of the day?", + placeholder="Enter the question here", + max_length=256, + style=discord.InputTextStyle.long)) async def callback(self, interaction: discord.Interaction): await interaction.response.send_message("The QOTD has been sent!") From 7f25c49badaa318540f1b25f911c2b6ce7581fbf Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:25:51 -0500 Subject: [PATCH 14/30] feat: gmember() now fetches the uuid and username from the db instead of looking at a user's nick. --- src/cogs/guild.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cogs/guild.py b/src/cogs/guild.py index b0939932..70fb334f 100644 --- a/src/cogs/guild.py +++ b/src/cogs/guild.py @@ -27,9 +27,10 @@ def __init__(self, bot): async def gmember(self, ctx, name: str = None): """View the given user's guild experience over the past week!""" if not name: - name = await name_grabber(ctx.author) - - res = await String(string=name).gmember(ctx) + uuid, username = await get_db_uuid_username_from_discord_id(ctx.author.id) + res = await String(uuid=uuid, username=username).gmember(ctx) + else: + res = await String(string=name).gmember(ctx) if isinstance(res, discord.Embed): await ctx.respond(embed=res) if isinstance(res, str): From 0b29c4c297375f7c0919ecefd4797a74e74d1724 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:26:07 -0500 Subject: [PATCH 15/30] feat: invites() now fetches the uuid and username from the db instead of looking at a user's nick. --- src/cogs/guild.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cogs/guild.py b/src/cogs/guild.py index 70fb334f..99471e74 100644 --- a/src/cogs/guild.py +++ b/src/cogs/guild.py @@ -6,7 +6,7 @@ from src.func.Integer import Integer from src.func.String import String from src.utils.consts import gvg_info_embed, requirements_embed, resident_embed -from src.utils.discord_utils import name_grabber +from src.utils.db_utils import get_db_uuid_username_from_discord_id class Guild(commands.Cog, name="guild"): @@ -92,8 +92,11 @@ async def invites(self, ctx, name: str = None): """View your invitation stats""" await ctx.defer() if not name: - name = await name_grabber(ctx.author) - await ctx.respond(embed=await String(name).invites()) + uuid, _ = await get_db_uuid_username_from_discord_id(ctx.author.id) + res = await String(string=uuid).invites() + else: + res = await String(string=name).invites() + await ctx.respond(embed=res) def setup(bot): bot.add_cog(Guild(bot)) From 8ce37a44f224c09998a4a1d2ee42c97358f80e8c Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:26:20 -0500 Subject: [PATCH 16/30] feat: info() now fetches the uuid and username from the db instead of looking at a user's nick. --- src/cogs/hypixel.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cogs/hypixel.py b/src/cogs/hypixel.py index 3ecdc688..b81cae1f 100644 --- a/src/cogs/hypixel.py +++ b/src/cogs/hypixel.py @@ -5,7 +5,7 @@ from src.func.General import General from src.func.String import String from src.func.Union import Union -from src.utils.discord_utils import name_grabber +from src.utils.db_utils import get_db_uuid_username_from_discord_id class Hypixel(commands.Cog, name="hypixel"): @@ -47,8 +47,11 @@ async def sync(self, ctx, name, tag: str = None): async def info(self, ctx, name: str = None): """View Hypixel stats of the given user!""" if not name: - name = await name_grabber(ctx.author) - await ctx.respond(embed=await String(string=name).info()) + uuid, username = await get_db_uuid_username_from_discord_id(ctx.author.id) + res = await String(uuid=uuid, username=username).info() + else: + res = await String(string=name).info() + await ctx.respond(embed=res) @bridge.bridge_command(aliases=['dnkladd', 'dnkla']) @commands.has_any_role("Staff") From 41bcbcd10f89eb5889079dbbf418b2124f1cd644 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:26:33 -0500 Subject: [PATCH 17/30] feat: dnkl_check() now fetches the uuid and username from the db instead of looking at a user's nick. --- src/cogs/hypixel.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cogs/hypixel.py b/src/cogs/hypixel.py index b81cae1f..f6c26361 100644 --- a/src/cogs/hypixel.py +++ b/src/cogs/hypixel.py @@ -96,9 +96,11 @@ async def dnkl_list(self, ctx): async def dnkl_check(self, ctx, name: str = None): """Check whether you are eligible for the do-not-kick-list!""" if not name: - name = await name_grabber(ctx.author) + uuid, username = await get_db_uuid_username_from_discord_id(ctx.author.id) + res = await String(uuid=uuid, username=username).dnklcheck() + else: + res = await String(string=name).dnklcheck() - res = await String(string=name).dnklcheck() if isinstance(res, discord.Embed): await ctx.respond(embed=res) elif isinstance(res, str): From 600a6d7b192a16d54f3486453172f0ced069254b Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:27:14 -0500 Subject: [PATCH 18/30] feat: removed rolecheck --- src/func/General.py | 138 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 3 deletions(-) diff --git a/src/func/General.py b/src/func/General.py index e87be6de..d44347e4 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -5,14 +5,13 @@ from __main__ import bot from datetime import datetime, timedelta -import aiohttp import discord import discord.ui from src.func.Union import Union from src.utils.calculation_utils import get_gexp_sorted, generate_lb_text from src.utils.consts import (accepted_staff_application_embed, active_req, - allies, error_color, guild_handle, + error_color, guild_handle, invalid_guild_embed, log_channel_id, member_req, milestone_emojis, milestones_channel, neg_color, neutral_color, pos_color, ticket_deleted_embed, @@ -20,7 +19,8 @@ staff_application_questions, ticket_categories, resident_req, dnkl_entries_not_found, positive_responses) -from src.utils.db_utils import insert_new_giveaway, select_all +from src.utils.db_utils import insert_new_giveaway, select_all, get_db_username_from_uuid, \ + get_db_uuid_username_from_discord_id from src.utils.discord_utils import (create_ticket, get_ticket_creator, log_event, name_grabber, has_tag_perms) @@ -193,6 +193,138 @@ async def sem_task(task): await progress_message.edit(content="Rolecheck complete!") + # + # async def rolecheck(ctx, send_ping: bool): + # # Define a message for sending progress updates + # progress_message = await ctx.respond("Processing prerequisites...") + # + # discord_members = bot.guild.members + # + # # Define arrays for guild and ally uuids and names + # guild_members = (await get_guild_by_name(guild_handle))['members'] + # guild_uuids = await get_guild_uuids(guild_handle) + # guild_usernames, ally_usernames, ally_uuids, ally_divisions = [], [], [], [] + # + # # Appending UUIDs of members of all ally guilds into one array + # for ally in allies: + # await progress_message.edit(content=f"Fetching ally UUIDs - {ally}") + # ally_uuids.extend(await get_guild_uuids(ally)) + # req = await get_player_guild(ally_uuids[-1]) + # gtag = " " if not req["tag"] or not req else req["tag"] + # ally_divisions.append([len(ally_uuids), gtag]) + # # Ally divisions marks the separation point of one guild + # # from another in the ally_uuids array along with the guild's gtag + # + # # Limiting the maximum concurrency + # async def gather_with_concurrency(n, *tasks): + # semaphore = asyncio.Semaphore(n) + # + # async def sem_task(task): + # async with semaphore: + # return await task + # + # return await asyncio.gather(*(sem_task(task) for task in tasks)) + # + # # Get guild and ally names from their respective UUIDs + # await progress_message.edit(content="Retrieving usernames...") + # + # for _set in [[guild_uuids, guild_usernames], [ally_uuids, ally_usernames]]: + # draw, dump = _set + # async with aiohttp.ClientSession(): + # tasks = await gather_with_concurrency(2, + # *[ + # get_name_by_uuid(uuid) for uuid in draw + # ]) # Gathering with a max concurrency of 2 + # dump.extend(tasks) + # # Loop through discord members + # + # await ctx.send("If you see the bot is stuck on a member along with an error message, " + # "forcesync member the bot is stuck on.") + # bot.admin_ids = [member.id for member in bot.admin.members] + # for discord_member in discord_members: + # # Do not check admins and bots + # if discord_member.id in bot.admin_ids or discord_member.bot: + # continue + # + # username = await name_grabber(discord_member) + # has_tag_permission = await has_tag_perms(discord_member) + # await progress_message.edit(content=f"Checking {username} - {discord_member}") + # # Member of guild + # if username in guild_usernames: + # for guild_member in guild_members: + # if guild_uuids[guild_usernames.index(username)] == guild_member['uuid']: + # # Finds a given UUID's corresponding hypixel data + # + # weekly_exp = sum(guild_member["expHistory"].values()) + # if weekly_exp >= active_req: # Meets active req + # await discord_member.add_roles(bot.active_role) + # elif weekly_exp < active_req and bot.active_role in discord_member.roles: # Doesn't meet active req + # await discord_member.remove_roles(bot.active_role) + # + # if not has_tag_permission: + # await discord_member.edit(nick=username) + # + # # Edit roles + # await discord_member.add_roles(bot.member_role) + # await discord_member.remove_roles(bot.new_member_role, bot.guest, bot.ally) + # continue + # + # # Member of an ally guild + # if username in ally_usernames: + # # Get player gtag + # position = ally_usernames.index(username) + # dividers = [x[0] for x in ally_divisions] + # tags = [x[1] for x in ally_divisions] + # + # start = 0 + # for divider in dividers: + # if start <= position < divider: + # gtag = tags[dividers.index(divider)] + # break + # start = divider + # + # ''' + # last_value = 1 + # for guild_division in ally_divisions: + # if last_value > 1: + # if last_value < position < guild_division[0]: + # gtag = guild_division[1] + # + # elif position < guild_division[0]: + # gtag = guild_division[1] + # last_value = guild_division[0] + # ''' + # # Set nick + # if not discord_member.nick or f" [{gtag}]" not in discord_member.nick: + # await discord_member.edit(nick=username + f' [{gtag}]') + # + # # Edit roles + # await discord_member.add_roles(bot.guest, bot.ally) + # await discord_member.remove_roles(bot.new_member_role, bot.member_role, bot.active_role) + # continue + # + # # Get player data + # username, uuid = await get_mojang_profile(username) + # if not username: + # # Edit roles and continue loop + # 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 + # + # # Guests + # else: + # if not has_tag_permission: + # await discord_member.edit(nick=username) + # await discord_member.add_roles(bot.guest) + # await discord_member.remove_roles(bot.new_member_role, bot.member_role, bot.active_role, bot.ally) + # continue + # + # # Send ping to new member role in registration channel + # if send_ping: + # await bot.get_channel(registration_channel_id).send(bot.new_member_role.mention, embed=registration_embed) + # + # await progress_message.edit(content="Rolecheck complete!") + async def delete(ctx): embed = discord.Embed(title="This ticket will be deleted in 10 seconds!", color=neg_color) From a5774d88359f57febf4c4b1714edb332390fb28e Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:27:53 -0500 Subject: [PATCH 19/30] feat: implemented new rolecheck --- src/func/General.py | 122 +++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 80 deletions(-) diff --git a/src/func/General.py b/src/func/General.py index d44347e4..e5d391a7 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -68,43 +68,34 @@ async def rolecheck(ctx, send_ping: bool): discord_members = bot.guild.members + discord_member_uuids = await select_all("SELECT uuid FROM members") + # Define arrays for guild and ally uuids and names guild_members = (await get_guild_by_name(guild_handle))['members'] guild_uuids = await get_guild_uuids(guild_handle) - guild_usernames, ally_usernames, ally_uuids, ally_divisions = [], [], [], [] - # Appending UUIDs of members of all ally guilds into one array + # Allies dict; {uuid: {username, gtag}} + allies = {} + + # Guild dict; {uuid: {username, gexp}} + guild = {} + for ally in allies: - await progress_message.edit(content=f"Fetching ally UUIDs - {ally}") - ally_uuids.extend(await get_guild_uuids(ally)) - req = await get_player_guild(ally_uuids[-1]) - gtag = " " if not req["tag"] or not req else req["tag"] - ally_divisions.append([len(ally_uuids), gtag]) - # Ally divisions marks the separation point of one guild - # from another in the ally_uuids array along with the guild's gtag - - # Limiting the maximum concurrency - async def gather_with_concurrency(n, *tasks): - semaphore = asyncio.Semaphore(n) - - async def sem_task(task): - async with semaphore: - return await task - - return await asyncio.gather(*(sem_task(task) for task in tasks)) - - # Get guild and ally names from their respective UUIDs - await progress_message.edit(content="Retrieving usernames...") - - for _set in [[guild_uuids, guild_usernames], [ally_uuids, ally_usernames]]: - draw, dump = _set - async with aiohttp.ClientSession(): - tasks = await gather_with_concurrency(2, - *[ - get_name_by_uuid(uuid) for uuid in draw - ]) # Gathering with a max concurrency of 2 - dump.extend(tasks) - # Loop through discord members + await progress_message.edit(content=f"Fetching ally UUIDs and usernames - {ally}") + ally_uuids = await get_guild_uuids(ally) + gtag = (await get_player_guild(ally_uuids[-1]))["tag"] + for uuid in ally_uuids: + if uuid not in discord_member_uuids: + continue + allies[uuid] = {"username": await get_db_username_from_uuid(uuid), "tag": gtag} + + await progress_message.edit(content=f"Fetching {guild_handle}'s UUIDs and usernames") + + for uuid in guild_uuids: + if uuid not in discord_member_uuids: + continue + guild[uuid] = {"username": await get_db_username_from_uuid(uuid), + "gexp": sum(guild_members["uuid"]["expHistory"].values())} await ctx.send("If you see the bot is stuck on a member along with an error message, " "forcesync member the bot is stuck on.") @@ -114,22 +105,20 @@ async def sem_task(task): if discord_member.id in bot.admin_ids or discord_member.bot: continue - username = await name_grabber(discord_member) + nick = await name_grabber(discord_member) + uuid, username = await get_db_uuid_username_from_discord_id(discord_member.id) has_tag_permission = await has_tag_perms(discord_member) - await progress_message.edit(content=f"Checking {username} - {discord_member}") + await progress_message.edit(content=f"Checking {nick} - {discord_member}") + # Member of guild - if username in guild_usernames: - for guild_member in guild_members: - if guild_uuids[guild_usernames.index(username)] == guild_member['uuid']: - # Finds a given UUID's corresponding hypixel data - - weekly_exp = sum(guild_member["expHistory"].values()) - if weekly_exp >= active_req: # Meets active req - await discord_member.add_roles(bot.active_role) - elif weekly_exp < active_req and bot.active_role in discord_member.roles: # Doesn't meet active req - await discord_member.remove_roles(bot.active_role) - - if not has_tag_permission: + if uuid in guild.keys(): + weekly_exp = guild[uuid]["gexp"] + if weekly_exp >= active_req: # Meets active req + await discord_member.add_roles(bot.active_role) + elif weekly_exp < active_req and bot.active_role in discord_member.roles: # Doesn't meet active req + await discord_member.remove_roles(bot.active_role) + + if nick != username and not has_tag_permission: await discord_member.edit(nick=username) # Edit roles @@ -137,33 +126,9 @@ async def sem_task(task): await discord_member.remove_roles(bot.new_member_role, bot.guest, bot.ally) continue - # Member of an ally guild - if username in ally_usernames: - # Get player gtag - position = ally_usernames.index(username) - dividers = [x[0] for x in ally_divisions] - tags = [x[1] for x in ally_divisions] - - start = 0 - for divider in dividers: - if start <= position < divider: - gtag = tags[dividers.index(divider)] - break - start = divider - - ''' - last_value = 1 - for guild_division in ally_divisions: - if last_value > 1: - if last_value < position < guild_division[0]: - gtag = guild_division[1] - - elif position < guild_division[0]: - gtag = guild_division[1] - last_value = guild_division[0] - ''' - # Set nick - if not discord_member.nick or f" [{gtag}]" not in discord_member.nick: + elif uuid in allies.keys(): + gtag = allies[uuid]["tag"] + if not discord_member.nick or nick != username or f"[{gtag}]" not in discord_member.nick: await discord_member.edit(nick=username + f' [{gtag}]') # Edit roles @@ -171,23 +136,20 @@ async def sem_task(task): await discord_member.remove_roles(bot.new_member_role, bot.member_role, bot.active_role) continue - # Get player data - username, uuid = await get_mojang_profile(username) - if not username: - # Edit roles and continue loop + elif not uuid: 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 # Guests else: - if not has_tag_permission: + if not has_tag_permission and discord_member.nick != username: await discord_member.edit(nick=username) await discord_member.add_roles(bot.guest) await discord_member.remove_roles(bot.new_member_role, bot.member_role, bot.active_role, bot.ally) continue - - # Send ping to new member role in registration channel + # Send ping to new mem + # ber role in registration channel if send_ping: await bot.get_channel(registration_channel_id).send(bot.new_member_role.mention, embed=registration_embed) From c5a8c5f435afeedfde6def645ca0ba8d5ac9c83b Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:30:30 -0500 Subject: [PATCH 20/30] feat: updated rolecheck to change a former active member's nick if it doesn't match their username --- src/func/General.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/func/General.py b/src/func/General.py index e5d391a7..2b1965fd 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -118,7 +118,8 @@ async def rolecheck(ctx, send_ping: bool): elif weekly_exp < active_req and bot.active_role in discord_member.roles: # Doesn't meet active req await discord_member.remove_roles(bot.active_role) - if nick != username and not has_tag_permission: + if (nick != username and bot.active_role not in discord_member.roles) or ( + nick != username and not has_tag_permission): await discord_member.edit(nick=username) # Edit roles From c68e84306a2fd76b08cd140a28c13f7080d1fbdb Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:31:34 -0500 Subject: [PATCH 21/30] feat: new class attributes, uuid and username --- src/func/String.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/func/String.py b/src/func/String.py index 7306d6f5..13571e5f 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -25,8 +25,10 @@ class String: - def __init__(self, string: str): + def __init__(self, string: str = None, uuid: str = None, username: str = None): self.string = string + self.uuid = uuid + self.username = username # Command from https://github.com/Rapptz/RoboDanny async def source(self): From ec77a2e9e306e5f2c4b83eebf4c7fd540bd8602e Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:32:12 -0500 Subject: [PATCH 22/30] feat: String.gmember() only calls the mojang api if self.uuid and self.username aren't present --- src/func/String.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/func/String.py b/src/func/String.py index 13571e5f..f3052071 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -66,9 +66,13 @@ async def source(self): return f"Following is the source code for {self.string}\n{final_url}" async def gmember(self, ctx): - name, uuid = await get_mojang_profile(self.string) - if not name: - return unknown_ign_embed + if self.uuid and self.username: + uuid = self.uuid + name = self.username + else: + name, uuid = await get_mojang_profile(self.string) + if not name: + return unknown_ign_embed guild = await get_player_guild(uuid) From 1253b66198785bf131cfc1ed362d53cba69f13d2 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:33:10 -0500 Subject: [PATCH 23/30] feat: String.info() only calls the mojang api if self.uuid and self.username aren't present. --- src/func/String.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/func/String.py b/src/func/String.py index f3052071..62e33e7c 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -156,7 +156,13 @@ async def gmember(self, ctx): return embed.set_image(url=chart.get_url()) async def info(self): - player_data = await get_hypixel_player(name=self.string) + if self.uuid and self.username: + uuid = self.uuid + else: + name, uuid = await get_mojang_profile(self.string) + if not name: + return unknown_ign_embed + player_data = await get_hypixel_player(name=uuid) # Player doesn't exist if not player_data: return unknown_ign_embed From 2e8edb0623877947d11cb580d497069c8856421d Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:33:26 -0500 Subject: [PATCH 24/30] feat: String.dnkl_check() only calls the mojang api if self.uuid and self.username aren't present. --- src/func/String.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/func/String.py b/src/func/String.py index 62e33e7c..2a8c8f93 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -225,7 +225,13 @@ async def dnklremove(self): return f"{username} has been removed from the do-not-kick-list!" async def dnklcheck(self): - self.string, uuid = await get_mojang_profile(self.string) + if self.uuid and self.username: + uuid = self.uuid + name = self.username + else: + name, uuid = await get_mojang_profile(self.string) + if not name: + return unknown_ign_embed _, weeklygexp = await get_player_gexp(uuid) # Player is not in a guild @@ -234,13 +240,13 @@ async def dnklcheck(self): # Player is eligible if weeklygexp > dnkl_req: - embed = discord.Embed(title=self.string, color=pos_color) + embed = discord.Embed(title=name, color=pos_color) embed.add_field(name="This player is eligible to apply for the do-not-kick-list.", value=f"They have {weeklygexp}/{dnkl_req} weekly guild experience.", inline=True) # Player is not eligible else: - embed = discord.Embed(title=self.string, color=neg_color) + embed = discord.Embed(title=name, color=neg_color) embed.add_field(name="This player is not eligible to apply for the do-not-kick-list.", value=f"They have {weeklygexp}/{dnkl_req} weekly guild experience to be eligible.", inline=True) From 1ea2c2e7f6a1ea5d9a7f24d3390651ad7d7d55c8 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 03:33:43 -0500 Subject: [PATCH 25/30] feat: String.invites() only calls the mojang api if self.uuid and self.username aren't present. --- src/func/String.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/func/String.py b/src/func/String.py index 2a8c8f93..2045dacd 100644 --- a/src/func/String.py +++ b/src/func/String.py @@ -282,9 +282,13 @@ async def qotd(self, ctx): await bot.get_channel(qotd_ans_channel_id).send(rainbow_separator) async def invites(self): - ign, uuid = await get_mojang_profile(self.string) - if not ign: - return unknown_ign_embed + if self.uuid and self.username: + uuid = self.uuid + name = self.username + else: + name, uuid = await get_mojang_profile(self.string) + if not name: + return unknown_ign_embed guild = await get_player_guild(uuid) if ("name" not in guild) or (guild["name"] != guild_handle): @@ -300,7 +304,7 @@ async def invites(self): weekly_invites = [await get_name_by_uuid(invitee) for invitee in weekly_invites] for invitee in weekly_invites: invites_text += f"**▸** {invitee}\n" - embed = discord.Embed(title=f"{ign}'s Invites", color=neutral_color) + embed = discord.Embed(title=f"{name}'s Invites", color=neutral_color) embed.add_field(name="Weekly Invites", value=None if not invites_text else invites_text, inline=False) embed.add_field(name="Total Invites", value=total_invites, inline=True) embed.add_field(name="Total Valid Invites", value=total_valid_invites, inline=True) From f96efd56779984a60e5efa20fc8fdb9447910528 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 04:29:54 -0500 Subject: [PATCH 26/30] fix: rolecheck fix --- src/func/General.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/func/General.py b/src/func/General.py index 2b1965fd..9c4f761c 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -18,9 +18,9 @@ registration_channel_id, registration_embed, staff_application_questions, ticket_categories, resident_req, dnkl_entries_not_found, - positive_responses) + positive_responses, allies) from src.utils.db_utils import insert_new_giveaway, select_all, get_db_username_from_uuid, \ - get_db_uuid_username_from_discord_id + get_db_uuid_username_from_discord_id, insert_new_member, select_one from src.utils.discord_utils import (create_ticket, get_ticket_creator, log_event, name_grabber, has_tag_perms) @@ -68,14 +68,13 @@ async def rolecheck(ctx, send_ping: bool): discord_members = bot.guild.members - discord_member_uuids = await select_all("SELECT uuid FROM members") - + discord_member_uuids = [x[0] for x in await select_all("SELECT uuid FROM members")] # Define arrays for guild and ally uuids and names - guild_members = (await get_guild_by_name(guild_handle))['members'] + guild_members = (await get_guild_by_name(guild_handle))["members"] guild_uuids = await get_guild_uuids(guild_handle) # Allies dict; {uuid: {username, gtag}} - allies = {} + allies_dict = {} # Guild dict; {uuid: {username, gexp}} guild = {} @@ -87,18 +86,19 @@ async def rolecheck(ctx, send_ping: bool): for uuid in ally_uuids: if uuid not in discord_member_uuids: continue - allies[uuid] = {"username": await get_db_username_from_uuid(uuid), "tag": gtag} - + allies_dict[uuid] = {"username": await get_db_username_from_uuid(uuid), "tag": gtag} await progress_message.edit(content=f"Fetching {guild_handle}'s UUIDs and usernames") for uuid in guild_uuids: if uuid not in discord_member_uuids: continue - guild[uuid] = {"username": await get_db_username_from_uuid(uuid), - "gexp": sum(guild_members["uuid"]["expHistory"].values())} + for player in guild_members: + if player["uuid"] == uuid: + guild[uuid] = {"username": await get_db_username_from_uuid(uuid), + "gexp": sum(player["expHistory"].values())} await ctx.send("If you see the bot is stuck on a member along with an error message, " - "forcesync member the bot is stuck on.") + "forcesync member the bot is stuck on.") bot.admin_ids = [member.id for member in bot.admin.members] for discord_member in discord_members: # Do not check admins and bots @@ -106,7 +106,12 @@ 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) + uuid_username = await get_db_uuid_username_from_discord_id(discord_member.id) + if not uuid_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}") @@ -127,8 +132,8 @@ async def rolecheck(ctx, send_ping: bool): await discord_member.remove_roles(bot.new_member_role, bot.guest, bot.ally) continue - elif uuid in allies.keys(): - gtag = allies[uuid]["tag"] + elif uuid in allies_dict.keys(): + gtag = allies_dict[uuid]["tag"] if not discord_member.nick or nick != username or f"[{gtag}]" not in discord_member.nick: await discord_member.edit(nick=username + f' [{gtag}]') @@ -151,7 +156,7 @@ async def rolecheck(ctx, send_ping: bool): continue # Send ping to new mem # ber role in registration channel - if send_ping: + if not send_ping: await bot.get_channel(registration_channel_id).send(bot.new_member_role.mention, embed=registration_embed) await progress_message.edit(content="Rolecheck complete!") From a70793b06c6a91919ba2badcff6004b1f41513fe Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 04:30:20 -0500 Subject: [PATCH 27/30] feat: implemented a new function that adds all players to the db --- src/func/General.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/func/General.py b/src/func/General.py index 9c4f761c..330aa8a3 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -901,3 +901,17 @@ async def compile_milestones(ctx): milestone_message = milestone_message + "\n**Congrats to everyone this week. If you wish to submit a milestone, look over at <#650248396480970782>!**" await bot.get_channel(milestones_channel).send(milestone_message) return f"{count} milestones have been compiled and sent in {bot.get_channel(milestones_channel)}" + + async def add_players(ctx): + for member in bot.guild.members: + if bot.new_member_role in member.roles or member.bot: + continue + record = await select_one("SELECT * FROM members where discord_id = (?)", (member.id,)) + if record: + continue + returned_name = await name_grabber(member) + name, uuid = await get_mojang_profile(returned_name) + if not (name and uuid): + continue + await insert_new_member(member.id, uuid, name) + await asyncio.sleep(15) From 7918cd15f17f47e8b23b4651a17c9858f181f73c Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 04:30:39 -0500 Subject: [PATCH 28/30] feat: created a new function for the new command --- src/cogs/staff.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/cogs/staff.py b/src/cogs/staff.py index 4513d0c2..9eb67fdc 100644 --- a/src/cogs/staff.py +++ b/src/cogs/staff.py @@ -79,6 +79,12 @@ async def rolecheck(self, ctx, send_ping: bool = True): """Sync the names and roles of everyone in the discord!""" await General.rolecheck(ctx, send_ping) + @bridge.bridge_command() + @commands.has_role("new role") + async def add_members(self, ctx): + await General.add_players(ctx) + + def setup(bot): bot.add_cog(Staff(bot)) From 0e6b4800208ed2a95c2063b58762b0a4ecd3f330 Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 04:30:59 -0500 Subject: [PATCH 29/30] fix: fixed incorrect return statement. --- src/utils/db_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/db_utils.py b/src/utils/db_utils.py index 8be14bad..9157238a 100644 --- a/src/utils/db_utils.py +++ b/src/utils/db_utils.py @@ -128,7 +128,7 @@ 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,)))[0] + return await select_one("SELECT uuid, username from members WHERE discord_id = (?)", (discord_id,)) async def get_db_username_from_uuid(uuid: str): From c848fee88198d5fedec1fcdb05438322a6a1871c Mon Sep 17 00:00:00 2001 From: "suspendedtundra2@gmail.com" Date: Thu, 21 Mar 2024 04:36:32 -0500 Subject: [PATCH 30/30] feat: added a print statement at the end --- src/func/General.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/func/General.py b/src/func/General.py index 330aa8a3..93f5112d 100644 --- a/src/func/General.py +++ b/src/func/General.py @@ -915,3 +915,4 @@ async def add_players(ctx): continue await insert_new_member(member.id, uuid, name) await asyncio.sleep(15) + await ctx.send("Adding players complete.")