Skip to content

Commit

Permalink
Merge pull request #55 from Code4GovTech/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
KDwevedi authored Jan 30, 2024
2 parents 27567ea + 664735d commit b4f7077
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
17 changes: 11 additions & 6 deletions cogs/listeners/role_events_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ def __init__(self, bot) -> None:
async def on_guild_role_create(self, role: discord.Role):
if role.name.startswith("College:"):
orgName = role.name[len("College: ") :]
SupabaseClient().addChapter(orgName=orgName, type="COLLEGE")
SupabaseClient().addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
elif role.name.startswith("Corporate:"):
orgName = role.name[len("Corporate: ") :]
SupabaseClient().addChapter(orgName=orgName, type="CORPORATE")
SupabaseClient().addChapter(
roleId=role.id, orgName=orgName, type="CORPORATE"
)

@commands.Cog.listener()
async def on_guild_role_delete(self, role: discord.Role):
# Role Removal is not being handled this version, but it should be defined eventually
pass
SupabaseClient().deleteChapter(roleID=role.id)

@commands.Cog.listener()
async def on_guild_role_update(self, before: discord.Role, after: discord.Role):
if after.name.startswith("College:"):
orgName = after.name[len("College:") :]
SupabaseClient().addChapter(orgName=orgName, type="COLLEGE")
SupabaseClient().addChapter(
roleId=after.id, orgName=orgName, type="COLLEGE"
)
elif after.name.startswith("Corporate:"):
orgName = after.name[len("Corporate: ") :]
SupabaseClient().addChapter(orgName=orgName, type="CORPORATE")
SupabaseClient().addChapter(
roleId=after.id, orgName=orgName, type="CORPORATE"
)


async def setup(bot: commands.Bot):
Expand Down
29 changes: 26 additions & 3 deletions cogs/serverManagement.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from discord.ext import commands
from discord.ext import commands, tasks

from config.server import ServerConfig
from helpers.supabaseClient import SupabaseClient
Expand All @@ -11,6 +11,7 @@
class ServerManagement(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
self.assign_contributor_role.start()

def validUser(self, ctx):
authorised_users = [
Expand Down Expand Up @@ -41,11 +42,15 @@ async def getServerData(self, ctx):
if role.name.startswith("College:"):
orgName = role.name[len("College: ") :]
chapterRoles.append(role)
SupabaseClient().addChapter(orgName=orgName, type="COLLEGE")
SupabaseClient().addChapter(
roleId=role.id, orgName=orgName, type="COLLEGE"
)
elif role.name.startswith("Corporate:"):
orgName = role.name[len("Corporate: ") :]
chapterRoles.append(role)
SupabaseClient().addChapter(orgName=orgName, type="CORPORATE")
SupabaseClient().addChapter(
roleId=role.id, orgName=orgName, type="CORPORATE"
)

print("added chapters")

Expand Down Expand Up @@ -79,6 +84,24 @@ async def getServerData(self, ctx):
SupabaseClient().deleteContributorDiscord(membersWhoLeft)
print("Updated Contributors")

@tasks.loop(minutes=30)
async def assign_contributor_role(self):
guild = self.bot.get_guild(serverConfig.SERVER)
contributorRole = guild.get_role(serverConfig.Roles.CONTRIBUTOR_ROLE)
contributorsGithub = SupabaseClient().read_all("contributors_registration")

contributorIds = [
contributor["discord_id"] for contributor in contributorsGithub
]

for member in guild.members:
if member.id in contributorIds and contributorRole not in member.roles:
await member.add_roles(contributorRole)

@assign_contributor_role.before_loop
async def before_assign_contributor_role(self):
await self.bot.wait_until_ready() # Wait until the bot is logged in and ready


async def setup(bot):
await bot.add_cog(ServerManagement(bot))
16 changes: 14 additions & 2 deletions helpers/supabaseClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,22 @@ def memberIsAuthenticated(self, member: Member):
else:
return False

def addChapter(self, orgName: str, type: str):
def addChapter(self, roleId: int, orgName: str, type: str):
data = (
self.client.table("chapters")
.upsert({"type": type, "org_name": orgName}, on_conflict="org_name")
.upsert(
{"discord_role_id": roleId, "type": type, "org_name": orgName},
on_conflict="discord_role_id",
)
.execute()
)
return data.data

def deleteChapter(self, roleId: int):
data = (
self.client.table("chapters")
.delete()
.eq("discord_role_id", roleId)
.execute()
)
return data.data
Expand Down

0 comments on commit b4f7077

Please sign in to comment.