Skip to content

Commit

Permalink
Github Registration
Browse files Browse the repository at this point in the history
  • Loading branch information
KDwevedi committed Oct 27, 2023
1 parent f21c3bb commit eecbe33
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
38 changes: 21 additions & 17 deletions cogs/user_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from discord.ext import commands, tasks
import time, csv
from utils.db import SupabaseInterface
from utils.api import GithubAPI

VERIFIED_CONTRIBUTOR_ROLE_ID = 1123967402175119482
NON_CONTRIBUTOR_ROLES = [973852321870118914, 976345770477387788, 973852439054782464]
Expand Down Expand Up @@ -54,34 +53,39 @@ async def create_embed(self):

#This is a Discord View that is a set of UI elements that can be sent together in a message in discord.
#This view send a link to Github Auth through c4gt flask app in the form of a button.
class RegistrationModal(discord.ui.Modal, title="Contributor Registration"):
name = discord.ui.TextInput(label='Name')
class AuthenticationView(discord.ui.View):
def __init__(self, discord_userdata):
super().__init__()
self.timeout = None
button = discord.ui.Button(label='Authenticate Github', style=discord.ButtonStyle.url, url=f'https://github-app.c4gt.samagra.io/authenticate/{discord_userdata}')
self.add_item(button)
self.message = None

class UserHandler(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
self.update_contributors.start()
# self.update_contributors.start()



#Executing this command sends a link to Github OAuth App via a Flask Server in the DM channel of the one executing the command
@commands.command(aliases=['join'])
async def join_as_contributor(self, ctx):
#create a direct messaging channel with the one who executed the command
if isinstance(ctx.channel, discord.DMChannel):
userdata = str(ctx.author.id)
view = AuthenticationView(userdata)
await ctx.send("Please authenticate your github account to register in the C4GT Community", view=view)
# Command logic for DMs
else:
# Command logic for other channels (e.g., servers, groups)
await ctx.send("Please use this command in Bot DMs.")
# Command logic for DMs
userdata = str(ctx.author.id)
view = AuthenticationView(userdata)
# await dmchannel.send("Please authenticate your github account to register for Code for GovTech 2023", view=view)
# @commands.command(aliases=['join'])
# async def join_as_contributor(self, ctx):
# #create a direct messaging channel with the one who executed the command
# if isinstance(ctx.channel, discord.DMChannel):
# userdata = str(ctx.author.id)
# view = AuthenticationView(userdata)
# await ctx.send("Please authenticate your github account to register in the C4GT Community", view=view)
# # Command logic for DMs
# else:
# # Command logic for other channels (e.g., servers, groups)
# await ctx.send("Please use this command in Bot DMs.")
# # Command logic for DMs
# userdata = str(ctx.author.id)
# view = AuthenticationView(userdata)
# # await dmchannel.send("Please authenticate your github account to register for Code for GovTech 2023", view=view)

@commands.command(aliases=["badges"])
async def list_badges(self, ctx):
Expand Down
61 changes: 58 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
from typing import Optional
import discord
from discord.ext import commands
import os, sys
import asyncio
from discord.utils import MISSING
import dotenv

#Since there are user defined packages, adding current directory to python path
current_directory = os.getcwd()
sys.path.append(current_directory)

dotenv.load_dotenv(".env")
intents = discord.Intents.all()

client = commands.Bot(command_prefix='!', intents=intents)
# class GithubAuthModal(discord.ui.Modal):
# def __init__(self, *,userID, title: str = None, timeout: float | None = None, custom_id: str = None) -> None:
# super().__init__(title=title, timeout=timeout, custom_id=custom_id)
# self.add_item(discord.ui.Button(label='Authenticate Github', style=discord.ButtonStyle.url, url=f'https://github-app.c4gt.samagra.io/authenticate/{userID}'))

class AuthenticationView(discord.ui.View):
def __init__(self, discord_userdata):
super().__init__()
self.timeout = None
button = discord.ui.Button(label='Authenticate Github', style=discord.ButtonStyle.url, url=f'https://github-app.c4gt.samagra.io/authenticate/{discord_userdata}')
self.add_item(button)
self.message = None

class RegistrationModal(discord.ui.Modal):
def __init__(self, *, title: str = None, timeout: float | None = None, custom_id: str = None) -> None:
super().__init__(title=title, timeout=timeout, custom_id=custom_id)
name = discord.ui.TextInput(label='Please Enter Your Name')

async def on_submit(self, interaction: discord.Interaction):
await interaction.response.send_message("Thanks! Now register your Github",view=AuthenticationView(interaction.user.id), ephemeral=True)

class RegistrationView(discord.ui.View):
def __init__(self):
super().__init__(timeout = None)

@discord.ui.button(label="Register", style=discord.enums.ButtonStyle.blurple, custom_id='registration_view:blurple')
async def reg(self, interaction: discord.Interaction, button: discord.ui.Button):
modal = RegistrationModal(title="Contributor Registration", custom_id="registration:modal")
await interaction.response.send_modal(modal)

class C4GTBot(commands.Bot):
def __init__(self):
intents = discord.Intents.all()
intents.message_content = True

super().__init__(command_prefix=commands.when_mentioned_or('!'), intents=intents)

async def setup_hook(self) -> None:
# Register the persistent view for listening here.
# Note that this does not send the view to any message.
# In order to do this you need to first send a message with the View, which is shown below.
# If you have the message_id you can also pass it as a keyword argument, but for this example
# we don't have one.
self.add_view(RegistrationView())

client = C4GTBot()

@client.command(aliases=['registration'])
async def registerAsContributor(ctx, channel: discord.TextChannel):
# guild = ctx.guild
# channelID = 1167054801385820240
# channel = guild.get_channel_or_thread(channelID)
await channel.send("Please register using Github to sign up as a C4GT Contributor", view=RegistrationView())


#alert message on commandline that bot has successfully logged in

@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
Expand All @@ -27,7 +82,7 @@ async def load():
async def main():
async with client:
await load()
await client.start(os.getenv("TOKEN"))
await client.start(os.getenv("TESTING_TOKEN"))


asyncio.run(main())
Expand Down

0 comments on commit eecbe33

Please sign in to comment.