Attempting to optimize cog

This commit is contained in:
unknown 2023-03-13 13:10:07 +01:00
parent c67b755075
commit 76a7c74a0c

View File

@ -18,17 +18,19 @@ class Recruitment(commands.Cog):
self.bot = bot self.bot = bot
self.message: str = '' self.message: str = ''
@commands.group(name="application", usage="[text]", invoke_without_command=True) @commands.group(name="application", usage="[text]", invoke_without_command=True)
async def application(self, ctx: commands.Context, *, _application: str = ""): async def application(self, ctx: commands.Context, *, _application: str = ""):
author = ctx.author author = ctx.author
if await self.is_direct_message(ctx):
await self.interactive_application(author)
# Check if the command was sent in a direct message to the bot
async def is_direct_message(self, ctx: commands.Context) -> bool:
if not isinstance(ctx.channel, discord.DMChannel): if not isinstance(ctx.channel, discord.DMChannel):
await ctx.send("This command can only be used in a direct message to the bot.") await ctx.send("This command can only be used in a direct message to the bot.")
return return False
return True
await self.interactive_application(author)
async def interactive_application(self, author: discord.Member): async def interactive_application(self, author: discord.Member):
"""Ask the user several questions to create an application.""" """Ask the user several questions to create an application."""
@ -39,30 +41,39 @@ class Recruitment(commands.Cog):
await author.send("You took too long to answer. Please try again later.") await author.send("You took too long to answer. Please try again later.")
return return
embed = await self.format_application(answers, author) embeddedApplication = await self.format_application(answers, author)
# Send the embed to the application channel # Call the sendApplication to check if the author is a member of the guild and send the application if they are
application_channel = self.bot.get_channel(application_channel_id) await self.sendApplication(author, embeddedApplication)
message = await application_channel.send(embed=embed)
# Add reactions to the message
reactions = ["", "", ""]
for reaction in reactions:
await message.add_reaction(reaction)
async def sendApplication(self, author: discord.Member, embeddedApplication: discord.Embed):
# Check if the author is a member of the guild # Check if the author is a member of the guild
guild = self.bot.get_guild(274657393936302080) guild = self.bot.get_guild(guild_id)
member = guild.get_member(author.id) member = guild.get_member(author.id)
if member is None: if member is None:
await author.send("You need to join the server before your application can be processed.") await author.send("You need to join the server before your application can be processed.")
return return
# Send the embed to the application channel
application_channel = self.bot.get_channel(application_channel_id)
message = await application_channel.send(embed=embeddedApplication)
# Add reactions to the message
await self.add_reactions(message)
# Assign the Trial role to the author # Assign the Trial role to the author
role = guild.get_role(531181363420987423) role = guild.get_role(531181363420987423)
await member.add_roles(role) await member.add_roles(role)
await author.send("Thank you for submitting your application! You have been given the 'Trial' role.") await author.send("Thank you for submitting your application! You have been given the 'Trial' role.")
async def add_reactions(self, message: discord.Message):
reactions = ["", "", ""]
for reaction in reactions:
await message.add_reaction(reaction)
async def format_application(self, answers: List[str], author: discord.Member) -> discord.Embed: async def format_application(self, answers: List[str], author: discord.Member) -> discord.Embed:
"""Format the application answers into an embed.""" """Format the application answers into an embed."""
embed = discord.Embed(title=f"Application from {author.display_name}", color=discord.Color.green()) embed = discord.Embed(title=f"Application from {author.display_name}", color=discord.Color.green())
@ -80,12 +91,12 @@ class Recruitment(commands.Cog):
for question in questions: for question in questions:
await author.send(question) await author.send(question)
answer = await self.get_answer(author) answer = await self.get_answers(author)
answers.append(answer.content) answers.append(answer.content)
return answers return answers
async def get_answer(self, author: discord.Member) -> discord.Message: async def get_answers(self, author: discord.Member) -> discord.Message:
"""Wait for the user to send a message.""" """Wait for the user to send a message."""
return await self.bot.wait_for( return await self.bot.wait_for(
"message", check=lambda m: m.author == author and m.guild is None "message", check=lambda m: m.author == author and m.guild is None