Skip to content

Commit

Permalink
Merge pull request #71 from Code4GovTech/feature/#69/invalidate-users
Browse files Browse the repository at this point in the history
[Feature] Mark users as inactive instead of deleting them while syncing discord to database via the bot
  • Loading branch information
KDwevedi authored Jul 19, 2024
2 parents 5949c2a + 52e5a49 commit 6be45e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 5 additions & 7 deletions cogs/serverManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ def __init__(self, bot):

def validUser(self, ctx):
authorised_users = [
1042682119035568178,
1120262151676895274,
1107555866422562926,
1107555866422562926,
599878601143222282,
] # bhavya, devaraj, navaneeth, venkatesh, sukhpreet
1059343450312544266,
] # devaraj, venkatesh, karn
return ctx.author.id in authorised_users

@commands.command(aliaes=["initiate"])
async def getServerData(self, ctx):
async def updateServerData(self, ctx):
# add all chapters
chapterRoles = []
guild = self.bot.get_guild(serverConfig.SERVER)
Expand Down Expand Up @@ -55,7 +53,7 @@ async def getServerData(self, ctx):
print("added chapters")

contributorsGithub = SupabaseClient().read_all("contributors_registration")
contributorsDiscord = SupabaseClient().read_all("contributors_discord")
contributorsDiscord = SupabaseClient().read_all_active("contributors_discord")

## Give contributor role
contributorIds = [
Expand All @@ -81,7 +79,7 @@ async def getServerData(self, ctx):
currentMembers = [member.id for member in guild.members]
membersWhoLeft = list(set(recordedMembers) - set(currentMembers))
print(f"{len(membersWhoLeft)} members left")
SupabaseClient().deleteContributorDiscord(membersWhoLeft)
SupabaseClient().invalidateContributorDiscord(membersWhoLeft)
print("Updated Contributors")

@tasks.loop(minutes=30)
Expand Down
10 changes: 10 additions & 0 deletions helpers/supabaseClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def read_by_order_limit(
def read_all(self, table):
data = self.client.table(table).select("*").execute()
return data.data

def read_all_active(self, table):
data = self.client.table(table).select("*").eq('is_active', 'true').execute()
return data.data

def update(self, table, update, query_key, query_value):
data = (
Expand Down Expand Up @@ -137,6 +141,7 @@ def updateContributors(self, contributors: [Member]):
"chapter": chapters[0] if chapters else None,
"gender": gender,
"joined_at": contributor.joined_at.isoformat(),
"is_active": 'true'
}
)

Expand All @@ -149,3 +154,8 @@ def deleteContributorDiscord(self, contributorDiscordIds):
table = "contributors_discord"
for id in contributorDiscordIds:
self.client.table(table).delete().eq("discord_id", id).execute()

def invalidateContributorDiscord(self, contributorDiscordIds):
table = "contributors_discord"
for id in contributorDiscordIds:
self.client.table(table).update({ 'is_active': 'false' }).eq('discord_id', id).execute()
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(
super().__init__(title=title, timeout=timeout, custom_id=custom_id)

async def post_data(self, data):
url = "https://kcavhjwafgtoqkqbbqrd.supabase.co/rest/v1/contributor_names"
url = os.getenv("SUPABASE_URL")
headers = {
"apikey": f"{os.getenv('SUPABASE_KEY')}",
"Authorization": f"Bearer {os.getenv('SUPABASE_KEY')}",
Expand Down

0 comments on commit 6be45e4

Please sign in to comment.