From eecbe33f2c0eef61b94a7a98d663034126dc489a Mon Sep 17 00:00:00 2001 From: KDwevedi Date: Fri, 27 Oct 2023 20:01:54 +0530 Subject: [PATCH] Github Registration --- cogs/user_interactions.py | 38 +++++++++++++----------- main.py | 61 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/cogs/user_interactions.py b/cogs/user_interactions.py index ba452b6..f5fd6d4 100644 --- a/cogs/user_interactions.py +++ b/cogs/user_interactions.py @@ -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] @@ -54,9 +53,12 @@ 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 @@ -64,24 +66,26 @@ def __init__(self, discord_userdata): 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): diff --git a/main.py b/main.py index 4df3b53..bfa07f0 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,9 @@ +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 @@ -9,11 +11,64 @@ 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}') @@ -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())