Skip to content

Commit

Permalink
allow mentionables for remind, prevent overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayerch1 committed Aug 4, 2021
1 parent 4139e22 commit 1a4fc0e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ servers_bak.json
tmp/
data/db/**
data/db2/**
prometheus_data/**

# ---> Python
# Byte-compiled / optimized / DLL files
Expand Down
32 changes: 22 additions & 10 deletions Bot/ReminderModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ReminderModule(commands.Cog):
'\n'\
'\t/remind @User 1 mon What\'s up\n'\
'\t/remind @User 24 dec Merry Christmas\n'\
'\t/remind @User eoy Happy new year\n'\
'\t/remind @Role eoy Happy new year\n'\
'\n\n'\
'allowed absolutes are\n'\
'\t• eoy - remind at end of year\n'\
Expand All @@ -61,7 +61,7 @@ class ReminderModule(commands.Cog):
'natural dates are supported, you can try different formats\n'\
'\t• 5 jul, 5th july, july 5\n'\
'\t• 23 sept at 3pm, 23 sept at 15:00\n'\
'\t2050\n'\
'\t2030\n'\
'\tNote: the parser uses day-first and year-least\n'\
'\t (01/02/21 -> 1st January 2021)\n'\
'\n'\
Expand Down Expand Up @@ -156,7 +156,8 @@ async def print_reminder(self, rem: Reminder):
if perms.send_messages and perms.embed_links:
try:
# embed does not hold user mention
await channel.send(f'<@!{rem.target}>', embed=eb)
await channel.send(rem.target_mention or f'<@{rem.target}>', embed=eb,
allowed_mentions=discord.AllowedMentions.all())
return
except discord.errors.Forbidden:
pass
Expand Down Expand Up @@ -247,9 +248,17 @@ async def process_reminder(self, ctx, author, target, period, message):
rem.at = remind_at
rem.author = author.id
rem.target = target.id
rem.target_name = target.name
rem.created_at = utcnow
rem.last_msg_id = last_msg.id if last_msg else None

# everyone requires a special case
# as it cannot be mentioned by using the id
if ctx.guild and ctx.guild.default_role == target:
rem.target_mention = '@everyone'
else:
rem.target_mention = target.mention

# the id is required in case the users wishes to abort
rem_id = Connector.add_reminder(rem)

Expand All @@ -267,7 +276,7 @@ async def process_reminder(self, ctx, author, target, period, message):
if target == author:
out_str = f'Reminding you in `{delta_str}` at `{at_str}`.'
else:
out_str = f'Reminding {target.name} in `{delta_str}` at `{at_str}`.'
out_str = f'Reminding {rem.target_mention} in `{delta_str}` at `{at_str}`.'

if (remind_at-utcnow) < timedelta(minutes=5):
out_str += '\nCall `/reminder_list` to edit all pending reminders'
Expand All @@ -293,7 +302,8 @@ async def process_reminder(self, ctx, author, target, period, message):
action_row = manage_components.create_actionrow(*buttons)

# delta_to_str cannot take relative delta
msg = await ctx.send(out_str, delete_after=300, components=[action_row])
msg = await ctx.send(out_str, delete_after=300, components=[action_row],
allowed_mentions=discord.AllowedMentions.none())


# =====================
Expand Down Expand Up @@ -412,10 +422,10 @@ async def check_interval_cnt_before(self):
@cog_ext.cog_slash(name='remind', description='set a reminder for another user',
options=[
create_option(
name='user',
description='the user you want to remind',
name='target',
description='the user or role you want to remind',
required=True,
option_type=SlashCommandOptionType.USER
option_type=SlashCommandOptionType.MENTIONABLE
),
create_option(
name='time',
Expand All @@ -431,8 +441,10 @@ async def check_interval_cnt_before(self):
)
])
@commands.guild_only()
async def add_remind_user(self, ctx, user, time, message):
await self.process_reminder(ctx, ctx.author, user, time, message)
async def add_remind_user(self, ctx, target, time, message):

target_resolve = ctx.guild.get_member(int(target)) or ctx.guild.get_role(int(target))
await self.process_reminder(ctx, ctx.author, target_resolve, time, message)


@cog_ext.cog_slash(name='remindme', description='set a reminder after a certain time period',
Expand Down
17 changes: 10 additions & 7 deletions Bot/lib/Reminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, json = {}):
target = json.get('target', None)
self.target = int(target) if target else None

self.target_mention = json.get('target_mention', None)
self.target_name = json.get('target_name', None)

author = json.get('author', None)
self.author = int(author) if author else None

Expand Down Expand Up @@ -66,6 +69,8 @@ def _to_json(self):
d['g_id'] = str(self.g_id) if self.g_id else None
d['ch_id'] = str(self.ch_id) if self.ch_id else None
d['target'] = str(self.target) if self.target else None
d['target_mention'] = self.target_mention
d['target_name'] = self.target_name
d['author'] = str(self.author) if self.author else None
d['last_msg_id'] = str(self.last_msg_id) if self.last_msg_id else None

Expand All @@ -91,16 +96,16 @@ def get_string(self):
"""

if self.target == self.author:
out_str = f'<@!{self.target}> Reminder: {self.msg}'
out_str = self.target_mention or f'<@!{self.target}>'
out_str += f' Reminder: {self.msg}'
else:
out_str = f'<@!{self.target}> Reminder: {self.msg} (delivered by <@!{self.author}>)'
out_str = self.target_mention or f'<@!{self.target}>'
out_str += f' Reminder: {self.msg} (delivered by <@!{self.author}>)'

out_str += '\n||This reminder can be more beautiful with `Embed Links` permissions||'
return out_str




def _get_embed_body(self, title, author=None):
eb = discord.Embed(title=title, description=f'{self.msg}', color=0xffcc00)

Expand All @@ -115,7 +120,6 @@ def _get_embed_body(self, title, author=None):

return eb



def get_info_embed(self, tz_str='UTC'):
"""return info embed of this reminders
Expand All @@ -130,7 +134,7 @@ def get_info_embed(self, tz_str='UTC'):


if self.author != self.target:
eb.add_field(name='Target user', value=f'<@!{self.target}>')
eb.add_field(name='Target user', value=self.target_name or f'<@!{self.target}>')

if self.at:
if tz_str == 'UTC':
Expand Down Expand Up @@ -183,7 +187,6 @@ async def get_embed(self, client, is_dm=False):
return eb



class IntervalReminder(Reminder):

def __init__(self, json = {}):
Expand Down
23 changes: 0 additions & 23 deletions Bot/lib/Server.py

This file was deleted.

12 changes: 8 additions & 4 deletions Bot/lib/input_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ def _parse_relative(args, utcnow):
info = info + f'• Ignoring {arg}, as required numerical part is missing\n'
else:
if arg[1].startswith('y'):
intvl = relativedelta(years=arg[0])
intvl = relativedelta(years=arg[0])

elif arg[1].startswith('mo'):
intvl = relativedelta(months=arg[0])
intvl = relativedelta(months=arg[0])

elif arg[1].startswith('w'):
intvl = relativedelta(weeks=arg[0])
Expand All @@ -222,8 +222,12 @@ def _parse_relative(args, utcnow):

total_intvl += intvl

try:
eval_interval = utcnow + total_intvl
except Exception as e:
return (utcnow, 'The interval is out of bounds/not a number')

return (utcnow + total_intvl, info)
return (eval_interval, info)


def _parse_iso(input, utcnow, display_tz):
Expand Down
16 changes: 14 additions & 2 deletions Bot/remindmeBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

token = open('token.txt', 'r').read()
client = commands.Bot(command_prefix='/', description='Reminding you whenever you want', help_command=None, intents=intents)
slash = SlashCommand(client, sync_commands=False)
slash = SlashCommand(client, sync_commands=True)

FEEDBACK_CHANNEL = 872104333007785984
FEEDBACK_MENTION = 872107119988588566
Expand Down Expand Up @@ -426,10 +426,22 @@ async def on_guild_remove(guild):
Analytics.guild_removed()
print(f'removed from guild (total count: {len(client.guilds)})')


@client.event
async def on_guild_join(guild):
Analytics.guild_added()
print(f'added to guild (total count: {len(client.guilds)})')
guild_cnt = len(client.guilds)
print(f'added to guild (total count: {guild_cnt})')

if not guild.system_channel:
return

eb = discord.Embed(title=f'You\'re the {guild_cnt}th server I\'ve been added to',
description='Here\'s a cool gif, just for you')
eb.set_image(url='https://media.giphy.com/media/kyLYXonQYYfwYDIeZl/giphy.gif')

if guild_cnt == 500 or (guild_cnt%1000) == 0:
await guild.system_channel.send(embed=eb)


def main():
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The bot supports the full `rfc5545`-spec (the smallest interval is limited to da
|Commands||
|---|---|
|```remindme <time> <message>``` | reminds you after the given `<time>` period|
|```remind <user> <time> <message>``` | reminds another user after the given `<time>` period|
|```remind <mentionable> <time> <message>``` | reminds another user/role after the given `<time>` period|
|```reminder_list``` | manage all your reminders for this server (interactive DM) |
|```timezone [set/get] <string>``` | set the timezone of your server, used for end-of-day calculation, defaults to UTC|

Expand All @@ -48,8 +48,8 @@ The bot supports the full `rfc5545`-spec (the smallest interval is limited to da
/remindme 2021-09-02T12:25:00+02:00 iso is cool
/remind @User 1 mon What's up
/remind @User 24 dec Merry Christmas
/remind @User eoy Happy new year
/remind @Role 24 dec Merry Christmas
/remind @everyone eoy Happy new year
```

## Time parsing
Expand Down Expand Up @@ -82,9 +82,9 @@ At the moment, different parameters cannot be combined.
dates are supported, you can try different formats
• 5 jul, 5th july, july 5
• 23 sept at 3pm, 23 sept at 15:00
2050
2030
tNote: the parser uses day-first and year-least
Note: the parser uses day-first and year-least
(01/02/21 -> 1st January)
the reminder can occur as much as 1 minute delayed
Expand Down
7 changes: 4 additions & 3 deletions prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
global:
scrape_interval: 15m # By default, scrape targets every 15 minutes.
scrape_interval: 4m # By default, scrape targets every 15 minutes.
evaluation_interval: 4m


scrape_configs:
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every day.
scrape_interval: 1d
scrape_interval: 12h

static_configs:
- targets: ['localhost:9090']

- job_name: 'remindmeBot'
# Override the global default and scrape targets from this job every 15 minutes.
scrape_interval: 15m
scrape_interval: 4m
static_configs:
- targets: ['bot:9091']

0 comments on commit 1a4fc0e

Please sign in to comment.