mirror of
				https://github.com/miaowware/qrm2.git
				synced 2025-10-26 01:40:20 -04:00 
			
		
		
		
	implement ?tex command and add rTeX server option
This commit is contained in:
		
							parent
							
								
									190eef7fa9
								
							
						
					
					
						commit
						137b275c78
					
				| @ -5,7 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ## [Unreleased] | ## [Unreleased] | ||||||
| 
 | ### Added | ||||||
|  | - `?tex` command to render a LaTeX expression. | ||||||
|  | - Configuration option to use another rTeX instance for `?tex`. | ||||||
| 
 | 
 | ||||||
| ## [2.6.0] - 2021-03-18 | ## [2.6.0] - 2021-03-18 | ||||||
| ### Added | ### Added | ||||||
|  | |||||||
							
								
								
									
										65
									
								
								exts/tex.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								exts/tex.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | |||||||
|  | """ | ||||||
|  | TeX extension for qrm | ||||||
|  | --- | ||||||
|  | Copyright (C) 2021 thxo | ||||||
|  | 
 | ||||||
|  | This file is part of qrm2 and is released under the terms of | ||||||
|  | the GNU General Public License, version 2. | ||||||
|  | """ | ||||||
|  | 
 | ||||||
|  | import aiohttp | ||||||
|  | from io import BytesIO | ||||||
|  | from urllib.parse import urljoin | ||||||
|  | 
 | ||||||
|  | import discord | ||||||
|  | import discord.ext.commands as commands | ||||||
|  | 
 | ||||||
|  | import common as cmn | ||||||
|  | import data.options as opt | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class TexCog(commands.Cog): | ||||||
|  |     def __init__(self, bot: commands.Bot): | ||||||
|  |         self.bot = bot | ||||||
|  |         self.session = aiohttp.ClientSession(connector=bot.qrm.connector) | ||||||
|  |         self.rtex_instance = getattr(opt, "rtex_instance", "https://rtex.probablyaweb.site/") | ||||||
|  |         with open(cmn.paths.resources / "template.1.tex") as latex_template: | ||||||
|  |             self.template = latex_template.read() | ||||||
|  | 
 | ||||||
|  |     @commands.command(name="tex", aliases=["latex"], category=cmn.cat.fun) | ||||||
|  |     async def tex(self, ctx: commands.Context, *, expr: str): | ||||||
|  |         """Renders a LaTeX expression.""" | ||||||
|  |         payload = { | ||||||
|  |             "format": "png", | ||||||
|  |             "code": self.template.replace("#CONTENT#", expr), | ||||||
|  |             "quality": 50 | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         with ctx.typing(): | ||||||
|  |             # ask rTeX to render our expression | ||||||
|  |             async with self.session.post(urljoin(self.rtex_instance, "api/v2"), json=payload) as r: | ||||||
|  |                 if r.status != 200: | ||||||
|  |                     raise cmn.BotHTTPError(r) | ||||||
|  | 
 | ||||||
|  |                 render_result = await r.json() | ||||||
|  |                 if render_result["status"] != "success": | ||||||
|  |                     embed = cmn.embed_factory(ctx) | ||||||
|  |                     embed.title = "LaTeX Rendering Failed!" | ||||||
|  |                     embed.description = render_result.get("description", "Unknown error") | ||||||
|  |                     embed.colour = cmn.colours.bad | ||||||
|  |                     await ctx.send(embed=embed) | ||||||
|  |                     return | ||||||
|  | 
 | ||||||
|  |             # if rendering went well, download the file given in the response | ||||||
|  |             async with self.session.get(urljoin(self.rtex_instance, "api/v2/" + render_result["filename"])) as r: | ||||||
|  |                 png_buffer = BytesIO(await r.read()) | ||||||
|  | 
 | ||||||
|  |             embed = cmn.embed_factory(ctx) | ||||||
|  |             embed.title = "LaTeX Expression" | ||||||
|  |             embed.description = "Rendered by [rTeX](https://rtex.probablyaweb.site/)." | ||||||
|  |             embed.set_image(url="attachment://tex.png") | ||||||
|  |             await ctx.send(file=discord.File(png_buffer, "tex.png"), embed=embed) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def setup(bot: commands.Bot): | ||||||
|  |     bot.add_cog(TexCog(bot)) | ||||||
| @ -41,6 +41,7 @@ exts = [ | |||||||
|     "morse", |     "morse", | ||||||
|     "qrz", |     "qrz", | ||||||
|     "study", |     "study", | ||||||
|  |     "tex", | ||||||
|     "weather", |     "weather", | ||||||
|     "dbconv", |     "dbconv", | ||||||
|     "propagation", |     "propagation", | ||||||
| @ -86,3 +87,6 @@ msg_reacts = {} | |||||||
| 
 | 
 | ||||||
| # A :pika: emote's ID, None for no emote :c | # A :pika: emote's ID, None for no emote :c | ||||||
| pika = 658733876176355338 | pika = 658733876176355338 | ||||||
|  | 
 | ||||||
|  | # Base URL to a deployment of rTeX, which performs LaTeX rendering. | ||||||
|  | rtex_instance = "https://rtex.probablyaweb.site/" | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user