Skip to content

Commit

Permalink
Merge pull request #135 from Taaku18/dev
Browse files Browse the repository at this point in the history
Scheduled close event for thread save to config vars
  • Loading branch information
kyb3r authored Jan 17, 2019
2 parents eefece7 + 03560c9 commit 325ad1a
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 75 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ 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.5.0

Non-Breaking Changes:

### Background
Bots hosted by Heroku restart at least once every 27 hours.
During this period, local caches are deleted, which results in the inability to
set the scheduled close time to longer than 24 hours. This update
resolves this issue.
[PR #135](https://github.com/kyb3r/modmail/pull/135)


### Changed
- Created a new internal config var: `closures`.
- Store closure details into `closures` when the scheduled time isn't "now".
- Loaded upon bot restart.
- Deleted when a thread is closed.
- Use `call_later()` instead of `sleep()` for scheduling.

# v2.4.5

### Fixed
Expand Down
31 changes: 30 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

__version__ = '2.4.5'
__version__ = '2.5.0'

import asyncio
import textwrap
Expand Down Expand Up @@ -202,8 +202,37 @@ async def on_ready(self):
print(Fore.RED + Style.BRIGHT + 'WARNING - The GUILD_ID provided does not exist!' + Style.RESET_ALL)
else:
await self.threads.populate_cache()
await self.config.update()

closures = self.config.closures.copy()

for recipient_id, items in closures.items():
after = (datetime.datetime.fromisoformat(items['time']) -
datetime.datetime.utcnow()).total_seconds()
if after < 0:
after = 0
recipient = self.get_user(int(recipient_id))

thread = await self.threads.find(
recipient=recipient)

if not thread:
# If the recipient is gone or channel is deleted
self.config.closures.pop(str(recipient_id))
await self.config.update()
continue

# TODO: Low priority,
# Retrieve messages/replies when bot is down, from history?
self.loop.create_task(
thread.close(
closer=self.get_user(items['closer_id']),
after=after,
silent=items['silent'],
delete_channel=items['delete_channel'],
message=items['message']
)
)

async def process_modmail(self, message):
"""Processes messages sent to the bot."""
Expand Down
10 changes: 5 additions & 5 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ async def _close(self, ctx, *, after: UserFriendlyTime=None):
silent = str(message).lower() in {'silent', 'silently'}
cancel = str(message).lower() == 'cancel'

if cancel and thread.close_task is not None and not thread.close_task.cancelled():
thread.close_task.cancel()
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description='Scheduled close has been cancelled.'))
return
elif cancel:
if cancel:
if thread.close_task is not None:
await thread.cancel_closure()
await ctx.send(embed=discord.Embed(color=discord.Color.red(), description='Scheduled close has been cancelled.'))
return
return await ctx.send(embed=discord.Embed(color=discord.Color.red(), description='This thread has not already been scheduled to close.'))

if after and after.dt > now:
Expand Down
18 changes: 12 additions & 6 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ class ConfigManager:
"""Class that manages a cached configuration"""

allowed_to_change_in_command = {
'activity_message', 'activity_type', 'log_channel_id', 'mention', 'disable_autoupdates', 'prefix',
'main_category_id', 'sent_emoji', 'blocked_emoji', 'thread_creation_response', 'twitch_url'
'activity_message', 'activity_type', 'log_channel_id',
'mention', 'disable_autoupdates', 'prefix',
'main_category_id', 'sent_emoji', 'blocked_emoji',
'thread_creation_response', 'twitch_url'
}

internal_keys = {
'snippets', 'aliases', 'blocked', 'notification_squad', 'subscriptions'
'snippets', 'aliases', 'blocked',
'notification_squad', 'subscriptions',
'closures'
}

protected_keys = {
'token', 'owners', 'modmail_api_token', 'guild_id', 'modmail_guild_id',
'mongo_uri', 'github_access_token', 'log_url'
}

valid_keys = allowed_to_change_in_command.union(internal_keys).union(protected_keys)
valid_keys = allowed_to_change_in_command | internal_keys | protected_keys

def __init__(self, bot):
self.bot = bot
Expand All @@ -38,7 +42,8 @@ def populate_cache(self):
'aliases': {},
'blocked': {},
'notification_squad': {},
'subscriptions': {}
'subscriptions': {},
'closures': {},
}

try:
Expand All @@ -47,7 +52,8 @@ def populate_cache(self):
pass
finally:
data.update(os.environ)
data = {k.lower(): v for k, v in data.items() if k.lower() in self.valid_keys}
data = {k.lower(): v for k, v in data.items()
if k.lower() in self.valid_keys}
self.cache = data

async def update(self, data=None):
Expand Down
Loading

0 comments on commit 325ad1a

Please sign in to comment.