Skip to content

Commit

Permalink
Merge pull request #79 from Code4GovTech/fix/discord-role-creation
Browse files Browse the repository at this point in the history
Adding logs to tasks and authentication flows
  • Loading branch information
karntrehan authored Sep 25, 2024
2 parents b75ae05 + 7857477 commit 9414167
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 133 deletions.
62 changes: 1 addition & 61 deletions cogs/userInteractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,6 @@ def __init__(self, discord_userdata):
class UserHandler(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
# 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'])
# async def join_as_contributor(self, ctx):
# #create a direct messaging channel with the one who executed the command
# if isinstance(ctx.channel, discord.DMChannel):
# userdata = str(ctx.author.id)
# view = AuthenticationView(userdata)
# await ctx.send("Please authenticate your github account to register in the C4GT Community", view=view)
# # Command logic for DMs
# else:
# # Command logic for other channels (e.g., servers, groups)
# await ctx.send("Please use this command in Bot DMs.")
# # Command logic for DMs
# userdata = str(ctx.author.id)
# view = AuthenticationView(userdata)
# # await dmchannel.send("Please authenticate your github account to register for Code for GovTech 2023", view=view)

@commands.command(aliases=["badges"])
async def list_badges(self, ctx):
Expand All @@ -220,12 +202,9 @@ async def list_badges(self, ctx):

return

# @commands.command()
# async def give_badges(self, ctx):
# self.give_discord_badges.start()

@tasks.loop(minutes=10)
async def update_contributors(self):
print("update_contributors running")
contributors = SupabaseClient().read_all("contributors_registration")
guild = await self.bot.fetch_guild(os.getenv("SERVER_ID"))
contributor_role = guild.get_role(VERIFIED_CONTRIBUTOR_ROLE_ID)
Expand All @@ -239,45 +218,6 @@ async def update_contributors(self):
print(member.name)
await member.add_roles(contributor_role)
print(f"Given Roles to {member.name if member else 'None'}")
# add to discord engagement
# SupabaseClient("discord_engagement").insert({"contributor": member.id})

# update engagement
# for contributor in contributors:
# contributorData = SupabaseClient("discord_engagement").read("contributor", contributor["discord_id"])[0]
# member = await guild.fetch_member(contributorData["contributor"])
# print(f"-----Contributor-----{member.name}-------")
# badges = Badges(member.name)
# if contributorData:
# if contributorData["total_message_count"]>10 and not contributorData["converserBadge"]:
# SupabaseClient("discord_engagement").update({"converserBadge":True},"contributor", contributorData["contributor"])
# dmchannel = member.dm_channel if member.dm_channel else await member.create_dm()
# await dmchannel.send(embed=badges.converseBadge)
# if contributorData["total_reaction_count"]>5 and not contributorData["rockstarBadge"]:
# SupabaseClient("discord_engagement").update({"rockstarBadge":True},"contributor", contributorData["contributor"])
# dmchannel = member.dm_channel if member.dm_channel else await member.create_dm()
# await dmchannel.send(embed=badges.rockstarBadge)
# if contributorData["has_introduced"] and not contributorData["apprenticeBadge"]:
# SupabaseClient("discord_engagement").update({"apprenticeBadge":True},"contributor", contributorData["contributor"])
# dmchannel = member.dm_channel if member.dm_channel else await member.create_dm()
# await dmchannel.send(embed=badges.apprenticeBadge)
# github_id = contributor["github_id"]
# prData = {
# "raised": SupabaseClient(table="pull_requests").read(query_key="raised_by", query_value=github_id),
# "merged":SupabaseClient(table="pull_requests").read(query_key="merged_by", query_value=github_id)
# }
# points = 0
# for action in prData.keys():
# prs = prData[action]
# for pr in prs:
# points+=pr["points"]
# if len(prData["raised"])+len(prData["merged"])>0and not contributorData["enthusiastBadge"]:
# SupabaseClient("discord_engagement").update({"enthusiastBadge":True},"contributor", contributorData["contributor"])
# await dmchannel.send(embed=Badges(member.name, points=points).enthusiastBadge)
# if points>=30 and not contributorData["risingStarBadge"]:
# SupabaseClient("discord_engagement").update({"risingStarBadge":True},"contributor", contributorData["contributor"])
# await dmchannel.send(embed=badges.risingStarBadge)

return

@commands.command()
Expand Down
77 changes: 5 additions & 72 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
dotenv.load_dotenv(".env")


# class GithubAuthModal(discord.ui.Modal):
# def __init__(self, *,userID, title: str = None, timeout: float | None = None, custom_id: str = None) -> None:
# super().__init__(title=title, timeout=timeout, custom_id=custom_id)
# self.add_item(discord.ui.Button(label='Authenticate Github', style=discord.ButtonStyle.url, url=f'https://github-app.c4gt.samagra.io/authenticate/{userID}'))
class AuthenticationView(discord.ui.View):
def __init__(self, discord_userdata):
super().__init__()
Expand All @@ -35,72 +31,6 @@ def __init__(self, discord_userdata):
self.message = None


# class ChapterSelect(discord.ui.Select):
# def __init__(self, affiliation, data):
# collegeOptions = [discord.SelectOption(label=option["label"], emoji=option["emoji"] ) for option in [
# {
# "label": "NIT Kurukshetra",
# "emoji": "\N{GRADUATION CAP}"
# },
# {
# "label": "ITER, Siksha 'O' Anusandhan",
# "emoji": "\N{GRADUATION CAP}"
# },
# {
# "label": "IIITDM Jabalpur",
# "emoji": "\N{GRADUATION CAP}"
# },
# {
# "label": "KIIT, Bhubaneswar",
# "emoji": "\N{GRADUATION CAP}"
# }

# ]]
# corporateOptions = []
# self.data = data
# super().__init__(placeholder="Please select your institute",max_values=1,min_values=1,options=collegeOptions if affiliation=="College Chapter" else corporateOptions)
# async def callback(self, interaction:discord.Interaction):
# self.data["chapter"] = self.values[0]
# self.data["discord_id"]= interaction.user.id

# await interaction.response.send_message("Now please Authenticate using Github so we can start awarding your points!",view=AuthenticationView(interaction.user.id), ephemeral=True)

# class AffiliationSelect(discord.ui.Select):
# def __init__(self, data):
# options = [discord.SelectOption(label=option["label"], emoji=option["emoji"] ) for option in [
# {
# "label": "College Chapter",
# "emoji": "\N{OPEN BOOK}"
# },
# {
# "label": "Corporate Chapter",
# "emoji": "\N{OFFICE BUILDING}"
# },
# {
# "label": "Individual Contributor",
# "emoji": "\N{BRIEFCASE}"
# }
# ]]
# super().__init__(placeholder="Please select applicable affliliation",max_values=1,min_values=1,options=options)
# self.data = data
# async def callback(self, interaction:discord.Interaction):
# self.data["affiliation"] = self.values[0]
# if self.values[0] == "College Chapter":
# chapterView = discord.ui.View()
# chapterView.add_item(ChapterSelect(self.values[0], self.data))
# await interaction.response.send_message("Please select your institute!", view=chapterView, ephemeral=True)
# elif self.values[0] == "Corporate Chapter":
# await interaction.response.send_message("We currently don't have any active Corporate Chapters!", ephemeral=True)
# elif self.values[0] == "Individual Contributor":
# await interaction.response.send_message("Now please Authenticate using Github so we can start awarding your points!",view=AuthenticationView(interaction.user.id), ephemeral=True)

# class AffiliationView(discord.ui.View):
# def __init__(self, data):
# super().__init__()
# self.timeout = None
# self.add_item(AffiliationSelect(data))


class RegistrationModal(discord.ui.Modal):
def __init__(
self,
Expand Down Expand Up @@ -160,22 +90,25 @@ async def on_submit(self, interaction: discord.Interaction):
if verifiedContributorRoleID in [role.id for role in user.roles]:
return
else:

async def hasIntroduced():
print("Checking...")
print("Checking hasIntroduced...")
authentication = SupabaseClient().read(
"contributors_registration", "discord_id", user.id
)
print("Authentication: "+authentication)
while not authentication:
print("Not authenticated")
await asyncio.sleep(30)
print("Found!")
discordEngagement = SupabaseClient().read(
"discord_engagement", "contributor", user.id
)[0]
print("Discord engagement: "+discordEngagement)
return discordEngagement["has_introduced"]

try:
await asyncio.wait_for(hasIntroduced(), timeout=1000)
print("Timedout on hasIntroduced")
verifiedContributorRole = user.guild.get_role(verifiedContributorRoleID)
if verifiedContributorRole:
if verifiedContributorRole not in user.roles:
Expand Down

0 comments on commit 9414167

Please sign in to comment.