Skip to content

Instantly share code, notes, and snippets.

@acbart
Created February 2, 2022 20:26
Show Gist options
  • Select an option

  • Save acbart/643270b6a710a267dabcde89bdaf0fdb to your computer and use it in GitHub Desktop.

Select an option

Save acbart/643270b6a710a267dabcde89bdaf0fdb to your computer and use it in GitHub Desktop.
Sage extension to render LaTeX
import { Message, MessageEmbed } from 'discord.js';
import moment from 'moment';
import fetch from 'node-fetch';
import { Command } from '@lib/types/Command';
export default class extends Command {
description = 'Render some LaTeX as a PNG';
usage = '[latex]';
extendedHelp = 'Type whatever LaTeX you want.';
latexAPI = 'https://latex.codecogs.com/gif.latex?\bg_white';
async run(msg: Message, [latex]: string[]): Promise<Message> {
const url: string = `${latexAPI}${encodeURIComponent(}`;
const result = await fetch(url);
if (result.code !== 200) {
return msg.channel.send(`Invalid LaTeX ${result.message}`);
}
return msg.channel.send({ embeds: [this.createImageEmbed(comic)] });
}
createImageEmbed(image: string, originalLatex: string): MessageEmbed {
return new MessageEmbed()
.setColor('GREYPLE')
.setDescription(originalLatex)
.setImage(image)
.setTimestamp();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment