Skip to content

Commit 7cfcd64

Browse files
Taaku18fourjr
authored andcommitted
[v2.19.0] Added guild_age config (#226)
* Added guild_age config * Updated changelog * Update CHANGELOG.md * Increment version
1 parent 5084f93 commit 7cfcd64

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
8+
# v2.19.0
9+
10+
### What's new?
11+
12+
- New config variable `guild_age`, similar to `account_age`, `guild_age` sets a limit as to how long a user has to wait after they joined the server to message Modmail.
13+
- `guild_age` can be set the same way as `account_age`.
14+
715
# v2.18.5
816

917
Fix help command bug when using external plugins.
@@ -68,7 +76,6 @@ When updating to this version, all prior permission settings with guild-based pe
6876

6977
- The help message no longer conceals inaccessible commands due to check failures.
7078

71-
7279
# v2.17.2
7380

7481
### Changed

bot.py

+49-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
"""
2424

25-
__version__ = '2.18.5'
25+
__version__ = '2.19.0'
2626

2727
import asyncio
2828
import logging
@@ -467,8 +467,10 @@ async def retrieve_emoji(self):
467467
async def process_modmail(self, message):
468468
"""Processes messages sent to the bot."""
469469
sent_emoji, blocked_emoji = await self.retrieve_emoji()
470+
now = datetime.utcnow()
470471

471472
account_age = self.config.get('account_age')
473+
guild_age = self.config.get('guild_age')
472474
if account_age is None:
473475
account_age = isodate.duration.Duration()
474476
else:
@@ -483,19 +485,41 @@ async def process_modmail(self, message):
483485
await self.config.update()
484486
account_age = isodate.duration.Duration()
485487

488+
if guild_age is None:
489+
guild_age = isodate.duration.Duration()
490+
else:
491+
try:
492+
guild_age = isodate.parse_duration(guild_age)
493+
except isodate.ISO8601Error:
494+
logger.warning('The guild join age limit needs to be a '
495+
'ISO-8601 duration formatted duration string '
496+
f'greater than 0 days, not "%s".', str(guild_age))
497+
del self.config.cache['guild_age']
498+
await self.config.update()
499+
guild_age = isodate.duration.Duration()
500+
486501
reason = self.blocked_users.get(str(message.author.id))
487502
if reason is None:
488503
reason = ''
504+
489505
try:
490506
min_account_age = message.author.created_at + account_age
491507
except ValueError as e:
492508
logger.warning(e.args[0])
493509
del self.config.cache['account_age']
494510
await self.config.update()
495-
min_account_age = message.author.created_at
511+
min_account_age = now
496512

497-
if min_account_age > datetime.utcnow():
498-
# user account has not reached the required time
513+
try:
514+
min_guild_age = self.guild.get_member(message.author.id).joined_at + guild_age
515+
except ValueError as e:
516+
logger.warning(e.args[0])
517+
del self.config.cache['guild_age']
518+
await self.config.update()
519+
min_guild_age = now
520+
521+
if min_account_age > now:
522+
# User account has not reached the required time
499523
reaction = blocked_emoji
500524
changed = False
501525
delta = human_timedelta(min_account_age)
@@ -514,6 +538,26 @@ async def process_modmail(self, message):
514538
color=discord.Color.red()
515539
))
516540

541+
elif min_guild_age > now:
542+
# User has not stayed in the guild for long enough
543+
reaction = blocked_emoji
544+
changed = False
545+
delta = human_timedelta(min_guild_age)
546+
547+
if str(message.author.id) not in self.blocked_users:
548+
new_reason = f'System Message: Recently Joined. Required to wait for {delta}.'
549+
self.config.blocked[str(message.author.id)] = new_reason
550+
await self.config.update()
551+
changed = True
552+
553+
if reason.startswith('System Message: Recently Joined.') or changed:
554+
await message.channel.send(embed=discord.Embed(
555+
title='Message not sent!',
556+
description=f'Your must wait for {delta} '
557+
f'before you can contact {self.user.mention}.',
558+
color=discord.Color.red()
559+
))
560+
517561
elif str(message.author.id) in self.blocked_users:
518562
reaction = blocked_emoji
519563
if reason.startswith('System Message: New Account.'):
@@ -524,8 +568,7 @@ async def process_modmail(self, message):
524568
else:
525569
end_time = re.search(r'%(.+?)%$', reason)
526570
if end_time is not None:
527-
after = (datetime.fromisoformat(end_time.group(1)) -
528-
datetime.utcnow()).total_seconds()
571+
after = (datetime.fromisoformat(end_time.group(1)) - now).total_seconds()
529572
if after <= 0:
530573
# No longer blocked
531574
reaction = sent_emoji

core/config.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ConfigManager(ConfigManagerABC):
1919

2020
# bot settings
2121
'main_category_id', 'disable_autoupdates', 'prefix', 'mention',
22-
'main_color', 'user_typing', 'mod_typing', 'account_age', 'reply_without_command',
22+
'main_color', 'user_typing', 'mod_typing', 'account_age', 'guild_age',
23+
'reply_without_command',
2324

2425
# logging
2526
'log_channel_id',
@@ -71,7 +72,7 @@ class ConfigManager(ConfigManagerABC):
7172
}
7273

7374
time_deltas = {
74-
'account_age'
75+
'account_age', 'guild_age'
7576
}
7677

7778
valid_keys = allowed_to_change_in_command | internal_keys | protected_keys

0 commit comments

Comments
 (0)