KaniumCogs/recruitmentCog/recruitment.py

110 lines
4.8 KiB
Python
Raw Normal View History

import asyncio
2023-03-12 22:55:57 +01:00
from typing import List
import discord
2023-03-12 22:58:29 +01:00
from redbot.core import Config, checks, commands
from redbot.core.utils import AsyncIter
2023-03-12 22:58:29 +01:00
from redbot.core.utils.chat_formatting import pagify, box
from redbot.core.utils.antispam import AntiSpam
from redbot.core.bot import Red
from redbot.core.utils.predicates import MessagePredicate
2023-03-13 01:16:41 +01:00
guild_id = 274657393936302080
2023-03-12 22:58:29 +01:00
application_channel_id = 1023172488143839252
2023-03-12 21:41:08 +01:00
class Recruitment(commands.Cog):
2023-03-12 22:58:29 +01:00
"""A cog that lets a user send a membership application."""
2023-03-12 21:41:08 +01:00
2023-03-12 22:55:57 +01:00
def __init__(self, bot: Red):
self.bot = bot
2023-03-12 23:55:46 +01:00
self.message: str = ''
2023-03-12 22:58:29 +01:00
2023-03-13 13:10:07 +01:00
2023-03-12 23:55:46 +01:00
@commands.group(name="application", usage="[text]", invoke_without_command=True)
async def application(self, ctx: commands.Context, *, _application: str = ""):
author = ctx.author
2023-03-13 13:10:07 +01:00
if await self.is_direct_message(ctx):
await self.interactive_application(author)
2023-03-13 13:10:07 +01:00
async def is_direct_message(self, ctx: commands.Context) -> bool:
2023-03-12 21:41:08 +01:00
if not isinstance(ctx.channel, discord.DMChannel):
await ctx.send("This command can only be used in a direct message to the bot.")
2023-03-13 13:10:07 +01:00
return False
return True
2023-03-12 22:58:29 +01:00
async def interactive_application(self, author: discord.Member):
"""Ask the user several questions to create an application."""
2023-03-13 13:33:26 +01:00
embed = discord.Embed(
title="Kanium Membership Application",
description="Ah, you wish to apply for Kanium membership. Very well, understand that This process is very important to us so we expect you to put effort in and be glorious about it. Let us begin!",
color=discord.Color.green(),
)
await author.send(embed=embed)
2023-03-13 13:26:46 +01:00
questions = ["First of all, what is your name?", "What age are you?", "Where are you from?", "Do you have any hobbies?", "Are you wishing to join because of a particular game? If so, which game?", "Write out, in a free-style way, what your motivation is for wanting to join us in particular and how you would be a good fit for Kanium"]
2023-03-12 22:55:57 +01:00
try:
2023-03-12 22:58:29 +01:00
answers = await self.ask_questions(author, questions)
2023-03-12 22:55:57 +01:00
except asyncio.TimeoutError:
await author.send("You took too long to answer. Please try again later.")
return
2023-03-13 13:10:07 +01:00
embeddedApplication = await self.format_application(answers, author)
2023-03-12 22:55:57 +01:00
2023-03-13 13:10:07 +01:00
# Call the sendApplication to check if the author is a member of the guild and send the application if they are
await self.sendApplication(author, embeddedApplication)
2023-03-12 23:14:26 +01:00
2023-03-12 22:55:57 +01:00
2023-03-13 13:10:07 +01:00
async def sendApplication(self, author: discord.Member, embeddedApplication: discord.Embed):
2023-03-13 01:21:02 +01:00
# Check if the author is a member of the guild
2023-03-13 13:10:07 +01:00
guild = self.bot.get_guild(guild_id)
2023-03-13 01:21:02 +01:00
member = guild.get_member(author.id)
if member is None:
await author.send("You need to join the server before your application can be processed.")
return
2023-03-12 22:55:57 +01:00
2023-03-13 13:10:07 +01:00
# 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)
2023-03-13 01:21:02 +01:00
# Assign the Trial role to the author
2023-03-13 01:10:31 +01:00
role = guild.get_role(531181363420987423)
2023-03-13 01:21:02 +01:00
await member.add_roles(role)
2023-03-13 01:10:31 +01:00
await author.send("Thank you for submitting your application! You have been given the 'Trial' role.")
2023-03-13 13:10:07 +01:00
async def add_reactions(self, message: discord.Message):
reactions = ["", "", ""]
for reaction in reactions:
await message.add_reaction(reaction)
2023-03-12 22:55:57 +01:00
async def format_application(self, answers: List[str], author: discord.Member) -> discord.Embed:
2023-03-12 22:58:29 +01:00
"""Format the application answers into an embed."""
2023-03-13 00:34:56 +01:00
embed = discord.Embed(title=f"Application from {author.display_name}", color=discord.Color.green())
2023-03-13 00:23:52 +01:00
embed.set_thumbnail(url=author.avatar_url)
2023-03-12 23:05:14 +01:00
embed.add_field(name="Name", value=answers[0])
2023-03-12 22:55:57 +01:00
embed.add_field(name="Discord ID", value=author.id)
embed.add_field(name="Age", value=answers[1])
embed.add_field(name="Reason wishing to become a member:", value=answers[2])
2023-03-13 00:34:56 +01:00
2023-03-12 22:55:57 +01:00
return embed
async def ask_questions(self, author: discord.Member, questions: List[str]) -> List[str]:
2023-03-12 22:58:29 +01:00
"""Ask the user several questions and return the answers."""
2023-03-12 22:55:57 +01:00
answers = []
for question in questions:
await author.send(question)
2023-03-13 13:10:07 +01:00
answer = await self.get_answers(author)
2023-03-12 22:55:57 +01:00
answers.append(answer.content)
2023-03-12 22:58:29 +01:00
return answers
2023-03-13 13:10:07 +01:00
async def get_answers(self, author: discord.Member) -> discord.Message:
2023-03-12 22:58:29 +01:00
"""Wait for the user to send a message."""
return await self.bot.wait_for(
"message", check=lambda m: m.author == author and m.guild is None
)