Skip to content

Commit

Permalink
Use forums instead of channels now
Browse files Browse the repository at this point in the history
  • Loading branch information
MattBSG committed Jul 13, 2024
1 parent 7d0c839 commit 41bc347
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 81 deletions.
86 changes: 53 additions & 33 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,48 @@ def __init__(self, bot):
@app_commands.describe(delay='The delay for the modmail to close, in 1w2d3h4m5s format')
@app_commands.guilds(discord.Object(id=config.guild))
@app_commands.default_permissions(view_audit_log=True)
async def _close(self, interaction: discord.Interaction, delay: typing.Optional[str]):
message, ephemeral = await self._close_generic(interaction.user, interaction.guild, interaction.channel, delay)
async def _close(self, interaction: discord.Interaction, delay: typing.Optional[str], auto: bool = False):
if not delay:
await interaction.response.send_message(f'This thread has been closed by {interaction.user}. Use `/open` to send any followup messages to this user.')

if message:
await interaction.response.send_message(message, ephemeral=ephemeral)
else:
await interaction.response.defer()

try:
scheduledTime = await self._close_generic(interaction.user, interaction.guild, interaction.channel, delay)

except exceptions.InvalidDuration:
await interaction.followup.send('Invalid duration passed', ephemeral=True)

except exceptions.NotAModmail:
await interaction.followup.send('This is not a modmail channel!', ephemeral=True)

except exceptions.InvalidType:
await interaction.followup.send(':x: Ban appeals cannot be closed with the `/close` command. Use `/appeal accept` or `/appeal deny` instead', ephemeral=True)

if delay:
await interaction.followup.send(f'Thread has been scheduled to be closed {scheduledTime} by {interaction.user}. Once closed, use `/open` to send any followup messages to this user.')

async def _close_generic(self, user, guild, channel, delay):
db = mclient.modmail.logs
doc = db.find_one({'channel_id': str(channel.id), 'open': True})

if not doc:
return 'This is not a modmail channel!', True
raise exceptions.NotAModmail

if doc['_id'] in self.closeQueue:
self.closeQueue[doc['_id']].cancel()

if doc['ban_appeal']:
return (
':x: Ban appeals cannot be closed with the `/close` command. Use `/appeal accept` or `/appeal deny` instead',
False,
)
raise exceptions.InvalidType

if delay:
try:
delayDate = utils.resolve_duration(delay)
delayTime = delayDate.timestamp() - datetime.now(tz=timezone.utc).timestamp()

except KeyError:
return 'Invalid duration', False
raise exceptions.InvalidDuration

event_loop = self.bot.loop
close_action = event_loop.call_later(
Expand All @@ -81,10 +94,10 @@ async def _close_generic(self, user, guild, channel, delay):
utils._close_thread(self.bot, user, guild, channel, self.bot.get_channel(config.modLog)),
)
self.closeQueue[doc['_id']] = close_action
return f'Thread scheduled to be closed <t:{int(delayDate.timestamp())}:R>', False

await utils._close_thread(self.bot, user, guild, channel, self.bot.get_channel(config.modLog))
return None, False
return f'<t:{int(delayDate.timestamp())}:R>'
else:
await utils._close_thread(self.bot, user, guild, channel, self.bot.get_channel(config.modLog))

@app_commands.command(name='reply', description='Replys to a modmail, with your username')
@app_commands.describe(content='The message to send to the user')
Expand Down Expand Up @@ -234,6 +247,7 @@ async def _open_context(self, interaction: discord.Interaction, member: discord.
Open a modmail thread with a user
"""

await interaction.response.defer()
await self._open_thread(interaction, member)

async def _open_thread(self, interaction: discord.Interaction, member: discord.Member):
Expand Down Expand Up @@ -265,7 +279,7 @@ async def _message_report(self, interaction: discord.Interaction, message: disco
return await interaction.followup.send(':x: You cannot report messages sent by bots', ephemeral=True)

try:
dmOpened = await self._user_create_thread(message, interaction)
dmOpened = await self._user_create_thread(message, interaction, menu_interacted=True)

except exceptions.ModmailBlacklisted:
return await interaction.followup.send(
Expand Down Expand Up @@ -307,6 +321,8 @@ async def _appeal_accept(self, interaction: discord.Interaction, reason: app_com
if not doc:
return await interaction.response.send_message(':x: This is not a ban appeal channel!', ephemeral=True)

await interaction.response.defer()

user = await self.bot.fetch_user(int(doc['recipient']['id']))
punsDB.update_one({'user': user.id, 'type': 'ban', 'active': True}, {'$set': {'active': False}})
punsDB.update_one({'user': user.id, 'type': 'appealdeny', 'active': True}, {'$set': {'active': False}})
Expand Down Expand Up @@ -343,13 +359,13 @@ async def _appeal_accept(self, interaction: discord.Interaction, reason: app_com
)

except:
await self.bot.get_channel(config.adminChannel).send(
f':warning: The ban appeal for {user} has been accepted by {interaction.user}, but I was unable to DM them the decision'
await interaction.followup.send(
f':warning: This ban appeal for {user} has been accepted by {interaction.user}, but I was unable to DM them the decision.'
)

else:
await self.bot.get_channel(config.adminChannel).send(
f':white_check_mark: The ban appeal for {user} has been accepted by {interaction.user}'
await interaction.followup.send(
f':white_check_mark: This ban appeal for {user} has been accepted by {interaction.user}.'
)

finally:
Expand Down Expand Up @@ -385,6 +401,8 @@ async def _appeal_deny(
if not doc:
return await interaction.response.send_message(':x: This is not a ban appeal channel!', ephemeral=True)

await interaction.response.defer()

user = await self.bot.fetch_user(int(doc['recipient']['id']))
try:
delayDate = utils.resolve_duration(next_attempt)
Expand Down Expand Up @@ -444,13 +462,13 @@ async def _appeal_deny(
)

except:
await self.bot.get_channel(config.adminChannel).send(
f':warning: The ban appeal for {user} has been denied by {interaction.user} {humanizedTimestamp}, but I was unable to DM them the decision'
await interaction.followup.send(
f':warning: This ban appeal for {user} has been denied by {interaction.user} {humanizedTimestamp}, but I was unable to DM them the decision.'
)

else:
await self.bot.get_channel(config.adminChannel).send(
f':white_check_mark: The ban appeal for {user} has been denied by {interaction.user} {humanizedTimestamp}'
await interaction.followup.send(
f':white_check_mark: This ban appeal for {user} has been denied by {interaction.user} {humanizedTimestamp}.'
)

finally:
Expand Down Expand Up @@ -505,8 +523,8 @@ async def on_member_ban(self, guild, member):
db = mclient.modmail.logs
thread = db.find_one({'recipient.id': str(member.id), 'open': True})
if thread:
channel = self.bot.get_guild(int(thread['guild_id'])).get_channel(int(thread['channel_id']))
await channel.send(f'**{member}** has been banned from the server')
channel = self.bot.get_channel(int(thread['channel_id']))
await channel.send(f'**{member}** has been banned from the server and this thread is now closed.')

if not thread['ban_appeal']:
await self._close_generic(member, member.guild, channel, None)
Expand Down Expand Up @@ -568,10 +586,8 @@ async def on_member_remove(self, member):
channel = self.bot.get_guild(int(thread['guild_id'])).get_channel(int(thread['channel_id']))

if member.guild.id == config.guild:
msg, _ = await self._close_generic(member, member.guild, channel, '4h')

if msg:
await channel.send(f'**{member}** has left the server. {msg}')
scheduledTime = await self._close_generic(member, member.guild, channel, '4h')
await channel.send(f'**{member}** has left the server. Thread scheduled to be closed {scheduledTime}')

elif (
thread['ban_appeal'] and member.guild.id == config.appealGuild
Expand Down Expand Up @@ -642,7 +658,7 @@ def _format_message_embed(

return content, embed

async def _user_create_thread(self, message: discord.Message, interaction: discord.Interaction = None):
async def _user_create_thread(self, message: discord.Message, interaction: discord.Interaction = None, menu_interacted: bool = False):
successfulDM = False
attachments = [x.url for x in message.attachments]
ctx = await self.bot.get_context(message)
Expand All @@ -666,7 +682,7 @@ async def _user_create_thread(self, message: discord.Message, interaction: disco
)

content, embed = self._format_message_embed(message, attachments, interaction=interaction)
await self.bot.get_guild(int(thread['guild_id'])).get_channel(int(thread['channel_id'])).send(
await self.bot.get_channel(int(thread['channel_id'])).send(
embed=embed
)
db.update_one(
Expand Down Expand Up @@ -698,12 +714,16 @@ async def _user_create_thread(self, message: discord.Message, interaction: disco
self.bot,
message.author if not interaction else interaction.user,
message,
'user',
'message_report' if menu_interacted else 'user',
interaction=interaction,
)

content, embed = self._format_message_embed(message, attachments, interaction=interaction)
await thread.send(embed=embed)
msgContent = f'<@&{config.modRole}>'
if not successfulDM:
msgContent += '\nPlease note, this user\'s DMs are closed. As such, they have been notified when they reported this message that they may not receive a moderator response.'

await thread.send(content=msgContent, embed=embed, silent=True)

if not interaction:
await message.add_reaction('✅')
Expand Down
Loading

0 comments on commit 41bc347

Please sign in to comment.