Skip to content

Commit ebacaab

Browse files
committed
remove edgedb
1 parent 029b5f0 commit ebacaab

File tree

10 files changed

+98
-153
lines changed

10 files changed

+98
-153
lines changed

Dockerfile.bot

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ WORKDIR /app
66
COPY requirements.txt requirements.txt
77
RUN pip install -r requirements.txt
88

9-
# DB
10-
COPY db.py db.py
11-
ADD dbschema dbschema
12-
COPY edgedb.toml edgedb.toml
13-
149
# Bot
10+
COPY db.py db.py
1511
COPY main.py main.py
1612
COPY gh.py gh.py
1713
COPY web.py web.py

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
A Discord Bot that allows users to verify their GitHub Sponsors status and get access to sponsor-only roles/channels. It can also detect contributors to a GitHub repository and assign them a contributor role.
44

5-
It works by prompting the user in a private thread to connect their GitHub account in Discord connection settings (even if it's not publicly visible on their Discord profile). The bot will then check if the user is a sponsor of the specified GitHub user and if so, assign the appropriate roles. It then stores this information in an EdgeDB database.
5+
It works by prompting the user in a private thread to connect their GitHub account in Discord connection settings (even if it's not publicly visible on their Discord profile). The bot will then check if the user is a sponsor of the specified GitHub user and if so, assign the appropriate roles. It then stores this information in an PostgreSQL database.

config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ def get_config():
2222
CLIENT_SECRET = CONFIG["DISCORD_BOT"]["CLIENT_SECRET"]
2323
# Discord Roles
2424
DISCORD_ROLES_LIST = CONFIG["DISCORD_ROLES"]
25+
# DB
26+
DB_HOST = CONFIG["DB"]["DB_HOST"]
27+
DB_PORT = CONFIG["DB"]["DB_PORT"]
28+
DB_USER = CONFIG["DB"]["DB_USER"]
29+
DB_PASS = CONFIG["DB"]["DB_PASS"]
30+
DB_NAME = CONFIG["DB"]["DB_NAME"]

config.yaml

110 Bytes
Binary file not shown.

db.py

Lines changed: 58 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,96 @@
1-
import edgedb
1+
import psycopg2
2+
from config import DB_HOST, DB_PORT, DB_USER, DB_PASS, DB_NAME
23

3-
class EdgeDB:
4+
class PostgresDB:
45
def __init__(self):
5-
self.client = edgedb.create_client()
6+
self.conn = psycopg2.connect(
7+
dbname=DB_NAME,
8+
host=DB_HOST,
9+
port=DB_PORT,
10+
user=DB_USER,
11+
password=DB_PASS,
12+
)
13+
self.cursor = self.conn.cursor()
614

715
def create_sponsor(self, gh_id, gh_username, discord_id=0, discord_name='', discord_code='', contributed_to_repos=None, is_currently_sponsoring=False):
816
if self.get_sponsor_by_gh_id(gh_id):
917
return
1018
if contributed_to_repos is None:
1119
contributed_to_repos = []
12-
self.client.execute('''
13-
INSERT Sponsor {
14-
gh_id := <int32>$gh_id,
15-
discord_id := <int64>$discord_id,
16-
gh_username := <str>$gh_username,
17-
discord_name := <str>$discord_name,
18-
discord_code := <str>$discord_code,
19-
contributed_to_repos := <array<str>>$contributed_to_repos,
20-
is_currently_sponsoring := <bool>$is_currently_sponsoring
21-
};
22-
''', gh_id=int(gh_id), discord_id=int(discord_id), gh_username=gh_username, discord_name=discord_name, discord_code=discord_code, contributed_to_repos=contributed_to_repos, is_currently_sponsoring=is_currently_sponsoring)
20+
self.cursor.execute('''
21+
INSERT INTO Sponsor (
22+
gh_id, discord_id, gh_username, discord_name, discord_code,
23+
contributed_to_repos, is_currently_sponsoring
24+
) VALUES (%s, %s, %s, %s, %s, %s, %s)
25+
''', (gh_id, discord_id, gh_username, discord_name, discord_code, contributed_to_repos, is_currently_sponsoring))
26+
self.conn.commit()
2327

2428
def get_sponsor_by_gh_id(self, gh_id):
25-
return self.client.query_single('''
26-
SELECT DISTINCT Sponsor {
27-
gh_id,
28-
gh_username,
29-
gh_url,
30-
discord_name,
31-
discord_id,
32-
discord_code,
33-
contributed_to_repos,
34-
is_contributor,
35-
is_currently_sponsoring
36-
}
37-
FILTER .gh_id = <int32>$gh_id
38-
LIMIT 1;
39-
''', gh_id=int(gh_id))
29+
self.cursor.execute('''
30+
SELECT * FROM Sponsor WHERE gh_id = %s LIMIT 1
31+
''', (gh_id,))
32+
return self.cursor.fetchone()
4033

4134
def get_sponsor_by_gh_username(self, gh_username):
42-
return self.client.query_single('''
43-
SELECT Sponsor {
44-
gh_id,
45-
gh_username,
46-
gh_url,
47-
discord_name,
48-
discord_id,
49-
discord_code,
50-
contributed_to_repos,
51-
is_contributor,
52-
is_currently_sponsoring
53-
}
54-
FILTER str_lower(.gh_username) = str_lower(<str>$gh_username)
55-
LIMIT 1;
56-
''', gh_username=gh_username)
35+
self.cursor.execute('''
36+
SELECT * FROM Sponsor WHERE LOWER(gh_username) = LOWER(%s) LIMIT 1
37+
''', (gh_username,))
38+
return self.cursor.fetchone()
5739

5840
def get_sponsor_by_discord_id(self, discord_id):
59-
return self.client.query_single('''
60-
SELECT Sponsor {
61-
gh_id,
62-
gh_username,
63-
gh_url,
64-
is_contributor,
65-
contributed_to_repos,
66-
discord_name,
67-
discord_id,
68-
discord_code,
69-
is_currently_sponsoring
70-
}
71-
FILTER .discord_id = <int64>$discord_id
72-
LIMIT 1;
73-
''', discord_id=int(discord_id))
74-
41+
self.cursor.execute('''
42+
SELECT * FROM Sponsor WHERE discord_id = %s LIMIT 1
43+
''', (discord_id,))
44+
return self.cursor.fetchone()
45+
7546
def update_sponsor_gh_username(self, gh_id, gh_username):
76-
sponsor = self.get_sponsor_by_gh_id(gh_id)
77-
if not sponsor:
47+
if not self.get_sponsor_by_gh_id(gh_id):
7848
return
79-
self.client.execute('''
80-
UPDATE Sponsor
81-
FILTER .gh_id = <int32>$gh_id
82-
SET {
83-
gh_username := <str>$gh_username
84-
};
85-
''', gh_id=int(gh_id), gh_username=gh_username)
49+
self.cursor.execute('''
50+
UPDATE Sponsor SET gh_username = %s WHERE gh_id = %s
51+
''', (gh_username, gh_id))
52+
self.conn.commit()
8653

8754
def update_sponsor_discord_id(self, gh_id, discord_id):
8855
if not self.get_sponsor_by_gh_id(gh_id):
8956
return
90-
self.client.execute('''
91-
UPDATE Sponsor
92-
FILTER .gh_id = <int32>$gh_id
93-
SET {
94-
discord_id := <int64>$discord_id
95-
};
96-
''', gh_id=int(gh_id), discord_id=int(discord_id))
57+
self.cursor.execute('''
58+
UPDATE Sponsor SET discord_id = %s WHERE gh_id = %s
59+
''', (discord_id, gh_id))
60+
self.conn.commit()
9761

9862
def update_sponsor_discord_name(self, gh_id, discord_name):
9963
if not self.get_sponsor_by_gh_id(gh_id):
10064
return
101-
self.client.execute('''
102-
UPDATE Sponsor
103-
FILTER .gh_id = <int32>$gh_id
104-
SET {
105-
discord_name := <str>$discord_name
106-
};
107-
''', gh_id=int(gh_id), discord_name=discord_name)
65+
self.cursor.execute('''
66+
UPDATE Sponsor SET discord_name = %s WHERE gh_id = %s
67+
''', (discord_name, gh_id))
68+
self.conn.commit()
10869

10970
def update_sponsor_discord_code(self, gh_id, discord_code):
11071
if not self.get_sponsor_by_gh_id(gh_id):
11172
return
112-
self.client.execute('''
113-
UPDATE Sponsor
114-
FILTER .gh_id = <int32>$gh_id
115-
SET {
116-
discord_code := <str>$discord_code
117-
};
118-
''', gh_id=int(gh_id), discord_code=discord_code)
73+
self.cursor.execute('''
74+
UPDATE Sponsor SET discord_code = %s WHERE gh_id = %s
75+
''', (discord_code, gh_id))
76+
self.conn.commit()
11977

12078
def update_sponsor_contributed_to_repos(self, gh_id, contributed_to_repos):
12179
if not self.get_sponsor_by_gh_id(gh_id):
12280
return
123-
self.client.execute('''
124-
UPDATE Sponsor
125-
FILTER .gh_id = <int32>$gh_id
126-
SET {
127-
contributed_to_repos := <array<str>>$contributed_to_repos
128-
};
129-
''', gh_id=int(gh_id), contributed_to_repos=contributed_to_repos)
81+
self.cursor.execute('''
82+
UPDATE Sponsor SET contributed_to_repos = %s WHERE gh_id = %s
83+
''', (contributed_to_repos, gh_id))
84+
self.conn.commit()
13085

13186
def update_sponsor_is_currently_sponsoring(self, gh_id, is_currently_sponsoring):
13287
if not self.get_sponsor_by_gh_id(gh_id):
13388
return
134-
self.client.execute('''
135-
UPDATE Sponsor
136-
FILTER .gh_id = <int32>$gh_id
137-
SET {
138-
is_currently_sponsoring := <bool>$is_currently_sponsoring
139-
};
140-
''', gh_id=int(gh_id), is_currently_sponsoring=is_currently_sponsoring)
89+
self.cursor.execute('''
90+
UPDATE Sponsor SET is_currently_sponsoring = %s WHERE gh_id = %s
91+
''', (is_currently_sponsoring, gh_id))
92+
self.conn.commit()
14193

14294
def get_sponsors(self):
143-
return self.client.query('''
144-
SELECT Sponsor {
145-
gh_id,
146-
gh_username,
147-
gh_url,
148-
is_contributor,
149-
contributed_to_repos,
150-
discord_name,
151-
discord_id,
152-
discord_code,
153-
is_currently_sponsoring
154-
};
155-
''')
95+
self.cursor.execute('SELECT * FROM Sponsor')
96+
return self.cursor.fetchall()

docker-compose.yml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
services:
22
web:
33
image: ghcr.io/nelsondane/sponsors-bot-web:latest
4+
# build:
5+
# context: .
6+
# dockerfile: Dockerfile.web
47
container_name: web
58
restart: always
69
volumes:
710
- ./config.yaml:/app/config.yaml
8-
environment:
9-
- EDGEDB_DSN=edgedb://edgedb:5656?tls_security=insecure
1011
ports:
1112
- 8080:8080
1213
depends_on:
13-
- edgedb
14+
- db
15+
- bot
1416

1517
cloudflare:
1618
image: cloudflare/cloudflared:latest
@@ -20,28 +22,29 @@ services:
2022

2123
bot:
2224
image: ghcr.io/nelsondane/sponsors-bot:latest
25+
# build:
26+
# context: .
27+
# dockerfile: Dockerfile.bot
28+
# platform: linux/amd64
2329
container_name: bot
2430
restart: always
2531
volumes:
2632
- ./config.yaml:/app/config.yaml
27-
environment:
28-
- EDGEDB_DSN=edgedb://edgedb:5656?tls_security=insecure
2933
depends_on:
30-
- edgedb
34+
- db
3135

32-
edgedb:
33-
image: edgedb/edgedb:latest
34-
container_name: edgedb
36+
db:
37+
image: postgres:17
38+
container_name: db
3539
restart: always
3640
environment:
37-
- EDGEDB_SERVER_SECURITY=insecure_dev_mode
38-
- EDGEDB_SERVER_ADMIN_UI=enabled
39-
- EDGEDB_SERVER_BIND_ADDRESS=0.0.0.0
41+
- POSTGRES_USER=${POSTGRES_USER}
42+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
43+
- POSTGRES_DB=sponsors
4044
volumes:
41-
- ./config/edgedb:/var/lib/edgedb/data
42-
- ./dbschema:/dbschema
45+
- ./db:/var/lib/postgresql/data
4346
ports:
44-
- 5656:5656
47+
- 5432:5432
4548

4649
watchtower:
4750
image: containrrr/watchtower
@@ -52,7 +55,6 @@ services:
5255
- WATCHTOWER_INCLUDE_STOPPED=true
5356
- WATCHTOWER_POLL_INTERVAL=3600
5457
command:
55-
- edgedb
5658
- bot
5759
- web
5860
volumes:

gh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import requests
2-
from db import EdgeDB
2+
from db import PostgresDB
33
from config import GH_TOKEN, GH_SPONSORS_TIER_ID, GH_REPOS
44

55
def get_sponsors():
@@ -28,7 +28,7 @@ def get_sponsors():
2828
raise Exception(f"Query failed to run by returning code {request.status_code}. {request.text}")
2929
return request.json()["data"]["viewer"]["sponsors"]["edges"]
3030

31-
def update_sponsors(db: EdgeDB):
31+
def update_sponsors(db: PostgresDB):
3232
users = db.get_sponsors()
3333
gh_sponsors = get_sponsors()
3434
for user in users:
@@ -68,7 +68,7 @@ def get_contributors():
6868
contributors += users
6969
return users
7070

71-
def update_contributors(db: EdgeDB):
71+
def update_contributors(db: PostgresDB):
7272
contributors = get_contributors()
7373
for contributor in contributors:
7474
if db.get_sponsor_by_gh_id(contributor["id"]):

main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from config import GUILD_ID, GH_SPONSORS_ROLE_ID, BOT_TOKEN, GH_REPOS, GH_SPONSORS_URL, DISCORD_ROLES_LIST
66
import emoji
77

8-
from db import EdgeDB
8+
from db import PostgresDB
99
from gh import update_sponsors, update_contributors
1010
from web import generate_uri
1111

1212
EMOJIS = emoji.EMOJI_DATA
1313

1414
def update_db():
15-
db = EdgeDB()
15+
db = PostgresDB()
1616
update_sponsors(db)
1717
update_contributors(db)
1818

@@ -140,7 +140,7 @@ async def give_old_reaction_roles():
140140

141141
async def update_sponsors_and_contributors():
142142
# If Sponsor or Contributor status changes in DB, update roles
143-
db = EdgeDB()
143+
db = PostgresDB()
144144
users = [member async for member in client.get_guild(GUILD_ID).fetch_members()]
145145
for user in users:
146146
sponsor_remove = True
@@ -220,7 +220,7 @@ async def whois_command(interaction: discord.Interaction, discord_user: discord.
220220
return
221221
await interaction.response.send_message("Searching for user...", ephemeral=True)
222222
print(f"{interaction.user.display_name} searched for Discord user: {discord_user.display_name}")
223-
db = EdgeDB()
223+
db = PostgresDB()
224224
user = db.get_sponsor_by_discord_id(discord_user.id)
225225
if user:
226226
# User found
@@ -241,7 +241,7 @@ async def ishere_command(interaction: discord.Interaction, gh_username: str):
241241
return
242242
await interaction.response.send_message("Searching for user...", ephemeral=True)
243243
print(f"{interaction.user.display_name} searched for GitHub user: {gh_username}")
244-
db = EdgeDB()
244+
db = PostgresDB()
245245
user = db.get_sponsor_by_gh_username(gh_username)
246246
if user and user.discord_id:
247247
# User found
@@ -263,7 +263,7 @@ async def verify_command(interaction: discord.Interaction):
263263
return
264264
await interaction.response.send_message("Starting the verification...", ephemeral=True)
265265
update_db()
266-
db = EdgeDB()
266+
db = PostgresDB()
267267
user = db.get_sponsor_by_discord_id(interaction.user.id)
268268
discord_display_name = interaction.user.display_name
269269
if user:

0 commit comments

Comments
 (0)