mirror of
				https://github.com/Ryujinx/ryuko-ng.git
				synced 2025-11-04 15:44:50 +00:00 
			
		
		
		
	remind: add remind, make crons more robust
This commit is contained in:
		
							parent
							
								
									a83c0ed94b
								
							
						
					
					
						commit
						eac078e1b9
					
				| 
						 | 
					@ -85,6 +85,7 @@ Main goal of this project is to get Robocop functionality done, secondary goal i
 | 
				
			||||||
- [ ] New feature: Highlights (problematic words automatically get posted to modmail channel, relies on modmail)
 | 
					- [ ] New feature: Highlights (problematic words automatically get posted to modmail channel, relies on modmail)
 | 
				
			||||||
- [ ] Feature creep: Shortlink completion (gl/ao/etc)
 | 
					- [ ] Feature creep: Shortlink completion (gl/ao/etc)
 | 
				
			||||||
- [ ] Feature creep: Pleroma embedding
 | 
					- [ ] Feature creep: Pleroma embedding
 | 
				
			||||||
 | 
					- [x] Feature creep: Reminds
 | 
				
			||||||
- [x] A system for running jobs in background with an interval (will be called robocronp)
 | 
					- [x] A system for running jobs in background with an interval (will be called robocronp)
 | 
				
			||||||
- [x] Commands to list said jobs and remove them
 | 
					- [x] Commands to list said jobs and remove them
 | 
				
			||||||
- [x] New moderation feature: timemute (mute with time, relies on robocronp)
 | 
					- [x] New moderation feature: timemute (mute with time, relies on robocronp)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -56,6 +56,7 @@ initial_extensions = ['cogs.common',
 | 
				
			||||||
                      'cogs.lockdown',
 | 
					                      'cogs.lockdown',
 | 
				
			||||||
                      'cogs.legacy',
 | 
					                      'cogs.legacy',
 | 
				
			||||||
                      'cogs.links',
 | 
					                      'cogs.links',
 | 
				
			||||||
 | 
					                      'cogs.remind',
 | 
				
			||||||
                      'cogs.robocronp',
 | 
					                      'cogs.robocronp',
 | 
				
			||||||
                      'cogs.meme']
 | 
					                      'cogs.meme']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,5 @@
 | 
				
			||||||
import discord
 | 
					import discord
 | 
				
			||||||
import config
 | 
					import config
 | 
				
			||||||
import time
 | 
					 | 
				
			||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
from discord.ext import commands
 | 
					from discord.ext import commands
 | 
				
			||||||
from helpers.checks import check_if_staff
 | 
					from helpers.checks import check_if_staff
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										49
									
								
								cogs/remind.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								cogs/remind.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,49 @@
 | 
				
			||||||
 | 
					import discord
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					from discord.ext import commands
 | 
				
			||||||
 | 
					from helpers.robocronp import add_job, get_crontab
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Remind:
 | 
				
			||||||
 | 
					    def __init__(self, bot):
 | 
				
			||||||
 | 
					        self.bot = bot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @commands.command()
 | 
				
			||||||
 | 
					    async def remindlist(self, ctx):
 | 
				
			||||||
 | 
					        """Lists your reminders."""
 | 
				
			||||||
 | 
					        ctab = get_crontab()
 | 
				
			||||||
 | 
					        embed = discord.Embed(title=f"Active robocronp jobs")
 | 
				
			||||||
 | 
					        for jobtimestamp in ctab["remind"]:
 | 
				
			||||||
 | 
					            job_details = ctab["remind"][jobtimestamp][str(ctx.author.id)]
 | 
				
			||||||
 | 
					            expiry_timestr = datetime.utcfromtimestamp(int(jobtimestamp))\
 | 
				
			||||||
 | 
					                .strftime('%Y-%m-%d %H:%M:%S (UTC)')
 | 
				
			||||||
 | 
					            embed.add_field(name=f"Reminder for {expiry_timestr}",
 | 
				
			||||||
 | 
					                            value=f"Added on: {job_details['added']}, "
 | 
				
			||||||
 | 
					                            f"Text: {job_details['text']}",
 | 
				
			||||||
 | 
					                            inline=False)
 | 
				
			||||||
 | 
					        await ctx.send(embed=embed)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @commands.command()
 | 
				
			||||||
 | 
					    async def remind(self, ctx, when: str, *, text: str = "something"):
 | 
				
			||||||
 | 
					        """Reminds you about something."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expiry_timestamp = self.bot.parse_time(when)
 | 
				
			||||||
 | 
					        expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
 | 
				
			||||||
 | 
					        duration_text = self.bot.get_relative_timestamp(time_to=expiry_datetime,
 | 
				
			||||||
 | 
					                                                        include_to=True,
 | 
				
			||||||
 | 
					                                                        humanized=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        safe_text = self.bot.escape_message(str(text))
 | 
				
			||||||
 | 
					        added_on = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        add_job("remind",
 | 
				
			||||||
 | 
					                ctx.author.id,
 | 
				
			||||||
 | 
					                {"text": safe_text, "added": added_on},
 | 
				
			||||||
 | 
					                expiry_timestamp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await ctx.send(f"{ctx.author.mention}: I'll remind you in DMs about"
 | 
				
			||||||
 | 
					                       f" {safe_text} in {duration_text}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def setup(bot):
 | 
				
			||||||
 | 
					    bot.add_cog(Remind(bot))
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@ import asyncio
 | 
				
			||||||
import config
 | 
					import config
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import discord
 | 
					import discord
 | 
				
			||||||
 | 
					import traceback
 | 
				
			||||||
from discord.ext import commands
 | 
					from discord.ext import commands
 | 
				
			||||||
from helpers.robocronp import get_crontab, delete_job
 | 
					from helpers.robocronp import get_crontab, delete_job
 | 
				
			||||||
from helpers.restrictions import remove_restriction
 | 
					from helpers.restrictions import remove_restriction
 | 
				
			||||||
| 
						 | 
					@ -53,14 +54,17 @@ class Robocronp:
 | 
				
			||||||
        await ctx.send(f"{ctx.author.mention}: Deleted!")
 | 
					        await ctx.send(f"{ctx.author.mention}: Deleted!")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def do_jobs(self, ctab, jobtype, timestamp):
 | 
					    async def do_jobs(self, ctab, jobtype, timestamp):
 | 
				
			||||||
 | 
					        log_channel = self.bot.get_channel(config.log_channel)
 | 
				
			||||||
        for job_name in ctab[jobtype][timestamp]:
 | 
					        for job_name in ctab[jobtype][timestamp]:
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
                job_details = ctab[jobtype][timestamp][job_name]
 | 
					                job_details = ctab[jobtype][timestamp][job_name]
 | 
				
			||||||
                if jobtype == "unban":
 | 
					                if jobtype == "unban":
 | 
				
			||||||
                    target_user = await self.bot.get_user_info(job_name)
 | 
					                    target_user = await self.bot.get_user_info(job_name)
 | 
				
			||||||
                    target_guild = self.bot.get_guild(job_details["guild"])
 | 
					                    target_guild = self.bot.get_guild(job_details["guild"])
 | 
				
			||||||
                await target_guild.unban(target_user,
 | 
					 | 
				
			||||||
                                         reason="Robocronp: Timed ban expired.")
 | 
					 | 
				
			||||||
                    delete_job(timestamp, jobtype, job_name)
 | 
					                    delete_job(timestamp, jobtype, job_name)
 | 
				
			||||||
 | 
					                    await target_guild.unban(target_user,
 | 
				
			||||||
 | 
					                                             reason="Robocronp: Timed "
 | 
				
			||||||
 | 
					                                                    "ban expired.")
 | 
				
			||||||
                elif jobtype == "unmute":
 | 
					                elif jobtype == "unmute":
 | 
				
			||||||
                    remove_restriction(job_name, config.mute_role)
 | 
					                    remove_restriction(job_name, config.mute_role)
 | 
				
			||||||
                    target_guild = self.bot.get_guild(job_details["guild"])
 | 
					                    target_guild = self.bot.get_guild(job_details["guild"])
 | 
				
			||||||
| 
						 | 
					@ -70,9 +74,22 @@ class Robocronp:
 | 
				
			||||||
                                                     reason="Robocronp: Timed "
 | 
					                                                     reason="Robocronp: Timed "
 | 
				
			||||||
                                                            "mute expired.")
 | 
					                                                            "mute expired.")
 | 
				
			||||||
                    delete_job(timestamp, jobtype, job_name)
 | 
					                    delete_job(timestamp, jobtype, job_name)
 | 
				
			||||||
 | 
					                elif jobtype == "remind":
 | 
				
			||||||
 | 
					                    text = job_details["text"]
 | 
				
			||||||
 | 
					                    added_on = job_details["added"]
 | 
				
			||||||
 | 
					                    target = await self.bot.get_user_info(int(job_name))
 | 
				
			||||||
 | 
					                    if target:
 | 
				
			||||||
 | 
					                        await target.send("You asked to be reminded about"
 | 
				
			||||||
 | 
					                                          f" `{text}` on {added_on}.")
 | 
				
			||||||
 | 
					                    delete_job(timestamp, jobtype, job_name)
 | 
				
			||||||
 | 
					            except:
 | 
				
			||||||
 | 
					                # Don't kill cronjobs if something goes wrong.
 | 
				
			||||||
 | 
					                await log_channel.send("Crondo has errored: ```"
 | 
				
			||||||
 | 
					                                       f"{traceback.format_exc()}```")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def minutely(self):
 | 
					    async def minutely(self):
 | 
				
			||||||
        await self.bot.wait_until_ready()
 | 
					        await self.bot.wait_until_ready()
 | 
				
			||||||
 | 
					        log_channel = self.bot.get_channel(config.log_channel)
 | 
				
			||||||
        while not self.bot.is_closed():
 | 
					        while not self.bot.is_closed():
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                ctab = get_crontab()
 | 
					                ctab = get_crontab()
 | 
				
			||||||
| 
						 | 
					@ -83,11 +100,13 @@ class Robocronp:
 | 
				
			||||||
                            await self.do_jobs(ctab, jobtype, jobtimestamp)
 | 
					                            await self.do_jobs(ctab, jobtype, jobtimestamp)
 | 
				
			||||||
            except:
 | 
					            except:
 | 
				
			||||||
                # Don't kill cronjobs if something goes wrong.
 | 
					                # Don't kill cronjobs if something goes wrong.
 | 
				
			||||||
                pass
 | 
					                await log_channel.send("Cron-minutely has errored: ```"
 | 
				
			||||||
 | 
					                                       f"{traceback.format_exc()}```")
 | 
				
			||||||
            await asyncio.sleep(60)
 | 
					            await asyncio.sleep(60)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def hourly(self):
 | 
					    async def hourly(self):
 | 
				
			||||||
        await self.bot.wait_until_ready()
 | 
					        await self.bot.wait_until_ready()
 | 
				
			||||||
 | 
					        log_channel = self.bot.get_channel(config.log_channel)
 | 
				
			||||||
        while not self.bot.is_closed():
 | 
					        while not self.bot.is_closed():
 | 
				
			||||||
            # Your stuff that should run at boot
 | 
					            # Your stuff that should run at boot
 | 
				
			||||||
            # and after that every hour goes here
 | 
					            # and after that every hour goes here
 | 
				
			||||||
| 
						 | 
					@ -96,7 +115,8 @@ class Robocronp:
 | 
				
			||||||
                await self.send_data()
 | 
					                await self.send_data()
 | 
				
			||||||
            except:
 | 
					            except:
 | 
				
			||||||
                # Don't kill cronjobs if something goes wrong.
 | 
					                # Don't kill cronjobs if something goes wrong.
 | 
				
			||||||
                pass
 | 
					                await log_channel.send("Cron-hourly has errored: ```"
 | 
				
			||||||
 | 
					                                       f"{traceback.format_exc()}```")
 | 
				
			||||||
            # Your stuff that should run an hour after boot
 | 
					            # Your stuff that should run an hour after boot
 | 
				
			||||||
            # and after that every hour goes here
 | 
					            # and after that every hour goes here
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue