From 51e67b8f603f6453c81eb132d2bac7771d2cfdd6 Mon Sep 17 00:00:00 2001 From: KDwevedi Date: Tue, 26 Dec 2023 10:16:47 +0530 Subject: [PATCH 1/2] cleanup --- cogs/discordDataScraper.py | 61 +++++++-------- cogs/serverManagement.py | 58 +++++++++------ cogs/testing.py | 149 ------------------------------------- cogs/userInteractions.py | 4 +- config/server.py | 2 +- helpers/supabaseClient.py | 26 +++++++ 6 files changed, 96 insertions(+), 204 deletions(-) delete mode 100644 cogs/testing.py diff --git a/cogs/discordDataScraper.py b/cogs/discordDataScraper.py index 236610f..5097c99 100644 --- a/cogs/discordDataScraper.py +++ b/cogs/discordDataScraper.py @@ -27,36 +27,37 @@ def __init__(self, bot) -> None: @commands.Cog.listener() async def on_message(self, message): - contributor = SupabaseClient().read( - "discord_engagement", "contributor", message.author.id - ) - print("message", len(message.content)) - if not contributor: - SupabaseClient().insert( - "discord_engagement", - { - "contributor": message.author.id, - "has_introduced": False, - "total_message_count": 1, - "total_reaction_count": 0, - }, - ) - return - if len(message.content) > 20: - if message.channel.id == INTRODUCTIONS_CHANNEL_ID: - print("intro") - SupabaseClient().update( - "discord_engagement", - {"has_introduced": True}, - "contributor", - message.author.id, - ) - SupabaseClient("discord_engagement").update( - "discord_engagement", - {"total_message_count": contributor[0]["total_message_count"] + 1}, - "contributor", - message.author.id, - ) + pass + # contributor = SupabaseClient().read( + # "discord_engagement", "contributor", message.author.id + # ) + # print("message", len(message.content)) + # if not contributor: + # SupabaseClient().insert( + # "discord_engagement", + # { + # "contributor": message.author.id, + # "has_introduced": False, + # "total_message_count": 1, + # "total_reaction_count": 0, + # }, + # ) + # return + # if len(message.content) > 20: + # if message.channel.id == INTRODUCTIONS_CHANNEL_ID: + # print("intro") + # SupabaseClient().update( + # "discord_engagement", + # {"has_introduced": True}, + # "contributor", + # message.author.id, + # ) + # SupabaseClient("discord_engagement").update( + # "discord_engagement", + # {"total_message_count": contributor[0]["total_message_count"] + 1}, + # "contributor", + # message.author.id, + # ) @commands.Cog.listener() async def on_reaction_add(self, reaction, user): diff --git a/cogs/serverManagement.py b/cogs/serverManagement.py index 00f9117..2f0cbcd 100644 --- a/cogs/serverManagement.py +++ b/cogs/serverManagement.py @@ -23,41 +23,55 @@ def validUser(self, ctx): return ctx.author.id in authorised_users @commands.command(aliaes=["initiate"]) - async def initiateServerData(self, ctx): + async def getServerData(self, ctx): # add all chapters + chapterRoles = [] guild = self.bot.get_guild(serverConfig.SERVER) for role in guild.roles: if role.name.startswith("College:"): orgName = role.name[len("College: ") :] + chapterRoles.append(role) SupabaseClient().addChapter(orgName=orgName, type="COLLEGE") elif role.name.startswith("Corporate:"): orgName = role.name[len("Corporate: ") :] + chapterRoles.append(role) SupabaseClient().addChapter(orgName=orgName, type="CORPORATE") - async for member in guild.fetch_members( - after=datetime(2023, 12, 1, 0, 0, 0), limit=None - ): - print(member.name) - SupabaseClient().updateContributor(member) - print("Done") + print("added chapters") - # async def notifs_on(self,ctx,channel: discord.TextChannel): - # try: - # SupabaseClient("discord_channels").update({"should_notify": True}, "channel_id", channel.id) - # await ctx.send(f"Notifications have been turned on for {channel.name}") - # except Exception as e: - # print(e) - # await ctx.send("An unexpected error occured") + contributorsGithub = SupabaseClient().read_all("contributors_registration") + contributorsDiscord = SupabaseClient().read_all("contributors_discord") - # async def notifs_off(self, ctx, channel: discord.TextChannel): - # try: - # SupabaseClient("discord_channels").update({"should_notify": False}, "channel_id", channel.id) - # await ctx.send(f"Notifications have been turned on for {channel.name}") - # except Exception as e: - # print(e) - # await ctx.send("An unexpected error occured") + ## Give contributor role + print(1) + contributorIds = [ + contributor["discord_id"] for contributor in contributorsGithub + ] + print(2) + contributorRole = guild.get_role(serverConfig.Roles.CONTRIBUTOR_ROLE) + print(3) + count = [0, 0, 0] + for member in guild.members: + count[0] += 1 + if member.id in contributorIds: + count[1] += 1 + if contributorRole not in member.roles: + print(contributorRole) + print(member.roles) + count[2] += 1 + print(4) + await member.add_roles(contributorRole) - # async def + print(count) + + SupabaseClient().updateContributors(guild.members) + recordedMembers = [ + contributor["discord_id"] for contributor in contributorsDiscord + ] + currentMembers = [member.id for member in guild.members] + membersWhoLeft = list(set(recordedMembers) - set(currentMembers)) + SupabaseClient().deleteContributorDiscord(membersWhoLeft) + print("Updated Contributors") async def setup(bot): diff --git a/cogs/testing.py b/cogs/testing.py deleted file mode 100644 index 54f9057..0000000 --- a/cogs/testing.py +++ /dev/null @@ -1,149 +0,0 @@ -from typing import Optional, Union - -import discord -from discord.emoji import Emoji -from discord.enums import ButtonStyle -from discord.ext import commands -from discord.partial_emoji import PartialEmoji -from discord.utils import MISSING -from utils.db import SupabaseInterface - - -async def on_button_click(interaction: discord.Interaction): - # Get the Discord username of the person who pressed the button - username = interaction.user.name - print(username) - - # Create a modal - modal = discord.ui.Modal(title="Testing Panel") - modal.add_item(discord.ui.TextInput(label="Link")) - - # Add a button to the modal that links to Google - modal.add_item( - discord.ui.Button(label="Go to Google", url="https://www.google.com") - ) - - # Add a button to the modal to close it - modal.add_item(discord.ui.Button(label="Close")) - - # Show the modal to the user - await interaction.response.send_modal(modal) - - # After the modal closes, send an ephemeral message to the user with their Discord ID - ephemeral_message = f"Your Discord ID is {interaction.user.id}" - await interaction.channel.send(ephemeral_message, ephemeral=True) - - -class Questionnaire(discord.ui.Modal, title="Questionnaire Response"): - name = discord.ui.TextInput(label="Name") - answer = discord.ui.TextInput(label="Answer", style=discord.TextStyle.paragraph) - - async def on_submit(self, interaction: discord.Interaction): - await interaction.response.send_message( - f"Thanks for your response, {self.name}!, Your answer is {self.answer}", - ephemeral=True, - ) - - -async def click(interaction: discord.Interaction): - # modal = InteractionModal() - # await interaction.response.send_message("You Pressed the Button!!!") - modal = Questionnaire() - await interaction.response.send_modal(modal) - - -class TestingPanel(discord.ui.View): - def __init__(self): - super().__init__() - self.timeout = None - - # create a textbox - self.add_item( - discord.ui.Select( - placeholder="CHOOSE", - options=[ - discord.SelectOption(label=f"{x}", value=f"{x}") - for x in [1, 2, 3, 4] - ], - ) - ) - - # Create a button - self.press_me_button = discord.ui.Button(label="Press me") - self.press_me_button.callback = click - - modal = Questionnaire() - - # Add the button to the view - self.add_item(self.press_me_button) - - # Handle the button press event - - -class InteractionModal(discord.ui.Modal): - def __init__( - self, *, title: str = ..., timeout: float | None = None, custom_id: str = ... - ) -> None: - super().__init__(title=title, timeout=timeout, custom_id=custom_id) - - self.add_item(discord.ui.TextInput(label="Enter your Name")) - self.add_item( - discord.ui.TextInput(label="Long Input", style=discord.TextStyle.long) - ) - - -class TestingModule(commands.Cog): - def __init__(self, bot): - self.bot = bot - - @commands.command() - async def newjoin(self, interaction: discord.Interaction): - button = discord.ui.Button( - style=ButtonStyle.green, label="Authenticate with Github" - ) - view = discord.ui.View() - view.add_item(button) - await interaction.response.send_message( - "Auuthenticate your Github", view=view, ephemeral=True - ) - - @commands.command() - async def arrowrow(self, ctx): - text_input_1 = discord.ui.TextInput( - label="Text 1", placeholder="Text Input 1", min_length=1, max_length=25 - ) - text_input_2 = discord.ui.TextInput( - label="Text 2", placeholder="Text Input 2", min_length=1, max_length=25 - ) - - select_input = discord.ui.Select( - placeholder="Select Input", - options=[ - discord.SelectOption(label="Option 1", value="1"), - discord.SelectOption(label="Option 2", value="2"), - discord.SelectOption(label="Option 3", value="3"), - ], - ) - - user_select_input = discord.ui.UserSelect(placeholder="User Select Input") - - button = discord.ui.Button(style=discord.ButtonStyle.primary, label="Submit") - - row = discord.ActionRow( - [text_input_1, text_input_2, select_input, user_select_input, button] - ) - - await ctx.send("Here's your arrowrow:", components=[row]) - - # Create a Discord command - @commands.command() - async def testing_panel(self, ctx): - # Create a view - view = TestingPanel() - - # Send the view to the user - await ctx.send("Testing Panel", view=view) - - -async def setup(bot): - await bot.add_cog(TestingModule(bot)) diff --git a/cogs/userInteractions.py b/cogs/userInteractions.py index 45dab26..60c4cea 100644 --- a/cogs/userInteractions.py +++ b/cogs/userInteractions.py @@ -177,7 +177,7 @@ def __init__(self, discord_userdata): class UserHandler(commands.Cog): def __init__(self, bot) -> None: self.bot = bot - self.update_contributors.start() + # self.update_contributors.start() # Executing this command sends a link to Github OAuth App via a Flask Server in the DM channel of the one executing the command # @commands.command(aliases=['join']) @@ -238,7 +238,7 @@ async def update_contributors(self): # Give Contributor Role print(member.name) await member.add_roles(contributor_role) - print("Given Roles") + print(f"Given Roles to {member.name if member else 'None'}") # add to discord engagement # SupabaseClient("discord_engagement").insert({"contributor": member.id}) diff --git a/config/server.py b/config/server.py index 15017cc..a772def 100644 --- a/config/server.py +++ b/config/server.py @@ -11,7 +11,7 @@ class Channels: @dataclass class Roles: - CONTRIBUTOR_ROLE: int = 973852365188907048 + CONTRIBUTOR_ROLE: int = 1123967402175119482 @classmethod def isCollegeChapter(roleName: str) -> bool: diff --git a/helpers/supabaseClient.py b/helpers/supabaseClient.py index d7c3f39..b8128c1 100644 --- a/helpers/supabaseClient.py +++ b/helpers/supabaseClient.py @@ -93,3 +93,29 @@ def updateContributor(self, contributor: Member): }, on_conflict="discord_id", ).execute() + + def updateContributors(self, contributors: [Member]): + table = "contributors_discord" + data = [] + for contributor in contributors: + chapters = lookForChapterRoles(contributor.roles) + gender = lookForGenderRoles(contributor.roles) + data.append( + { + "discord_id": contributor.id, + "discord_username": contributor.name, + "chapter": chapters[0] if chapters else None, + "gender": gender, + "joined_at": contributor.joined_at.isoformat(), + } + ) + + self.client.table(table).upsert( + data, + on_conflict="discord_id", + ).execute() + + def deleteContributorDiscord(self, contributorDiscordIds): + table = "contributors_discord" + for id in contributorDiscordIds: + self.client.table(table).delete().eq("discord_id", id).execute() From f2c9288e86622529847cafac6fa7a3ca16c2d990 Mon Sep 17 00:00:00 2001 From: KDwevedi Date: Mon, 29 Jan 2024 09:35:21 +0530 Subject: [PATCH 2/2] cleanup --- cogs/badges.py | 6 ++-- cogs/serverManagement.py | 18 ++++++++---- cogs/userInteractions.py | 16 ++--------- models/__init__.py | 0 models/product.py | 59 ---------------------------------------- models/project.py | 39 -------------------------- models/user.py | 33 ---------------------- 7 files changed, 17 insertions(+), 154 deletions(-) delete mode 100644 models/__init__.py delete mode 100644 models/product.py delete mode 100644 models/project.py delete mode 100644 models/user.py diff --git a/cogs/badges.py b/cogs/badges.py index e024133..665d53c 100644 --- a/cogs/badges.py +++ b/cogs/badges.py @@ -160,10 +160,10 @@ def get_user_badges(self, discord_id): github_id = contributorData[0]["github_id"] prData = { "raised": SupabaseClient().read( - table="pull_requests", query_key="raised_by", query_value=github_id + table="connected_prs", query_key="raised_by", query_value=github_id ), - "merged": SupabaseClient(table="pull_requests").read( - table="pull_requests", query_key="merged_by", query_value=github_id + "merged": SupabaseClient(table="connected_prs").read( + table="connected_prs", query_key="merged_by", query_value=github_id ), } points = 0 diff --git a/cogs/serverManagement.py b/cogs/serverManagement.py index 2f0cbcd..440dd4a 100644 --- a/cogs/serverManagement.py +++ b/cogs/serverManagement.py @@ -27,6 +27,16 @@ async def getServerData(self, ctx): # add all chapters chapterRoles = [] guild = self.bot.get_guild(serverConfig.SERVER) + ## Clear Error + oldRole = guild.get_role(973852365188907048) + print("started") + print(f"{len(oldRole.members)} have the old contributor role") + for member in oldRole.members: + print(member.joined_at) + if member.joined_at.timestamp() > datetime(2022, 12, 25).timestamp(): + await member.remove_roles(oldRole, reason="mistakenly given") + print("Member removed") + for role in guild.roles: if role.name.startswith("College:"): orgName = role.name[len("College: ") :] @@ -43,23 +53,17 @@ async def getServerData(self, ctx): contributorsDiscord = SupabaseClient().read_all("contributors_discord") ## Give contributor role - print(1) contributorIds = [ contributor["discord_id"] for contributor in contributorsGithub ] - print(2) contributorRole = guild.get_role(serverConfig.Roles.CONTRIBUTOR_ROLE) - print(3) count = [0, 0, 0] for member in guild.members: count[0] += 1 if member.id in contributorIds: count[1] += 1 if contributorRole not in member.roles: - print(contributorRole) - print(member.roles) count[2] += 1 - print(4) await member.add_roles(contributorRole) print(count) @@ -68,8 +72,10 @@ async def getServerData(self, ctx): recordedMembers = [ contributor["discord_id"] for contributor in contributorsDiscord ] + print(f"{len(recordedMembers)} have their data saved") currentMembers = [member.id for member in guild.members] membersWhoLeft = list(set(recordedMembers) - set(currentMembers)) + print(f"{len(membersWhoLeft)} members left") SupabaseClient().deleteContributorDiscord(membersWhoLeft) print("Updated Contributors") diff --git a/cogs/userInteractions.py b/cogs/userInteractions.py index 60c4cea..9b11024 100644 --- a/cogs/userInteractions.py +++ b/cogs/userInteractions.py @@ -332,18 +332,6 @@ async def github_profile(self, ctx): ```[![C4GTGithubDisplay](https://kcavhjwafgtoqkqbbqrd.supabase.co/storage/v1/object/public/c4gt-github-profile/{ctx.author.id}githubdisplay.jpg)](https://github.com/Code4GovTech) Know more about: Code For GovTech ([Website](https://www.codeforgovtech.in) | [GitHub](https://github.com/Code4GovTech/C4GT/wiki)) | [Digital Public Goods (DPGs)](https://digitalpublicgoods.net/digital-public-goods/) | [India & DPGs](https://government.economictimes.indiatimes.com/blog/digital-public-goods-digital-public-infrastructure-an-evolving-india-story/99532036)```""" ) - # githubProfileInfoEmbed.set_footer(text="Respond with 🏆 to get the link") - # message = await ctx.send(embed=githubProfileInfoEmbed) - # await message.add_reaction("🏆") - # def check(reaction, user): - # return user == ctx.message.author and str(reaction.emoji) in ['🏆'] - # try: - # reaction, user = await self.bot.wait_for('reaction_add', timeout=60.0, check=check) - # except asyncio.TimeoutError: - # await ctx.send("You took too long to respond.") - # else: - # if str(reaction.emoji) == '🏆': - # await ctx.send(f'[![C4GTGithubDisplay](https://kcavhjwafgtoqkqbbqrd.supabase.co/storage/v1/object/public/c4gt-github-profile/{ctx.author.id}githubdisplay.jpg?maxAge=10)](https://github.com/Code4GovTech)') @update_contributors.before_loop async def before_update_loop(self): @@ -406,10 +394,10 @@ async def get_points(self, ctx): print(contributor) github_id = contributor[0]["github_id"] prs_raised = SupabaseClient().read( - table="pull_requests", query_key="raised_by", query_value=github_id + table="connected_prs", query_key="raised_by", query_value=github_id ) prs_merged = SupabaseClient().read( - table="pull_requests", query_key="merged_by", query_value=github_id + table="connected_prs", query_key="merged_by", query_value=github_id ) raise_points = 0 merge_points = 0 diff --git a/models/__init__.py b/models/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/models/product.py b/models/product.py deleted file mode 100644 index e010dd8..0000000 --- a/models/product.py +++ /dev/null @@ -1,59 +0,0 @@ -from helpers.supabaseClient import SupabaseClient - - -class Product: - def __init__(self, data=None, name=None): - if name is not None: - data = SupabaseClient().read( - table="products", query_key="name", query_value=name - ) - if not data: - raise Exception("There is no product with name = ", name) - else: - # Assuming is_unique constraint on name in Products table - data = data[0] - # Name of the product - self.name = data["name"] - # Description of the product - self.desc = data["description"] - # Organisation associated with the product - self.org = data["organisation"] - # The wili page url for the product on C4GT github - self.wiki_url = data["wiki_url"] - # Projects under this product - self.projects = data["projects"] - # Mentors assigned to projects associated with this product - self.mentors = data["mentors"] if data["mentors"] else [] - # Contributors assigned to projects under this product - self.contributers = data["contributors"] if data["contributors"] else [] - # discord channel id of the dedicated discord channel for this Product - self.channel = data["channel"] - - @classmethod - def is_product(cls, product_name): - db_client = SupabaseClient() - data = db_client.read( - table="products", query_key="name", query_value=product_name - ) - if len(data) == 1: - return True - if len(data) > 1: - raise Exception( - "Product name should be unique but recieved multiple items for this name." - ) - return False - - @classmethod - def get_all_products(cls): - db_client = SupabaseClient() - data = db_client.read_all(table="products") - return data - - def assign_channel(self, discord_id): - SupabaseClient().update( - table="products", - update={"channel": discord_id}, - query_key="name", - query_value=self.name, - ) - return diff --git a/models/project.py b/models/project.py deleted file mode 100644 index eb74f6e..0000000 --- a/models/project.py +++ /dev/null @@ -1,39 +0,0 @@ -from helpers.supabaseClient import SupabaseClient - - -class Project: - def __init__(self, data=None, name=None) -> None: - if name is not None: - data = SupabaseClient().read( - table="projects", query_key="name", query_value=name - )[0] - self.name = data["name"] - self.desc = data["description"] - self.repository = data["repository"] - self.contributor = data["contributor"] - self.mentor = data["mentor"] - self.product = data["product"] - self.issue_page_url = data["issue_page_url"] - - @classmethod - def is_project(project_name): - db_client = SupabaseClient() - data = db_client.read( - table="projects", query_key="name", query_value=project_name - ) - if len(data) == 1: - return True - if len(data) > 1: - raise Exception( - "Project name should be unique but recieved multiple items for this name." - ) - return False - - @classmethod - def get_all_projects(cls): - db_client = SupabaseClient() - data = db_client.read_all(table="projects") - return data - - -# test = Project(name='test') diff --git a/models/user.py b/models/user.py deleted file mode 100644 index 77eaa02..0000000 --- a/models/user.py +++ /dev/null @@ -1,33 +0,0 @@ -from helpers.supabaseClient import SupabaseClient - - -class User: - def __init__(self, userData): - # self.name = userData["name"] - self.discordId = userData["discordId"] - self.discordUserName = userData("discordUserName") - self.githubId = userData["githubId"] - - def exists(self, table): - data = SupabaseClient().read( - table, query_key="discord_id", query_value=self.discordID - ) - if len(data.data) > 0: - return True - else: - return False - - -class Contributor(User): - def __init__(self, userData): - super().__init__(userData) - - -class Mentor(User): - def __init__(self, userData): - super().__init__(userData) - - -class OrgMember(User): - def __init__(self, userData): - super().__init__(userData)