mirror of
				https://github.com/osmarks/autobotrobot
				synced 2025-10-31 07:53:00 +00:00 
			
		
		
		
	general changes, fix webhook rate limit incursions
This commit is contained in:
		| @@ -4,6 +4,7 @@ import random | |||||||
| from numpy.random import default_rng | from numpy.random import default_rng | ||||||
| import re | import re | ||||||
| import aiohttp | import aiohttp | ||||||
|  | import subprocess | ||||||
| import discord.ext.commands as commands | import discord.ext.commands as commands | ||||||
|  |  | ||||||
| import tio | import tio | ||||||
| @@ -11,7 +12,7 @@ import util | |||||||
|  |  | ||||||
| cleaner = commands.clean_content() | cleaner = commands.clean_content() | ||||||
| def clean(ctx, text): | def clean(ctx, text): | ||||||
|     return cleaner.convert(self, ctx, text) |     return cleaner.convert(ctx, text) | ||||||
|  |  | ||||||
| class GeneralCommands(commands.Cog): | class GeneralCommands(commands.Cog): | ||||||
|     def __init__(self, bot): |     def __init__(self, bot): | ||||||
| @@ -21,7 +22,7 @@ class GeneralCommands(commands.Cog): | |||||||
|     @commands.command(help="Gives you a random fortune as generated by `fortune`.") |     @commands.command(help="Gives you a random fortune as generated by `fortune`.") | ||||||
|     async def fortune(self, ctx): |     async def fortune(self, ctx): | ||||||
|         proc = await asyncio.create_subprocess_exec("fortune", stdout=subprocess.PIPE) |         proc = await asyncio.create_subprocess_exec("fortune", stdout=subprocess.PIPE) | ||||||
|         stdout = (await proc.communicate()).decode("utf-8") |         stdout = (await proc.communicate())[0].decode("utf-8") | ||||||
|         await ctx.send(stdout) |         await ctx.send(stdout) | ||||||
|  |  | ||||||
|     @commands.command(help="Generates an apioform type.") |     @commands.command(help="Generates an apioform type.") | ||||||
| @@ -78,12 +79,12 @@ class GeneralCommands(commands.Cog): | |||||||
|  |  | ||||||
|     @commands.command(rest_is_raw=True, help="Execute provided code (in a codeblock) using TIO.run.") |     @commands.command(rest_is_raw=True, help="Execute provided code (in a codeblock) using TIO.run.") | ||||||
|     async def exec(self, ctx, *, arg): |     async def exec(self, ctx, *, arg): | ||||||
|         match = re.match(EXEC_REGEX, arg, flags=re.DOTALL) |         match = re.match(GeneralCommands.EXEC_REGEX, arg, flags=re.DOTALL) | ||||||
|         if match == None: |         if match == None: | ||||||
|             await ctx.send(embed=util.error_embed("Invalid format. Expected a codeblock.")) |             await ctx.send(embed=util.error_embed("Invalid format. Expected a codeblock.")) | ||||||
|             return |             return | ||||||
|         flags_raw = match.group(1) |         flags_raw = match.group(1) | ||||||
|         flags = exec_flag_parser.parse_args(flags_raw.split()) |         flags = GeneralCommands.exec_flag_parser.parse_args(flags_raw.split()) | ||||||
|         lang = flags.language or match.group(2) |         lang = flags.language or match.group(2) | ||||||
|         if not lang: |         if not lang: | ||||||
|             await ctx.send(embed=util.error_embed("No language specified. Use the -L flag or add a language to your codeblock.")) |             await ctx.send(embed=util.error_embed("No language specified. Use the -L flag or add a language to your codeblock.")) | ||||||
| @@ -137,7 +138,7 @@ AutoBotRobot is operated by gollark/osmarks. | |||||||
|         rolls = [ random.randint(1, x) for _ in range(n) ] |         rolls = [ random.randint(1, x) for _ in range(n) ] | ||||||
|         await ctx.send(f"{sum(rolls)} ({' '.join(map(str, sorted(rolls)))})") |         await ctx.send(f"{sum(rolls)} ({' '.join(map(str, sorted(rolls)))})") | ||||||
|  |  | ||||||
|     def weight(thing): |     def weight(self, thing): | ||||||
|         lthing = thing.lower() |         lthing = thing.lower() | ||||||
|         weight = 1.0 |         weight = 1.0 | ||||||
|         if lthing == "c": weight *= 0.3 |         if lthing == "c": weight *= 0.3 | ||||||
| @@ -166,7 +167,7 @@ AutoBotRobot is operated by gollark/osmarks. | |||||||
|             return |             return | ||||||
|  |  | ||||||
|         # because of python weirdness, using sum() on the bare map iterator consumes it, which means we have to actually make a list |         # because of python weirdness, using sum() on the bare map iterator consumes it, which means we have to actually make a list | ||||||
|         weights = list(map(weight, choices)) |         weights = list(map(self.weight, choices)) | ||||||
|  |  | ||||||
|         if samples == 1: return await ctx.send(random.choices(choices, weights=weights, k=1)[0]) |         if samples == 1: return await ctx.send(random.choices(choices, weights=weights, k=1)[0]) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -44,6 +44,7 @@ def find_all_destinations(source): | |||||||
|         visited.add(current) |         visited.add(current) | ||||||
|     return visited |     return visited | ||||||
|  |  | ||||||
|  | # 5 messages per 5 seconds from each input channel | ||||||
| RATE = 10.0 | RATE = 10.0 | ||||||
| PER = 5000000.0 # µs | PER = 5000000.0 # µs | ||||||
|  |  | ||||||
|   | |||||||
| @@ -69,6 +69,14 @@ class Telephone(commands.Cog): | |||||||
|         self.webhooks = {} |         self.webhooks = {} | ||||||
|         self.bot = bot |         self.bot = bot | ||||||
|         self.unlisten = eventbus.add_listener("discord", self.on_bridge_message) |         self.unlisten = eventbus.add_listener("discord", self.on_bridge_message) | ||||||
|  |         self.webhook_queue = asyncio.Queue(50) | ||||||
|  |         self.webhook_queue_handler_task = asyncio.create_task(self.send_webhooks()) | ||||||
|  |  | ||||||
|  |     async def send_webhooks(self): | ||||||
|  |         while True: | ||||||
|  |             webhook, content, username, avatar_url = await self.webhook_queue.get() | ||||||
|  |             wh_obj = discord.Webhook.from_url(webhook, adapter=discord.AsyncWebhookAdapter(self.bot.http._HTTPClient__session)) | ||||||
|  |             await wh_obj.send(content=content, username=username, avatar_url=avatar_url, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) | ||||||
|  |  | ||||||
|     async def initial_load_webhooks(self): |     async def initial_load_webhooks(self): | ||||||
|         rows = await self.bot.database.execute_fetchall("SELECT * FROM discord_webhooks") |         rows = await self.bot.database.execute_fetchall("SELECT * FROM discord_webhooks") | ||||||
| @@ -81,10 +89,11 @@ class Telephone(commands.Cog): | |||||||
|         if channel: |         if channel: | ||||||
|             webhook = self.webhooks.get(channel_id) |             webhook = self.webhooks.get(channel_id) | ||||||
|             if webhook: |             if webhook: | ||||||
|                 wh_obj = discord.Webhook.from_url(webhook, adapter=discord.AsyncWebhookAdapter(self.bot.http._HTTPClient__session)) |                 try: | ||||||
|                 await wh_obj.send( |                     self.webhook_queue.put_nowait((webhook, render_formatting(channel, msg.message)[:2000], msg.author.name, msg.author.avatar_url)) | ||||||
|                     content=render_formatting(channel, msg.message)[:2000], username=msg.author.name, avatar_url=msg.author.avatar_url, |                 except asyncio.QueueFull: | ||||||
|                     allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) |                     text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}" | ||||||
|  |                     await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) | ||||||
|             else: |             else: | ||||||
|                 text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}" |                 text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}" | ||||||
|                 await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) |                 await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) | ||||||
| @@ -102,6 +111,7 @@ class Telephone(commands.Cog): | |||||||
|  |  | ||||||
|     def cog_unload(self): |     def cog_unload(self): | ||||||
|         self.unlisten() |         self.unlisten() | ||||||
|  |         self.webhook_queue_handler_task.cancel() | ||||||
|  |  | ||||||
|     # ++tel commands |     # ++tel commands | ||||||
|  |  | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ async def run(http_session, lang, code): | |||||||
|     real_lang = aliases.get(lang, lang) |     real_lang = aliases.get(lang, lang) | ||||||
|     req = pytio.TioRequest(real_lang, code) |     req = pytio.TioRequest(real_lang, code) | ||||||
|     res = await (await http_session.post("https://tio.run/cgi-bin/run/api/", data=req.as_deflated_bytes(), timeout=65)).text() |     res = await (await http_session.post("https://tio.run/cgi-bin/run/api/", data=req.as_deflated_bytes(), timeout=65)).text() | ||||||
|     split = list(filter(lambda x: x != "\n" and x != "", content.split(content[:16]))) |     split = list(filter(lambda x: x != "\n" and x != "", res.split(res[:16]))) | ||||||
|     if len(split) == 1: |     if len(split) == 1: | ||||||
|         return False, real_lang, split[0], None |         return False, real_lang, split[0], None | ||||||
|     else: |     else: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user