KaniumCogs/recruitmentCog/recruitment.py

88 lines
3.4 KiB
Python
Raw Normal View History

import asyncio
2023-03-12 18:02:28 +01:00
from typing import Union, List
from datetime import timedelta
from copy import copy
import contextlib
import discord
from redbot.core import Config, checks, commands
from redbot.core.utils import AsyncIter
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-12 21:26:26 +01:00
application_channel_id = 1023172488143839252
2023-03-12 21:41:08 +01:00
class Recruitment(commands.Cog):
2023-03-12 21:26:26 +01:00
"""A cog that lets a user send a membership application.
2023-03-12 21:26:26 +01:00
Users can open an application using `[!]apply`. These are then sent
to a channel in the server for staff, and the application creator
gets a DM. Both can be used to communicate.
"""
2023-03-12 21:41:08 +01:00
2023-03-12 21:26:26 +01:00
def __init__(self, bot):
self.bot = bot
2023-03-12 21:26:26 +01:00
self.message: str = ''
self.active = True
2023-03-12 21:42:43 +01:00
@commands.group(name="application", usage="[text]", invoke_without_command=True)
async def application(self, ctx: commands.Context, *, _application: str = ""):
"""Send an application.
2023-03-12 21:26:26 +01:00
Use without arguments for an interactive application creation flow, or do
2023-03-12 21:42:43 +01:00
`[p]application [text]` to use it non-interactively.
"""
author = ctx.author
2023-03-12 21:41:08 +01:00
# Check if the command was sent in a direct message to the bot
if not isinstance(ctx.channel, discord.DMChannel):
await ctx.send("This command can only be used in a direct message to the bot.")
return
2023-03-12 21:26:26 +01:00
# If there is no text argument, use an interactive flow
2023-03-12 21:42:43 +01:00
if not _application:
2023-03-12 22:08:59 +01:00
answers = ask_questions(self, author)
2023-03-12 21:41:08 +01:00
# Create an embed with the application information
application_embed = await format_application(answers, author)
# Send the embed to the application channel
2023-03-12 21:26:26 +01:00
application_channel = self.bot.get_channel(application_channel_id)
2023-03-12 21:41:08 +01:00
await application_channel.send(embed=application_embed)
2023-03-12 21:26:26 +01:00
# Send a confirmation message to the author
await author.send("Thank you for submitting your application!")
else:
# If there is a text argument, use a non-interactive flow
application_channel = self.bot.get_channel(application_channel_id)
2023-03-12 21:42:43 +01:00
await application_channel.send(_application)
2023-03-12 21:26:26 +01:00
await author.send("Thank you for submitting your application!")
2023-03-12 21:41:08 +01:00
async def format_application(answers: List[str], author: discord.Member) -> discord.Embed:
embed = discord.Embed(title="Application", color=discord.Color.green())
embed.add_field(name="Name", value=author.display_name)
embed.add_field(name="Discord ID", value=author.id)
embed.add_field(name="Age", value=answers[1])
embed.add_field(name="Reason for joining", value=answers[2])
return embed
2023-03-12 22:08:59 +01:00
async def ask_questions(self, author):
await author.send("Please answer the following questions to complete your application.")
questions = ["What's your name?", "What's your age?", "Why do you want to join our community?"]
answers = []
# Ask the user the questions
for question in questions:
await author.send(embed=discord.Embed(description=question))
# Wait for the user to respond
answer = await self.bot.wait_for(
"message", check=lambda m: m.author == author and m.guild is None
)
answers.append(answer.content)
return answers