Skip to content

Commit

Permalink
Deleting thread channels is now a valid way to close a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kyb3r committed Jan 6, 2019
1 parent 6488a80 commit aeda15e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

#v2.0.4
# v2.0.5

### Changed
- Alias command now checks if you are adding a valid alias - command combo.
- Deleting a channel manually will now correctly close the thread and post logs.

# v2.0.4

### Fixed
- Fixed a one off bug where the channel topic dissapears, but modmail operations should still continue
Expand Down
47 changes: 45 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

__version__ = '2.0.4'
__version__ = '2.0.5'

import asyncio
import textwrap
Expand Down Expand Up @@ -244,6 +244,48 @@ async def on_message(self, message):
message.content = f'{prefix}reply {self.snippets[cmd]}'

await self.process_commands(message)

async def on_guild_channel_delete(self, channel):
if channel.guild != self.modmail_guild:
return
thread = await self.threads.find(channel=channel)
if thread:
del self.threads.cache[thread.id]

mod = None

audit_logs = self.modmail_guild.audit_logs()
entry = await audit_logs.find(lambda e: e.target.id == channel.id)
mod = entry.user

log_data = await self.modmail_api.post_log(channel.id, {
'open': False,
'closed_at': str(datetime.datetime.utcnow()),
'closer': {
'id': str(mod.id),
'name': mod.name,
'discriminator': mod.discriminator,
'avatar_url': mod.avatar_url,
'mod': True
}})

em = discord.Embed(title='Thread Closed')
em.description = f'{mod.mention} has closed this modmail thread.'
em.color = discord.Color.red()

try:
await thread.recipient.send(embed=em)
except:
pass

log_url = f"https://logs.modmail.tk/{log_data['user_id']}/{log_data['key']}"

user = thread.recipient.mention if thread.recipient else f'`{thread.id}`'

desc = f"[`{log_data['key']}`]({log_url}) {mod.mention} closed a thread with {user}"
em = discord.Embed(description=desc, color=em.color)
em.set_author(name='Thread closed', url=log_url)
await self.main_category.channels[0].send(embed=em)

async def on_message_delete(self, message):
"""Support for deleting linked messages"""
Expand Down Expand Up @@ -342,7 +384,7 @@ async def autoupdate_loop(self):

if metadata['latest_version'] != self.version:
data = await self.modmail_api.update_repository()
print('Updating bot.')


em = discord.Embed(title='Updating bot', color=discord.Color.green())

Expand All @@ -357,6 +399,7 @@ async def autoupdate_loop(self):
html_url = commit_data["html_url"]
short_sha = commit_data['sha'][:6]
em.add_field(name='Merge Commit', value=f"[`{short_sha}`]({html_url}) {message} - {user['username']}")
print('Updating bot.')
else:
await asyncio.sleep(3600)
continue
Expand Down
6 changes: 6 additions & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ async def _add(self, ctx, name: str.lower, *, value):
if 'aliases' not in self.bot.config.cache:
self.bot.config['aliases'] = {}

if self.bot.get_command(name) or self.bot.config.aliases.get(name):
return await ctx.send(f'A command or alias already exists with the same name: `{name}`')

if not self.bot.get_command(value.split()[0]):
return await ctx.send(f'The command you are attempting to point to does not exist: `{value.split()[0]}`')

self.bot.config.aliases[name] = value
await self.bot.config.update()

Expand Down

0 comments on commit aeda15e

Please sign in to comment.