Skip to content

Commit

Permalink
feat: 💫
Browse files Browse the repository at this point in the history
  • Loading branch information
fuegovic committed Oct 29, 2023
1 parent 3bdda89 commit 4a9f741
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
42 changes: 17 additions & 25 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@

load_dotenv()

TOKEN = os.getenv('TOKEN')
REPO = os.getenv('GITHUB_REPO')
OWNER = os.getenv('REPO_OWNER')
ROLE = os.getenv('ROLE_ID')
GTOKEN = os.getenv('GITHUB_TOKEN')
CLIENT_ID = os.getenv('CLIENT_ID')
GITHUB_CLIENT_ID = os.getenv('GITHUB_CLIENT_ID')
DOMAIN = os.getenv('DOMAIN')
token = os.getenv('TOKEN')
repo = os.getenv('GITHUB_REPO')
owner = os.getenv('REPO_OWNER')
role = os.getenv('ROLE_ID')
client_id = os.getenv('CLIENT_ID')
domain = os.getenv('DOMAIN')
CLIENT = None
DB = None

Expand All @@ -54,7 +52,7 @@

client = Client(
intents=Intents.ALL,
token=TOKEN,
token=token,
sync_interactions=True,
asyncio_debug=False,
logger=cls_log,
Expand All @@ -66,7 +64,7 @@
async def on_startup():
print(f'{client.user} connected to discord')
print('----------------------------------------------------------------------------------------------------------------')
print(f'Bot invite link: https://discord.com/api/oauth2/authorize?client_id={CLIENT_ID}&permissions=268453888&scope=bot')
print(f'Bot invite link: https://discord.com/api/oauth2/authorize?client_id={client_id}&permissions=268453888&scope=bot')
print('----------------------------------------------------------------------------------------------------------------')


Expand Down Expand Up @@ -157,12 +155,12 @@ async def github(ctx: SlashContext):
Button(
style=ButtonStyle.URL,
label="⚠️ new issue",
url=f"https://github.com/{OWNER}/{REPO}/issues/new",
url=f"https://github.com/{owner}/{repo}/issues/new",
),
Button(
style=ButtonStyle.URL,
label="💬 new discussion",
url=f"https://github.com/{OWNER}/{REPO}/discussions/new/choose",
url=f"https://github.com/{owner}/{repo}/discussions/new/choose",
)
)
]
Expand All @@ -175,38 +173,32 @@ async def start_callback(ctx: ComponentContext):
user = ctx.author
userid = ctx.author_id

oauth_url = f"{DOMAIN}/login?id={userid}&name={user}"
oauth_url = f"{domain}/login?id={userid}&name={user}"

await ctx.send(
content="click this button to connect your GitHub account:",
components=[ActionRow(Button(style=ButtonStyle.LINK, label="Authorize", url=oauth_url))],
ephemeral=True
)

# Start time
start_time = datetime.now()

# Loop for 1 minute
while datetime.now() < start_time + timedelta(minutes=1):
# Check MongoDB for entry with Discord ID
user_collection = DB['users']
user_entry = user_collection.find_one({'discord_id': f'{userid}'})
print(f"tst: {userid}")
user_entry = user_collection.find_one({'discord_id': f'{userid}', 'linked_repo': f"https://github.com/{owner}/{repo}/"})

if user_entry:
# Check if 'starred_repo' is set to true and assign role
if user_entry['starred_repo']:
role = os.getenv('ROLE_ID')
await ctx.author.add_role(role, reason='auth')
await ctx.send("role activated")
if user_entry.get('starred_repo', False):
await ctx.author.add_role(role, reason='star')
await ctx.send("Confirmation: Your account has been successfully linked!", ephemeral=True)
else:
await ctx.author.remove_role(role, reason='no_star')
await ctx.send("Your account has been unlinked!", ephemeral=True)
break

# If entry not found, wait for a short period before checking again
await asyncio.sleep(5)

else:
# If loop completes (1 minute passes) without finding entry, send try again message
await ctx.send("Could not find your account. Please try again.", ephemeral=True)

client.start()
40 changes: 21 additions & 19 deletions server/github_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,16 @@ def authorize():
print(f"User's email: {email}")
print(f"Token: {token}")

repo_owner = os.getenv('REPO_OWNER')
github_repo = os.getenv('GITHUB_REPO')
owner = os.getenv('REPO_OWNER')
repo = os.getenv('GITHUB_REPO')
url = f"https://github.com/{owner}/{repo}/"

# Make a GET request to the "Check if a repository is starred" endpoint
starred_resp = github.get(f'user/starred/{repo_owner}/{github_repo}', token=token)
starred_resp = github.get(f'user/starred/{owner}/{repo}', token=token)
print(f"starred response = {starred_resp}")

# If the response status code is 204, the repository is starred
if starred_resp.status_code == 204:
starred_repo = True
else:
starred_repo = False

# Save user information to MongoDB
user_collection = DB['users']

# Check if a document with the same email exists
existing_user = user_collection.find_one({'github_email': email})
starred_repo = starred_resp.status_code == 204

# Get the current timestamp in UTC
current_time = datetime.now(timezone.utc).isoformat()
Expand All @@ -102,17 +94,13 @@ def authorize():
'discord_id': discord_id,
'github_username': username,
'github_email': email,
'linked_repo': url,
'starred_repo': starred_repo,
'github_token': token,
'updated_at': current_time,
}

if existing_user:
# Update the existing document
user_collection.update_one({'github_email': email}, {'$set': user_data})
else:
# Insert a new document
user_collection.insert_one(user_data)
save_user_data_to_db(user_data)

else:
# Auth failed
Expand All @@ -125,6 +113,20 @@ def authorize():

return render_template('result.html', message=message, user_data=user_data)

def save_user_data_to_db(user_data):
# Save user information to MongoDB
user_collection = DB['users']

# Check if a document with the same email exists
existing_user = user_collection.find_one({'github_email': user_data['github_email']})

if existing_user:
# Update the existing document
user_collection.update_one({'github_email': user_data['github_email']}, {'$set': user_data})
else:
# Insert a new document
user_collection.insert_one(user_data)

@app.route('/')
def home():
return render_template('home.html')
Expand Down

0 comments on commit 4a9f741

Please sign in to comment.