Copy Discord emojis/stickers to Matrix
This commit is contained in:
		
							parent
							
								
									d81aa51787
								
							
						
					
					
						commit
						807ab899be
					
				
					 4 changed files with 121 additions and 3 deletions
				
			
		
							
								
								
									
										82
									
								
								d2m/converters/expression.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								d2m/converters/expression.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,82 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const assert = require("assert").strict
 | 
			
		||||
const DiscordTypes = require("discord-api-types/v10")
 | 
			
		||||
 | 
			
		||||
const passthrough = require("../../passthrough")
 | 
			
		||||
const {discord, sync, db, select} = passthrough
 | 
			
		||||
/** @type {import("../../matrix/file")} */
 | 
			
		||||
const file = sync.require("../../matrix/file")
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {DiscordTypes.APIEmoji[]} emojis
 | 
			
		||||
 */
 | 
			
		||||
async function emojisToState(emojis) {
 | 
			
		||||
	const result = {
 | 
			
		||||
		pack: {
 | 
			
		||||
			display_name: "Discord Emojis",
 | 
			
		||||
			usage: ["emoticon"] // we'll see...
 | 
			
		||||
		},
 | 
			
		||||
		images: {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	await Promise.all(emojis.map(emoji =>
 | 
			
		||||
		// the homeserver can probably cope with doing this in parallel
 | 
			
		||||
		file.uploadDiscordFileToMxc(file.emoji(emoji.id, emoji.animated)).then(url => {
 | 
			
		||||
			result.images[emoji.name] = {
 | 
			
		||||
				info: {
 | 
			
		||||
					mimetype: emoji.animated ? "image/gif" : "image/png"
 | 
			
		||||
				},
 | 
			
		||||
				url
 | 
			
		||||
			}
 | 
			
		||||
		}).catch(e => {
 | 
			
		||||
			if (e.data.errcode === "M_TOO_LARGE") { // Lol.
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			console.error(`Trying to handle emoji ${emoji.name} (${emoji.id}), but...`)
 | 
			
		||||
			throw e
 | 
			
		||||
		})
 | 
			
		||||
	))
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @param {DiscordTypes.APISticker[]} stickers
 | 
			
		||||
 */
 | 
			
		||||
async function stickersToState(stickers) {
 | 
			
		||||
	const result = {
 | 
			
		||||
		pack: {
 | 
			
		||||
			display_name: "Discord Stickers",
 | 
			
		||||
			usage: ["sticker"] // we'll see...
 | 
			
		||||
		},
 | 
			
		||||
		images: {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	const shortcodes = []
 | 
			
		||||
	await Promise.all(stickers.map(sticker =>
 | 
			
		||||
		// the homeserver can probably cope with doing this in parallel
 | 
			
		||||
		file.uploadDiscordFileToMxc(file.sticker(sticker)).then(url => {
 | 
			
		||||
 | 
			
		||||
			/** @type {string | undefined} */
 | 
			
		||||
			let body = sticker.name
 | 
			
		||||
			if (sticker && sticker.description) body += ` - ${sticker.description}`
 | 
			
		||||
			if (!body) body = undefined
 | 
			
		||||
 | 
			
		||||
			let shortcode = sticker.name.toLowerCase().replace(/[^a-zA-Z0-9-_]/g, "-").replace(/^-|-$/g, "").replace(/--+/g, "-")
 | 
			
		||||
			while (shortcodes.includes(shortcode)) shortcode = shortcode + "~"
 | 
			
		||||
			shortcodes.push(shortcode)
 | 
			
		||||
 | 
			
		||||
			result.images[shortcodes] = {
 | 
			
		||||
				info: {
 | 
			
		||||
					mimetype: file.stickerFormat.get(sticker.format_type)?.mime || "image/png"
 | 
			
		||||
				},
 | 
			
		||||
				body,
 | 
			
		||||
				url
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	))
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports.emojisToState = emojisToState
 | 
			
		||||
module.exports.stickersToState = stickersToState
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue