2023-03-12 12:13:30 +01:00
|
|
|
import asyncio
|
2023-03-12 18:02:28 +01:00
|
|
|
from typing import Union, List
|
2023-03-12 12:13:30 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
from copy import copy
|
|
|
|
|
import contextlib
|
|
|
|
|
import discord
|
|
|
|
|
|
2023-03-12 17:49:13 +01:00
|
|
|
|
2023-03-12 12:13:30 +01:00
|
|
|
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
|
|
|
|
|
from redbot.core.utils.tunnel import Tunnel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Recruitment(commands.Cog):
|
|
|
|
|
"""Create user applications that server staff can respond to.
|
|
|
|
|
|
|
|
|
|
Users can open an application using `[p]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.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
default_guild_settings = {"output_channel": None, "active": False, "next_application": 1}
|
|
|
|
|
|
2023-03-12 17:49:13 +01:00
|
|
|
default_application = {"application": {}}
|
2023-03-12 12:13:30 +01:00
|
|
|
|
|
|
|
|
# This can be made configurable later if it
|
|
|
|
|
# becomes an issue.
|
|
|
|
|
# Intervals should be a list of tuples in the form
|
|
|
|
|
# (period: timedelta, max_frequency: int)
|
|
|
|
|
# see redbot/core/utils/antispam.py for more details
|
|
|
|
|
|
|
|
|
|
intervals = [
|
|
|
|
|
(timedelta(seconds=5), 1),
|
|
|
|
|
(timedelta(minutes=5), 3),
|
|
|
|
|
(timedelta(hours=1), 10),
|
|
|
|
|
(timedelta(days=1), 24),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def __init__(self, bot: Red):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.bot = bot
|
2023-03-12 17:29:05 +01:00
|
|
|
self.config = Config.get_conf(self, 42631423034200142, force_registration=True)
|
|
|
|
|
self.config.register_guild(**self.default_guild_settings)
|
2023-03-12 17:49:13 +01:00
|
|
|
self.config.init_custom("RECRUITMENT", 2)
|
|
|
|
|
self.config.register_custom("RECRUITMENT", **self.default_application)
|
2023-03-12 12:13:30 +01:00
|
|
|
self.antispam = {}
|
|
|
|
|
self.user_cache = []
|
|
|
|
|
self.tunnel_store = {}
|
2023-03-12 17:29:05 +01:00
|
|
|
|
2023-03-12 12:13:30 +01:00
|
|
|
# (guild, ticket#):
|
|
|
|
|
# {'tun': Tunnel, 'msgs': List[int]}
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def tunnels(self):
|
|
|
|
|
return [x["tun"] for x in self.tunnel_store.values()]
|
|
|
|
|
|
|
|
|
|
@checks.admin_or_permissions(manage_guild=True)
|
|
|
|
|
@commands.guild_only()
|
|
|
|
|
@commands.group(name="applicationset")
|
|
|
|
|
async def applicationset(self, ctx: commands.Context):
|
|
|
|
|
"""+++Manage Applications+++"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@checks.admin_or_permissions(manage_guild=True)
|
|
|
|
|
@applicationset.command(name="output")
|
|
|
|
|
async def reportset_output(
|
2023-03-12 17:00:59 +01:00
|
|
|
self, ctx: commands.Context, channel: discord.TextChannel
|
2023-03-12 12:13:30 +01:00
|
|
|
):
|
|
|
|
|
"""Set the channel where applications will be sent."""
|
|
|
|
|
await self.config.guild(ctx.guild).output_channel.set(channel.id)
|
2023-03-12 18:02:28 +01:00
|
|
|
await ctx.send("The application channel has been set.")
|
2023-03-12 12:13:30 +01:00
|
|
|
|
|
|
|
|
@checks.admin_or_permissions(manage_guild=True)
|
|
|
|
|
@applicationset.command(name="toggle", aliases=["toggleactive"])
|
|
|
|
|
async def applicationset_toggle(self, ctx: commands.Context):
|
|
|
|
|
"""Enable or disable applications for this server."""
|
|
|
|
|
active = await self.config.guild(ctx.guild).active()
|
|
|
|
|
active = not active
|
|
|
|
|
await self.config.guild(ctx.guild).active.set(active)
|
|
|
|
|
if active:
|
2023-03-12 18:02:28 +01:00
|
|
|
await ctx.send("Applications are now enabled")
|
2023-03-12 12:13:30 +01:00
|
|
|
else:
|
2023-03-12 18:02:28 +01:00
|
|
|
await ctx.send("Applications are now disabled")
|
2023-03-12 12:13:30 +01:00
|
|
|
|
|
|
|
|
async def internal_filter(self, m: discord.Member, mod=False, perms=None):
|
|
|
|
|
if perms and m.guild_permissions >= perms:
|
|
|
|
|
return True
|
|
|
|
|
if mod and await self.bot.is_mod(m):
|
|
|
|
|
return True
|
|
|
|
|
# The following line is for consistency with how perms are handled
|
|
|
|
|
# in Red, though I'm not sure it makes sense to use here.
|
|
|
|
|
if await self.bot.is_owner(m):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
async def discover_guild(
|
|
|
|
|
self,
|
|
|
|
|
author: discord.User,
|
|
|
|
|
*,
|
|
|
|
|
mod: bool = False,
|
|
|
|
|
permissions: Union[discord.Permissions, dict] = None,
|
|
|
|
|
prompt: str = "",
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
discovers which of shared guilds between the bot
|
|
|
|
|
and provided user based on conditions (mod or permissions is an or)
|
|
|
|
|
|
|
|
|
|
prompt is for providing a user prompt for selection
|
|
|
|
|
"""
|
|
|
|
|
shared_guilds = []
|
|
|
|
|
if permissions is None:
|
|
|
|
|
perms = discord.Permissions()
|
|
|
|
|
elif isinstance(permissions, discord.Permissions):
|
|
|
|
|
perms = permissions
|
|
|
|
|
else:
|
|
|
|
|
perms = discord.Permissions(**permissions)
|
|
|
|
|
|
|
|
|
|
async for guild in AsyncIter(self.bot.guilds, steps=100):
|
|
|
|
|
x = guild.get_member(author.id)
|
|
|
|
|
if x is not None:
|
|
|
|
|
if await self.internal_filter(x, mod, perms):
|
|
|
|
|
shared_guilds.append(guild)
|
|
|
|
|
if len(shared_guilds) == 0:
|
|
|
|
|
raise ValueError("No Qualifying Shared Guilds")
|
|
|
|
|
if len(shared_guilds) == 1:
|
|
|
|
|
return shared_guilds[0]
|
|
|
|
|
output = ""
|
|
|
|
|
guilds = sorted(shared_guilds, key=lambda g: g.name)
|
|
|
|
|
for i, guild in enumerate(guilds, 1):
|
|
|
|
|
output += "{}: {}\n".format(i, guild.name)
|
|
|
|
|
output += "\n{}".format(prompt)
|
|
|
|
|
|
|
|
|
|
for page in pagify(output, delims=["\n"]):
|
|
|
|
|
await author.send(box(page))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
message = await self.bot.wait_for(
|
|
|
|
|
"message",
|
|
|
|
|
check=MessagePredicate.same_context(channel=author.dm_channel, user=author),
|
|
|
|
|
timeout=45,
|
|
|
|
|
)
|
|
|
|
|
except asyncio.TimeoutError:
|
2023-03-12 18:02:28 +01:00
|
|
|
await author.send("You took too long to select. Try again later.")
|
2023-03-12 12:13:30 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
message = int(message.content.strip())
|
|
|
|
|
guild = guilds[message - 1]
|
|
|
|
|
except (ValueError, IndexError):
|
2023-03-12 18:02:28 +01:00
|
|
|
await author.send("That wasn't a valid choice.")
|
2023-03-12 12:13:30 +01:00
|
|
|
return None
|
|
|
|
|
else:
|
|
|
|
|
return guild
|
|
|
|
|
|
|
|
|
|
async def send_application(self, ctx: commands.Context, msg: discord.Message, guild: discord.Guild):
|
|
|
|
|
author = guild.get_member(msg.author.id)
|
|
|
|
|
application = msg.clean_content
|
|
|
|
|
|
|
|
|
|
channel_id = await self.config.guild(guild).output_channel()
|
|
|
|
|
channel = guild.get_channel(channel_id)
|
|
|
|
|
if channel is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
files: List[discord.File] = await Tunnel.files_from_attach(msg)
|
|
|
|
|
|
|
|
|
|
application_number = await self.config.guild(guild).next_application()
|
|
|
|
|
await self.config.guild(guild).next_application.set(application_number + 1)
|
|
|
|
|
|
|
|
|
|
if await self.bot.embed_requested(channel):
|
|
|
|
|
em = discord.Embed(description=application, colour=await ctx.embed_colour())
|
|
|
|
|
em.set_author(
|
2023-03-12 18:02:28 +01:00
|
|
|
name="Application from {author}{maybe_nick}".format(
|
2023-03-12 12:13:30 +01:00
|
|
|
author=author, maybe_nick=(f" ({author.nick})" if author.nick else "")
|
|
|
|
|
),
|
|
|
|
|
icon_url=author.display_avatar,
|
|
|
|
|
)
|
2023-03-12 18:02:28 +01:00
|
|
|
em.set_footer(text="Application #{}".format(application_number))
|
2023-03-12 12:13:30 +01:00
|
|
|
send_content = None
|
|
|
|
|
else:
|
|
|
|
|
em = None
|
2023-03-12 18:02:28 +01:00
|
|
|
send_content = "Application from {author.mention} (Application #{number})".format(
|
2023-03-12 12:13:30 +01:00
|
|
|
author=author, number=application_number
|
|
|
|
|
)
|
|
|
|
|
send_content += "\n" + application
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await Tunnel.message_forwarder(
|
|
|
|
|
destination=channel, content=send_content, embed=em, files=files
|
|
|
|
|
)
|
|
|
|
|
except (discord.Forbidden, discord.HTTPException):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
return application_number
|
|
|
|
|
|
2023-03-12 17:49:13 +01:00
|
|
|
@commands.group(name="application", usage="[text]", invoke_without_command=True)
|
2023-03-12 12:13:30 +01:00
|
|
|
async def application(self, ctx: commands.Context, *, _application: str = ""):
|
|
|
|
|
"""Send an application.
|
|
|
|
|
|
|
|
|
|
Use without arguments for an interactive experience, or do
|
|
|
|
|
`[p]apply [text]` to use it non-interactively.
|
|
|
|
|
"""
|
|
|
|
|
author = ctx.author
|
|
|
|
|
guild = ctx.guild
|
|
|
|
|
if guild is None:
|
|
|
|
|
guild = await self.discover_guild(
|
2023-03-12 18:02:28 +01:00
|
|
|
author, prompt="Select a server to make an application in by number."
|
2023-03-12 12:13:30 +01:00
|
|
|
)
|
|
|
|
|
if guild is None:
|
|
|
|
|
return
|
|
|
|
|
g_active = await self.config.guild(guild).active()
|
|
|
|
|
if not g_active:
|
2023-03-12 18:02:28 +01:00
|
|
|
return await author.send("Applications are currently closed")
|
2023-03-12 12:13:30 +01:00
|
|
|
if guild.id not in self.antispam:
|
|
|
|
|
self.antispam[guild.id] = {}
|
|
|
|
|
if author.id not in self.antispam[guild.id]:
|
|
|
|
|
self.antispam[guild.id][author.id] = AntiSpam(self.intervals)
|
|
|
|
|
if self.antispam[guild.id][author.id].spammy:
|
2023-03-12 18:02:28 +01:00
|
|
|
return await author.send(
|
2023-03-12 12:13:30 +01:00
|
|
|
"You've sent too many applications recently. "
|
|
|
|
|
"Are you sure you are in the right place? "
|
|
|
|
|
)
|
|
|
|
|
if author.id in self.user_cache:
|
|
|
|
|
return await author.send(
|
|
|
|
|
"Please finish making your prior application before trying to make an "
|
|
|
|
|
"additional one!"
|
|
|
|
|
)
|
|
|
|
|
self.user_cache.append(author.id)
|
|
|
|
|
|
|
|
|
|
if _application:
|
|
|
|
|
_m = copy(ctx.message)
|
|
|
|
|
_m.content = _application
|
|
|
|
|
_m.content = _m.clean_content
|
|
|
|
|
val = await self.send_application(ctx, _m, guild)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
await author.send(
|
|
|
|
|
"Please respond to this message with your application."
|
|
|
|
|
"\nYour application should be a single message"
|
|
|
|
|
)
|
|
|
|
|
except discord.Forbidden:
|
2023-03-12 18:02:28 +01:00
|
|
|
return await ctx.send("This requires DMs enabled.")
|
2023-03-12 12:13:30 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
message = await self.bot.wait_for(
|
|
|
|
|
"message",
|
|
|
|
|
check=MessagePredicate.same_context(ctx, channel=author.dm_channel),
|
|
|
|
|
timeout=180,
|
|
|
|
|
)
|
|
|
|
|
except asyncio.TimeoutError:
|
2023-03-12 18:02:28 +01:00
|
|
|
return await author.send("You took too long. Try again later.")
|
2023-03-12 12:13:30 +01:00
|
|
|
else:
|
|
|
|
|
val = await self.send_application(ctx, message, guild)
|
2023-03-12 16:34:38 +01:00
|
|
|
# Get the role to assign using its ID
|
|
|
|
|
trialRole_id = 531181363420987423
|
|
|
|
|
role = get(ctx.guild.roles, id=trialRole_id)
|
|
|
|
|
|
|
|
|
|
# Assign the role to the user who sent the application
|
|
|
|
|
if role is not None:
|
|
|
|
|
await author.add_roles(role)
|
2023-03-12 12:13:30 +01:00
|
|
|
|
|
|
|
|
with contextlib.suppress(discord.Forbidden, discord.HTTPException):
|
|
|
|
|
if val is None:
|
|
|
|
|
if await self.config.guild(ctx.guild).output_channel() is None:
|
|
|
|
|
await author.send(
|
|
|
|
|
"Hmm, most embarrassing, it rather seems Hatt has neglected to tell me where the applications room is. Please contact him for me."
|
|
|
|
|
)
|
|
|
|
|
else:
|
2023-03-12 18:02:28 +01:00
|
|
|
await author.send("Drat! There was an error sending your application, please contact Hatt.")
|
2023-03-12 12:13:30 +01:00
|
|
|
else:
|
2023-03-12 18:02:28 +01:00
|
|
|
await author.send("Your application was submitted. (Application #{})".format(val))
|
2023-03-12 12:13:30 +01:00
|
|
|
self.antispam[guild.id][author.id].stamp()
|
|
|
|
|
|
|
|
|
|
@application.after_invoke
|
|
|
|
|
async def application_cleanup(self, ctx: commands.Context):
|
|
|
|
|
"""
|
|
|
|
|
The logic is cleaner this way
|
|
|
|
|
"""
|
|
|
|
|
if ctx.author.id in self.user_cache:
|
|
|
|
|
self.user_cache.remove(ctx.author.id)
|
|
|
|
|
if ctx.guild and ctx.invoked_subcommand is None:
|
|
|
|
|
if ctx.bot_permissions.manage_messages:
|
|
|
|
|
try:
|
|
|
|
|
await ctx.message.delete()
|
|
|
|
|
except discord.NotFound:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
|
async def on_message(self, message: discord.Message):
|
|
|
|
|
to_remove = []
|
|
|
|
|
|
|
|
|
|
for k, v in self.tunnel_store.items():
|
|
|
|
|
guild, application_number = k
|
|
|
|
|
if await self.bot.cog_disabled_in_guild(self, guild):
|
|
|
|
|
to_remove.append(k)
|
|
|
|
|
continue
|
|
|
|
|
|
2023-03-12 18:02:28 +01:00
|
|
|
topic = "Re: application# {application_number} in {guild.name}".format(
|
2023-03-12 12:13:30 +01:00
|
|
|
application_number=application_number, guild=guild
|
|
|
|
|
)
|
|
|
|
|
# Tunnels won't forward unintended messages, this is safe
|
|
|
|
|
msgs = await v["tun"].communicate(message=message, topic=topic)
|
|
|
|
|
if msgs:
|
|
|
|
|
self.tunnel_store[k]["msgs"] = msgs
|
|
|
|
|
|
|
|
|
|
for key in to_remove:
|
|
|
|
|
if tun := self.tunnel_store.pop(key, None):
|
|
|
|
|
guild, application = key
|
|
|
|
|
await tun["tun"].close_because_disabled(
|
|
|
|
|
"Correspondence about application# {application_number} in "
|
|
|
|
|
"{guild.name} has been ended due "
|
|
|
|
|
"to applications being closed."
|
2023-03-12 18:02:28 +01:00
|
|
|
.format(application_number=application, guild=guild)
|
2023-03-12 12:13:30 +01:00
|
|
|
)
|