Skip to content

Commit

Permalink
Merge pull request #75 from Code4GovTech/migration_orm
Browse files Browse the repository at this point in the history
Migration orm
  • Loading branch information
sasi2312 authored Aug 21, 2024
2 parents 6be45e4 + 2908558 commit 20eee57
Show file tree
Hide file tree
Showing 12 changed files with 1,868 additions and 68 deletions.
12 changes: 6 additions & 6 deletions cogs/badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient


class BadgeModal(discord.ui.Modal, title="Your Badges"):
Expand Down Expand Up @@ -133,7 +133,7 @@ def get_user_badges(self, discord_id):
userBadges = {"points": [], "achievements": []}
if (
len(
SupabaseClient().read(
PostgresClient().read(
"contributors_registration",
query_key="discord_id",
query_value=discord_id,
Expand All @@ -143,7 +143,7 @@ def get_user_badges(self, discord_id):
):
userBadges["achievements"].append(self.discordXGithubBadge)

discordMemberData = SupabaseClient().read(
discordMemberData = PostgresClient().read(
"discord_engagement", "contributor", discord_id
)
if discordMemberData:
Expand All @@ -153,16 +153,16 @@ def get_user_badges(self, discord_id):
userBadges["achievements"].append(self.rockstarBadge)
if discordMemberData[0]["has_introduced"]:
userBadges["achievements"].append(self.apprenticeBadge)
contributorData = SupabaseClient().read(
contributorData = PostgresClient().read(
"contributors_registration", query_key="discord_id", query_value=discord_id
)
if contributorData:
github_id = contributorData[0]["github_id"]
prData = {
"raised": SupabaseClient().read(
"raised": PostgresClient().read(
table="connected_prs", query_key="raised_by", query_value=github_id
),
"merged": SupabaseClient(table="connected_prs").read(
"merged": PostgresClient(table="connected_prs").read(
table="connected_prs", query_key="merged_by", query_value=github_id
),
}
Expand Down
28 changes: 14 additions & 14 deletions cogs/discordDataScraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from discord.channel import TextChannel
from discord.ext import commands, tasks

from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient

with open("config.json") as config_file:
config_data = json.load(config_file)
Expand All @@ -28,12 +28,12 @@ def __init__(self, bot) -> None:
@commands.Cog.listener()
async def on_message(self, message):
pass
# contributor = SupabaseClient().read(
# contributor = PostgresClient().read(
# "discord_engagement", "contributor", message.author.id
# )
# print("message", len(message.content))
# if not contributor:
# SupabaseClient().insert(
# PostgresClient().insert(
# "discord_engagement",
# {
# "contributor": message.author.id,
Expand All @@ -46,13 +46,13 @@ async def on_message(self, message):
# if len(message.content) > 20:
# if message.channel.id == INTRODUCTIONS_CHANNEL_ID:
# print("intro")
# SupabaseClient().update(
# PostgresClient().update(
# "discord_engagement",
# {"has_introduced": True},
# "contributor",
# message.author.id,
# )
# SupabaseClient("discord_engagement").update(
# PostgresClient("discord_engagement").update(
# "discord_engagement",
# {"total_message_count": contributor[0]["total_message_count"] + 1},
# "contributor",
Expand All @@ -62,11 +62,11 @@ async def on_message(self, message):
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
message = reaction.message
contributor = SupabaseClient().read(
contributor = PostgresClient().read(
"discord_engagement", "contributor", message.author.id
)[0]
if not contributor:
SupabaseClient().insert(
PostgresClient().insert(
"discord_engagement",
{
"contributor": message.author.id,
Expand All @@ -77,7 +77,7 @@ async def on_reaction_add(self, reaction, user):
)
return
print("reaction")
SupabaseClient().update(
PostgresClient().update(
"discord_engagement",
{"total_reaction_count": contributor["total_reaction_count"] + 1},
"contributor",
Expand All @@ -89,7 +89,7 @@ async def add_engagement(self, ctx):
await ctx.channel.send("started")

def addEngagmentData(data):
client = SupabaseClient()
client = PostgresClient()
client.insert("discord_engagement", data)
return

Expand Down Expand Up @@ -145,7 +145,7 @@ async def enable_webhook(self, ctx):
channels = await guild.fetch_channels()
enabled = [
channel["channel_id"]
for channel in SupabaseClient().read_all("discord_channels")
for channel in PostgresClient().read_all("discord_channels")
]
for channel in channels:
try:
Expand All @@ -156,7 +156,7 @@ async def enable_webhook(self, ctx):
webhook = await channel.create_webhook(name="New Ticket Alert")
feedback = f"""URL: {webhook.url}\n Token:{"Yes" if webhook.token else "No"}"""
await ctx.send(feedback)
SupabaseClient().insert(
PostgresClient().insert(
"discord_channels",
{
"channel_id": channel.id,
Expand All @@ -177,7 +177,7 @@ async def update_applicants(self, ctx):
await ctx.send("Member List Count: " + str(len(members)))
for member in members:
try:
SupabaseClient().insert(
PostgresClient().insert(
"applicant",
{"sheet_username": member.name, "discord_id": member.id},
)
Expand Down Expand Up @@ -216,12 +216,12 @@ async def collect_all_messages(self):

async def add_messages(self):
def addMessageData(data):
client = SupabaseClient()
client = PostgresClient()
client.insert("unstructured discord data", data)
return

def getLastMessageObject(channelId):
last_message = SupabaseClient().read_by_order_limit(
last_message = PostgresClient().read_by_order_limit(
table="unstructured discord data",
query_key="channel",
query_value=channelId,
Expand Down
6 changes: 3 additions & 3 deletions cogs/listeners/member_events_cog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient


class MemberEventsListener(commands.Cog):
Expand All @@ -11,7 +11,7 @@ def __init__(self, bot) -> None:

@commands.Cog.listener("on_member_join")
async def on_member_join(self, member: discord.Member):
SupabaseClient().updateContributor(member)
PostgresClient().updateContributor(member)

@commands.Cog.listener("on_member_remove")
async def on_member_remove(self, member: discord.Member):
Expand All @@ -20,7 +20,7 @@ async def on_member_remove(self, member: discord.Member):

@commands.Cog.listener("on_member_update")
async def on_member_update(self, before: discord.Member, after: discord.Member):
SupabaseClient().updateContributor(after)
PostgresClient().updateContributor(after)


async def setup(bot: commands.Bot):
Expand Down
6 changes: 3 additions & 3 deletions cogs/listeners/message_events_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from discord.ext import commands

from config.server import ServerConfig
from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient

serverConfig = ServerConfig()
supabaseClient = SupabaseClient()
postgresClient = PostgresClient()


async def grantVerifiedRole(member: discord.Member):
Expand All @@ -32,7 +32,7 @@ def __init__(self, bot) -> None:
async def on_message(self, message: discord.Message):
# Listen for Introduction
if message.channel.id == serverConfig.Channels.INTRODUCTION_CHANNEL:
if await supabaseClient.memberIsAuthenticated(message.author):
if await postgresClient.memberIsAuthenticated(message.author):
await grantVerifiedRole(message.author)
else:
return
Expand Down
12 changes: 6 additions & 6 deletions cogs/listeners/role_events_cog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import discord
from discord.ext import commands

from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient


class RoleEventsListener(commands.Cog):
Expand All @@ -13,27 +13,27 @@ 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(roleId=role.id, orgName=orgName, type="COLLEGE")
PostgresClient().addChapter(roleId=role.id, orgName=orgName, type="COLLEGE")
elif role.name.startswith("Corporate:"):
orgName = role.name[len("Corporate: ") :]
SupabaseClient().addChapter(
PostgresClient().addChapter(
roleId=role.id, orgName=orgName, type="CORPORATE"
)

@commands.Cog.listener()
async def on_guild_role_delete(self, role: discord.Role):
SupabaseClient().deleteChapter(roleID=role.id)
PostgresClient().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(
PostgresClient().addChapter(
roleId=after.id, orgName=orgName, type="COLLEGE"
)
elif after.name.startswith("Corporate:"):
orgName = after.name[len("Corporate: ") :]
SupabaseClient().addChapter(
PostgresClient().addChapter(
roleId=after.id, orgName=orgName, type="CORPORATE"
)

Expand Down
16 changes: 8 additions & 8 deletions cogs/serverManagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from discord.ext import commands, tasks

from config.server import ServerConfig
from helpers.supabaseClient import SupabaseClient
from helpers.supabaseClient import PostgresClient

serverConfig = ServerConfig()

Expand Down Expand Up @@ -40,20 +40,20 @@ async def updateServerData(self, ctx):
if role.name.startswith("College:"):
orgName = role.name[len("College: ") :]
chapterRoles.append(role)
SupabaseClient().addChapter(
PostgresClient().addChapter(
roleId=role.id, orgName=orgName, type="COLLEGE"
)
elif role.name.startswith("Corporate:"):
orgName = role.name[len("Corporate: ") :]
chapterRoles.append(role)
SupabaseClient().addChapter(
PostgresClient().addChapter(
roleId=role.id, orgName=orgName, type="CORPORATE"
)

print("added chapters")

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

## Give contributor role
contributorIds = [
Expand All @@ -71,22 +71,22 @@ async def updateServerData(self, ctx):

print(count)

SupabaseClient().updateContributors(guild.members)
PostgresClient().updateContributors(guild.members)
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().invalidateContributorDiscord(membersWhoLeft)
PostgresClient().invalidateContributorDiscord(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")
contributorsGithub = PostgresClient().read_all("contributors_registration")

contributorIds = [
contributor["discord_id"] for contributor in contributorsGithub
Expand Down
Loading

0 comments on commit 20eee57

Please sign in to comment.