From 12d298c48a580a4060ab8664a125395d1e3b2650 Mon Sep 17 00:00:00 2001 From: Jersey Date: Mon, 6 May 2024 13:47:48 -0400 Subject: [PATCH] remove most references to platform(s) and simplify err and message types --- packages/bolt-discord/conv.ts | 23 ++++---- packages/bolt-discord/mod.ts | 20 +++---- packages/bolt-discord/to.ts | 8 +-- packages/bolt-guilded/deps.ts | 1 - packages/bolt-guilded/guilded.ts | 12 ++--- packages/bolt-guilded/messages.ts | 14 ++--- packages/bolt-guilded/mod.ts | 12 ++--- packages/bolt-matrix/deps.ts | 4 +- packages/bolt-matrix/events.ts | 16 +++--- packages/bolt-matrix/mod.ts | 54 ++++++++----------- packages/bolt-revolt/messages.ts | 22 ++++---- packages/bolt-revolt/mod.ts | 22 ++++---- packages/lightning/README.md | 22 ++++---- packages/lightning/mod.ts | 2 +- .../src/bridges/command_functions.ts | 16 +++--- .../lightning/src/bridges/handle_message.ts | 41 +++++++------- packages/lightning/src/bridges/mod.ts | 13 +++-- packages/lightning/src/cli/migrations.ts | 4 +- packages/lightning/src/lightning.ts | 4 +- packages/lightning/src/migrations.ts | 14 +++-- packages/lightning/src/plugins.ts | 8 +-- .../lightning/src/tests/error_handling.ts | 8 +-- packages/lightning/src/tests/migrations.ts | 10 ++-- packages/lightning/src/tests/utils.ts | 8 +-- packages/lightning/src/types.ts | 51 +++++++----------- packages/lightning/src/utils.ts | 21 ++++---- 26 files changed, 193 insertions(+), 237 deletions(-) diff --git a/packages/bolt-discord/conv.ts b/packages/bolt-discord/conv.ts index 7279a99..4dd03dc 100644 --- a/packages/bolt-discord/conv.ts +++ b/packages/bolt-discord/conv.ts @@ -25,10 +25,10 @@ type discord_message = Omit; type webhook_message = wh_query & wh_token & { files?: RawFile[]; wait: true }; export async function to_discord( - message: message, + message: message, replied_message?: discord_message ): Promise { - if (message.replytoid && replied_message) { + if (message.reply_id && replied_message) { if (!message.embeds) message.embeds = []; message.embeds.push( { @@ -46,7 +46,8 @@ export async function to_discord( ...(replied_message.embeds || []).map(i => { return { ...i, - timestamp: i.timestamp ? Number(i.timestamp) : undefined + timestamp: i.timestamp ? Number(i.timestamp) : undefined, + video : i.video ? { ...i.video, url: i.video.url || "" } : undefined }; }) ); @@ -80,7 +81,7 @@ export async function to_discord( export async function to_core( api: API, message: discord_message -): Promise> { +): Promise { if (message.flags && message.flags & 128) message.content = 'Loading...'; if (message.type === 7) message.content = '*joined on discord*'; if (message.sticker_items) { @@ -132,7 +133,7 @@ export async function to_core( timestamp: i.timestamp ? Number(i.timestamp) : undefined }; }), - reply: async (msg: message) => { + reply: async (msg: message) => { if (!data.author.id || data.author.id == '') return; await api.channels.createMessage(message.channel_id, { ...(await to_discord(msg)), @@ -141,11 +142,7 @@ export async function to_core( } }); }, - platform: { - name: 'bolt-discord', - message, - webhookid: message.webhook_id - }, + plugin: 'bolt-discord', attachments: message.attachments?.map(i => { return { file: i.url, @@ -154,9 +151,9 @@ export async function to_core( size: i.size / 1000000 }; }), - replytoid: message.referenced_message?.id + reply_id: message.referenced_message?.id }; - return data; + return data as message; } export function to_command(interaction: { api: API; data: APIInteraction }) { @@ -180,7 +177,7 @@ export function to_command(interaction: { api: API; data: APIInteraction }) { ); }, channel: interaction.data.channel.id, - platform: 'bolt-discord', + plugin: 'bolt-discord', opts, timestamp: to_instant(interaction.data.id) } as command_arguments; diff --git a/packages/bolt-discord/mod.ts b/packages/bolt-discord/mod.ts index 855e393..c91a906 100644 --- a/packages/bolt-discord/mod.ts +++ b/packages/bolt-discord/mod.ts @@ -61,7 +61,7 @@ export class discord_plugin extends plugin { this.bot.on(GatewayDispatchEvents.InteractionCreate, interaction => { const cmd = conv.to_command(interaction); - if (cmd) this.emit('create_command', cmd); + if (cmd) this.lightning.run_command(cmd); }); } @@ -86,28 +86,24 @@ export class discord_plugin extends plugin { } create_message( - msg: message, + msg: message, bridge: to.channel, _?: undefined, - replytoid?: string + reply_id?: string ): Promise { - return to.send_to_discord(this.bot.api, msg, bridge, _, replytoid); + return to.send_to_discord(this.bot.api, msg, bridge, _, reply_id); } edit_message( - msg: message, + msg: message, bridge: to.channel, edit_id: string, - replytoid?: string + reply_id?: string ): Promise { - return to.send_to_discord(this.bot.api, msg, bridge, edit_id, replytoid); + return to.send_to_discord(this.bot.api, msg, bridge, edit_id, reply_id); } - delete_message( - _msg: deleted_message, - bridge: to.channel, - id: string - ) { + delete_message(_msg: deleted_message, bridge: to.channel, id: string) { return to.delete_on_discord(this.bot.api, bridge, id); } } diff --git a/packages/bolt-discord/to.ts b/packages/bolt-discord/to.ts index 4ad6473..3627d33 100644 --- a/packages/bolt-discord/to.ts +++ b/packages/bolt-discord/to.ts @@ -15,16 +15,16 @@ export async function webhook_on_discord(api: API, channel: string) { export async function send_to_discord( api: API, - message: message, + message: message, channel: channel, edit_id?: string, - replytoid?: string + reply_id?: string ) { let replied_message; - if (replytoid) { + if (reply_id) { try { - replied_message = await api.channels.getMessage(channel.id, replytoid); + replied_message = await api.channels.getMessage(channel.id, reply_id); } catch { // safe to ignore } diff --git a/packages/bolt-guilded/deps.ts b/packages/bolt-guilded/deps.ts index 0747b04..d96fb06 100644 --- a/packages/bolt-guilded/deps.ts +++ b/packages/bolt-guilded/deps.ts @@ -14,4 +14,3 @@ export { type lightning, type message } from '../lightning/mod.ts'; - diff --git a/packages/bolt-guilded/guilded.ts b/packages/bolt-guilded/guilded.ts index 9dbeaf3..cadba6c 100644 --- a/packages/bolt-guilded/guilded.ts +++ b/packages/bolt-guilded/guilded.ts @@ -60,7 +60,7 @@ export async function create_webhook( type guilded_msg = RESTPostWebhookBody & { replyMessageIds?: string[] }; export async function convert_msg( - msg: message, + msg: message, channel?: string, plugin?: guilded_plugin ): Promise { @@ -74,7 +74,7 @@ export async function convert_msg( ] } as guilded_msg; - if (msg.replytoid) message.replyMessageIds = [msg.replytoid]; + if (msg.reply_id) message.replyMessageIds = [msg.reply_id]; if (msg.attachments?.length) { if (!message.embeds) message.embeds = []; @@ -94,7 +94,7 @@ export async function convert_msg( return message; } -function get_valid_username(msg: message) { +function get_valid_username(msg: message) { function valid(e: string) { if (!e || e.length === 0 || e.length > 32) return false; return /^[a-zA-Z0-9_ ()-]*$/gms.test(e); @@ -110,15 +110,15 @@ function get_valid_username(msg: message) { } async function get_reply_embeds( - msg: message, + msg: message, channel?: string, plugin?: guilded_plugin ) { - if (!msg.replytoid || !channel || !plugin) return []; + if (!msg.reply_id || !channel || !plugin) return []; try { const msg_replied_to = await plugin.bot.messages.fetch( channel, - msg.replytoid + msg.reply_id ); let author; if (!msg_replied_to.createdByWebhookId) { diff --git a/packages/bolt-guilded/messages.ts b/packages/bolt-guilded/messages.ts index 371ccbe..4aa3d5b 100644 --- a/packages/bolt-guilded/messages.ts +++ b/packages/bolt-guilded/messages.ts @@ -5,7 +5,7 @@ import { guilded_plugin } from './mod.ts'; export async function tocore( message: Message, plugin: guilded_plugin -): Promise | undefined> { +): Promise { if (!message.serverId) return; let author; if (!message.createdByWebhookId && message.authorId !== 'Ann6LewA') { @@ -33,7 +33,7 @@ export async function tocore( name: embed.author.name || 'embed author', iconUrl: embed.author.iconURL || undefined, url: embed.author.url || undefined - } + } : undefined, image: embed.image || undefined, thumbnail: embed.thumbnail || undefined, @@ -52,15 +52,11 @@ export async function tocore( video: embed.video || undefined }; }), - platform: { - name: 'bolt-guilded', - message, - webhookid: message.createdByWebhookId || undefined - }, - reply: async (msg: message) => { + plugin: 'bolt-guilded', + reply: async (msg: message) => { await message.reply(await convert_msg(msg)); }, content: update_content, - replytoid: message.isReply ? message.replyMessageIds[0] : undefined + reply_id: message.isReply ? message.replyMessageIds[0] : undefined }; } diff --git a/packages/bolt-guilded/mod.ts b/packages/bolt-guilded/mod.ts index ea9e5e6..257bb95 100644 --- a/packages/bolt-guilded/mod.ts +++ b/packages/bolt-guilded/mod.ts @@ -33,7 +33,7 @@ export class guilded_plugin extends plugin<{ token: string }> { this.emit('delete_message', { channel: del.channelId, id: del.id, - platform: { message: del, name: 'bolt-guilded' }, + plugin: 'bolt-guilded', timestamp: Temporal.Instant.from(del.deletedAt) }); }); @@ -47,24 +47,24 @@ export class guilded_plugin extends plugin<{ token: string }> { } async create_message( - message: message, + message: message, channel: bridge_channel, _?: undefined, - replytoid?: string + reply_id?: string ) { const { id } = await new WebhookClient( channel.data as { token: string; id: string } - ).send(await convert_msg({ ...message, replytoid }, channel.id, this)); + ).send(await convert_msg({ ...message, reply_id }, channel.id, this)); return id; } // deno-lint-ignore require-await - async edit_message(_: message, __: bridge_channel, edit_id: string) { + async edit_message(_: message, __: bridge_channel, edit_id: string) { return edit_id; } async delete_message( - _message: deleted_message, + _message: deleted_message, channel: bridge_channel, id: string ) { diff --git a/packages/bolt-matrix/deps.ts b/packages/bolt-matrix/deps.ts index afc1fdf..aa9a05a 100644 --- a/packages/bolt-matrix/deps.ts +++ b/packages/bolt-matrix/deps.ts @@ -1,9 +1,9 @@ export { lightning, plugin, - type bridge_platform, + type bridge_channel, type message -} from 'jsr:@jersey/lightning@0.6.2'; +} from '../lightning/mod.ts'; export { existsSync } from 'jsr:@std/fs@0.221.0/exists'; export { Buffer } from 'node:buffer'; export { diff --git a/packages/bolt-matrix/events.ts b/packages/bolt-matrix/events.ts index 397b2a4..dda8d76 100644 --- a/packages/bolt-matrix/events.ts +++ b/packages/bolt-matrix/events.ts @@ -34,7 +34,7 @@ export async function onEvent( if (event.type === 'm.room.redaction') { this.emit('delete_message', { id: event.redacts as string, - platform: { name: 'bolt-matrix', message: event }, + plugin: 'bolt-matrix', channel: event.room_id, timestamp: Temporal.Instant.fromEpochMilliseconds(event.origin_server_ts) }); @@ -45,7 +45,7 @@ export async function messageToCore( event: WeakEvent, intent: Intent, homeserverUrl: string -): Promise> { +): Promise { console.log(event.content.formatted_body); const sender = await intent.getProfileInfo(event.sender); const is_reply = event.content['m.relates_to'] @@ -62,7 +62,7 @@ export async function messageToCore( ? `${sender.avatar_url?.replace( 'mxc://', `${homeserverUrl}/_matrix/media/v3/thumbnail/` - )}?width=96&height=96&method=scale` + )}?width=96&height=96&method=scale` : undefined }, channel: event.room_id, @@ -75,20 +75,20 @@ export async function messageToCore( ? (event.content.formatted_body as string).replace( /(.*?)<\/mx-reply>/i, '' - ) + ) : ((event.content['m.new_content']?.body || event.content.body) as string), - reply: async (msg: message) => { + reply: async (msg: message) => { await intent.sendMessage(event.room_id, coreToMessage(msg)); }, - replytoid: event.content['m.relates_to'] + reply_id: event.content['m.relates_to'] ? event.content['m.relates_to']['m.in_reply_to']?.event_id : undefined, - platform: { name: 'bolt-matrix', message: event } + plugin: 'bolt-matrix' }; } -export function coreToMessage(msg: message) { +export function coreToMessage(msg: message) { return { body: msg.content ? msg.content diff --git a/packages/bolt-matrix/mod.ts b/packages/bolt-matrix/mod.ts index 2229c5b..07f1df5 100644 --- a/packages/bolt-matrix/mod.ts +++ b/packages/bolt-matrix/mod.ts @@ -1,7 +1,7 @@ import { AppServiceRegistration, Bridge, - bridge_platform, + bridge_channel, Buffer, existsSync, lightning, @@ -22,8 +22,7 @@ type MatrixConfig = { export class matrix_plugin extends plugin { bot: Bridge; name = 'bolt-matrix'; - version = '0.6.1'; - support = ['0.6.1']; + version = '0.7.0'; constructor(l: lightning, config: MatrixConfig) { super(l, config); @@ -58,12 +57,13 @@ export class matrix_plugin extends plugin { } async create_message( - msg: message, - platform: bridge_platform, - edit = false + msg: message, + channel: bridge_channel, + edit_id?: string, + reply_id?: string, + edit?: boolean ) { - const room = platform.senddata as string; - const name = `@${platform.plugin}_${msg.author.id}:${this.config.domain}`; + const name = `@${msg.plugin}_${msg.author.id}:${this.config.domain}`; const intent = this.bot.getIntent(name); await intent.ensureProfile(msg.author.username); const store = this.bot.getUserStore(); @@ -82,52 +82,44 @@ export class matrix_plugin extends plugin { await store?.setMatrixUser(storeUser); } // now to our message - const message = coreToMessage(msg); + const message = coreToMessage({...msg, reply_id}); let editinfo = {}; if (edit) { editinfo = { 'm.new_content': message, 'm.relates_to': { rel_type: 'm.replace', - event_id: msg.id + event_id: edit_id! } }; } - const result = await intent.sendMessage(room, { + const result = await intent.sendMessage(channel.id, { ...message, ...editinfo }); - return { - channel: room, - id: result.event_id, - plugin: 'bolt-matrix', - senddata: room - }; + return result.event_id } async edit_message( - msg: message, - platform: bridge_platform & { id: string } + msg: message, + channel: bridge_channel, + edit_id?: string, + reply_id?: string ) { - return await this.create_message(msg, platform, true); + return await this.create_message(msg, channel, edit_id, reply_id, true); } async delete_message( - _msg: message, - platform: bridge_platform & { id: string } + _msg: message, + channel: bridge_channel, + delete_id: string ) { - const room = platform.senddata as string; const intent = this.bot.getIntent(); await intent.botSdkIntent.underlyingClient.redactEvent( - room, - platform.id, + channel.id, + delete_id, 'bridge message deletion' ); - return { - channel: room, - id: platform.id, - plugin: 'bolt-matrix', - senddata: room - }; + return delete_id } } diff --git a/packages/bolt-revolt/messages.ts b/packages/bolt-revolt/messages.ts index 334410d..5b141a0 100644 --- a/packages/bolt-revolt/messages.ts +++ b/packages/bolt-revolt/messages.ts @@ -1,7 +1,7 @@ import type { API, Message, TextEmbed, message } from './deps.ts'; export async function torevolt( - message: message, + message: message, masquerade = true ): Promise> { const dat: API.DataMessageSend = { @@ -29,13 +29,13 @@ export async function torevolt( ).json() )?.id; }) - ) + ) : undefined, content: message.content ? message.content : message.embeds - ? undefined - : 'empty message', + ? undefined + : 'empty message', embeds: message.embeds?.map(embed => { if (embed.fields) { for (const field of embed.fields) { @@ -49,10 +49,10 @@ export async function torevolt( avatar: message.author.profile, name: message.author.username.slice(0, 32), colour: message.author.color - } + } : undefined, - replies: message.replytoid - ? [{ id: message.replytoid, mention: true }] + replies: message.reply_id + ? [{ id: message.reply_id, mention: true }] : undefined }; @@ -64,7 +64,7 @@ export async function torevolt( return dat; } -export function tocore(message: Message): message { +export function tocore(message: Message): message { return { author: { username: @@ -92,8 +92,8 @@ export function tocore(message: Message): message { url: i.url ? i.url : undefined }; }), - platform: { name: 'bolt-revolt', message }, - reply: async (msg: message, masquerade = true) => { + plugin: 'bolt-revolt', + reply: async (msg: message, masquerade = true) => { message.reply(await torevolt(msg, masquerade as boolean)); }, attachments: message.attachments?.map( @@ -107,6 +107,6 @@ export function tocore(message: Message): message { } ), content: message.content, - replytoid: message.replyIds ? message.replyIds[0] : undefined + reply_id: message.replyIds ? message.replyIds[0] : undefined }; } diff --git a/packages/bolt-revolt/mod.ts b/packages/bolt-revolt/mod.ts index ff8af91..0d266f0 100644 --- a/packages/bolt-revolt/mod.ts +++ b/packages/bolt-revolt/mod.ts @@ -28,11 +28,11 @@ export class revolt_plugin extends plugin<{ token: string }> { this.emit('delete_message', { channel: message.channelId, id: message.id, - platform: { message, name: 'bolt-revolt' }, + plugin: 'bolt-revolt', timestamp: message.editedAt ? Temporal.Instant.fromEpochMilliseconds( message.editedAt?.getUTCMilliseconds() - ) + ) : Temporal.Now.instant() }); }); @@ -48,34 +48,30 @@ export class revolt_plugin extends plugin<{ token: string }> { } async create_message( - msg: message, + msg: message, bridge: bridge_channel, _: undefined, - replytoid?: string + reply_id?: string ) { const channel = await this.bot.channels.fetch(bridge.id); const result = await channel.sendMessage( - await torevolt({ ...msg, replytoid }) + await torevolt({ ...msg, reply_id }) ); return result.id; } async edit_message( - msg: message, + msg: message, bridge: bridge_channel, edit_id: string, - replytoid?: string + reply_id?: string ) { const message = await this.bot.messages.fetch(bridge.id, edit_id); - await message.edit(await torevolt({ ...msg, replytoid })); + await message.edit(await torevolt({ ...msg, reply_id })); return edit_id; } - async delete_message( - _: deleted_message, - bridge: bridge_channel, - id: string - ) { + async delete_message(_: deleted_message, bridge: bridge_channel, id: string) { const message = await this.bot.messages.fetch(bridge.id, id); await message.delete(); return id; diff --git a/packages/lightning/README.md b/packages/lightning/README.md index a0206ac..ea01d23 100644 --- a/packages/lightning/README.md +++ b/packages/lightning/README.md @@ -1,21 +1,23 @@ # @jersey/lightning -lightning is a Typescript-based chatbot that supports bridging multiple chat platforms via plugins. +lightning is a Typescript-based chatbot that supports bridging multiple chat +apps via plugins. ## [docs](https://williamhorning.dev/bolt) + ## example config ```ts -import { define_config } from "jsr:@jersey/lightning@0.7.0"; -import { discord_plugin } from "https://williamhorning.dev/bolt/x/bolt-discord/0.7.0/mod.ts"; +import { define_config } from 'jsr:@jersey/lightning@0.7.0'; +import { discord_plugin } from 'https://williamhorning.dev/bolt/x/bolt-discord/0.7.0/mod.ts'; export default define_config({ - redis_host: "localhost", - redis_port: 6379, - plugins: [ - discord_plugin.new({ - // ... - }), - ], + redis_host: 'localhost', + redis_port: 6379, + plugins: [ + discord_plugin.new({ + // ... + }) + ] }); ``` diff --git a/packages/lightning/mod.ts b/packages/lightning/mod.ts index 65ce8b8..4514a18 100644 --- a/packages/lightning/mod.ts +++ b/packages/lightning/mod.ts @@ -1,6 +1,6 @@ /** * lightning is a typescript-based chatbot that supports - * bridging multiple chat platforms via plugins. + * bridging multiple chat apps via plugins. * @module */ diff --git a/packages/lightning/src/bridges/command_functions.ts b/packages/lightning/src/bridges/command_functions.ts index a6e486e..66e0578 100644 --- a/packages/lightning/src/bridges/command_functions.ts +++ b/packages/lightning/src/bridges/command_functions.ts @@ -21,8 +21,8 @@ export async function join( ]; } - const plugin = l.plugins.get(opts.platform); - + const plugin = l.plugins.get(opts.plugin); + const bridge = (await l.bridges.get_bridge({ id })) || { @@ -34,13 +34,17 @@ export async function join( bridge.channels.push({ id: opts.channel, - plugin: opts.platform, + plugin: opts.plugin, data: await plugin!.create_bridge(opts.channel) }); await l.bridges.set_bridge(bridge); - await l.redis.sendCommand(['SET', `lightning-bchannel-${opts.channel}`, bridge.id]) + await l.redis.sendCommand([ + 'SET', + `lightning-bchannel-${opts.channel}`, + bridge.id + ]); return [true, 'Joined a bridge!']; } @@ -60,11 +64,11 @@ export async function leave( await l.bridges.set_bridge({ ...bridge, channels: bridge.channels.filter( - i => i.id !== opts.channel && i.plugin !== opts.platform + i => i.id !== opts.channel && i.plugin !== opts.plugin ) }); - await l.redis.sendCommand(['DEL', `lightning-bchannel-${opts.channel}`]) + await l.redis.sendCommand(['DEL', `lightning-bchannel-${opts.channel}`]); return [true, 'Left a bridge!']; } diff --git a/packages/lightning/src/bridges/handle_message.ts b/packages/lightning/src/bridges/handle_message.ts index 68347b3..3d49b25 100644 --- a/packages/lightning/src/bridges/handle_message.ts +++ b/packages/lightning/src/bridges/handle_message.ts @@ -9,7 +9,7 @@ import { log_error } from '../utils.ts'; export async function handle_message( lightning: lightning, - msg: message | deleted_message, + msg: message | deleted_message, type: 'create_message' | 'edit_message' | 'delete_message' ): Promise { const bridge = @@ -22,7 +22,7 @@ export async function handle_message( if (type !== 'create_message' && bridge.allow_editing !== true) return; const channels = bridge.channels.filter( - i => i.id !== msg.channel && i.plugin !== msg.platform.name + i => i.id !== msg.channel && i.plugin !== msg.plugin ); if (channels.length < 1) return; @@ -31,33 +31,28 @@ export async function handle_message( for (const channel of channels) { const index = bridge.channels.indexOf(channel); - const platform = bridge.channels[index]; const bridged_id = bridge.messages?.[index]; - if (!platform.data || (type !== 'create_message' && !bridged_id)) continue; + if (!channel.data || (type !== 'create_message' && !bridged_id)) continue; - const plugin = lightning.plugins.get(platform.plugin); + const plugin = lightning.plugins.get(channel.plugin); if (!plugin || !plugin[type]) { await log_error( - new Error(`plugin ${platform.plugin} doesn't have ${type}_message`), - { platform, bridged_id } + new Error(`plugin ${channel.plugin} doesn't have ${type}_message`), + { channel, bridged_id } ); continue; } let dat; - const reply_id = await get_reply_id( - lightning, - msg as message, - platform - ); + const reply_id = await get_reply_id(lightning, msg as message, channel); try { dat = await plugin[type]( - msg as message, - platform, + msg as message, + channel, bridged_id?.id!, reply_id ); @@ -65,16 +60,16 @@ export async function handle_message( if (type === 'delete_message') continue; try { - const err_msg = (await log_error(e, { platform, bridged_id })).message; + const err_msg = (await log_error(e, { channel, bridged_id })).message; - dat = await plugin[type](err_msg, platform, bridged_id?.id!, reply_id); + dat = await plugin[type](err_msg, channel, bridged_id?.id!, reply_id); } catch (e) { await log_error( new Error( - `Failed to send error for ${type}_message to ${platform.plugin}`, + `Failed to send error for ${type}_message to ${channel.plugin}`, { cause: e } ), - { platform, bridged_id } + { channel, bridged_id } ); continue; } @@ -86,7 +81,7 @@ export async function handle_message( '1' ]); - messages.push({ id: dat, channel: platform.id, platform: platform.plugin }); + messages.push({ id: dat, channel: channel.id, plugin: channel.plugin }); } for (const i of messages) { @@ -106,17 +101,17 @@ export async function handle_message( async function get_reply_id( lightning: lightning, - msg: message, + msg: message, channel: bridge_channel ) { - if ('replytoid' in msg && msg.replytoid) { + if (msg.reply_id) { try { - const bridged = await lightning.bridges.get_bridge_message(msg.replytoid); + const bridged = await lightning.bridges.get_bridge_message(msg.reply_id); if (!bridged) return; const bridge_channel = bridged.messages?.find( - i => i.channel === channel.id && i.platform === channel.plugin + i => i.channel === channel.id && i.plugin === channel.plugin ); return bridge_channel?.id; diff --git a/packages/lightning/src/bridges/mod.ts b/packages/lightning/src/bridges/mod.ts index 41ca78d..5759f60 100644 --- a/packages/lightning/src/bridges/mod.ts +++ b/packages/lightning/src/bridges/mod.ts @@ -3,7 +3,7 @@ import type { bridge_document } from '../types.ts'; import { bridge_commands } from './commands.ts'; import { handle_message } from './handle_message.ts'; -/** a thing that bridges messages between platforms defined by plugins */ +/** a thing that bridges messages between plugins */ export class bridges { /** the parent instance of lightning */ private l: lightning; @@ -30,8 +30,8 @@ export class bridges { } /** - * get all the platforms a message was bridged to - * @param id the id of the message to get the platforms for + * get all the channels a message was bridged to + * @param id the id of the message to get the channels for */ async get_bridge_message(id: string): Promise { const rdata = await this.l.redis.sendCommand([ @@ -47,10 +47,9 @@ export class bridges { * @param id the id of the message to check */ async is_bridged(id: string): Promise { - return Boolean(await this.l.redis.sendCommand([ - 'EXISTS', - `lightning-bridged-${id}` - ])); + return Boolean( + await this.l.redis.sendCommand(['EXISTS', `lightning-bridged-${id}`]) + ); } /** diff --git a/packages/lightning/src/cli/migrations.ts b/packages/lightning/src/cli/migrations.ts index 8d6b6c8..26dfb44 100644 --- a/packages/lightning/src/cli/migrations.ts +++ b/packages/lightning/src/cli/migrations.ts @@ -2,7 +2,7 @@ import { MongoClient, RedisClient } from '../../deps.ts'; import { convert_five_to_seven_redis } from '../migrations.ts'; import { versions } from '../types.ts'; import { apply_migrations, get_migrations } from '../utils.ts'; -import { writeFile } from "node:fs/promises"; +import { writeFile } from 'node:fs/promises'; export async function migrations() { const redis_hostname = prompt( @@ -100,7 +100,7 @@ export async function migrations() { console.log(`migrated your data!`); - const file = await Deno.makeTempFile() + const file = await Deno.makeTempFile(); await writeFile(file, JSON.stringify(final_data)); diff --git a/packages/lightning/src/lightning.ts b/packages/lightning/src/lightning.ts index e1a29df..a053dac 100644 --- a/packages/lightning/src/lightning.ts +++ b/packages/lightning/src/lightning.ts @@ -63,8 +63,6 @@ export class lightning extends EventEmitter { private listen_commands() { const prefix = this.config.cmd_prefix || 'l!'; - this.on('create_command', this.run_command); - this.on('create_nonbridged_message', m => { if (!m.content?.startsWith(prefix)) return; @@ -78,7 +76,7 @@ export class lightning extends EventEmitter { subcmd: subcmd as string, opts, channel: m.channel, - platform: m.platform.name, + plugin: m.plugin, reply: m.reply, timestamp: m.timestamp }); diff --git a/packages/lightning/src/migrations.ts b/packages/lightning/src/migrations.ts index 2dca475..0336986 100644 --- a/packages/lightning/src/migrations.ts +++ b/packages/lightning/src/migrations.ts @@ -1,4 +1,4 @@ -import { versions } from './types.ts'; +import { type bridge_document, versions } from './types.ts'; type doc = [string, unknown][]; @@ -36,7 +36,7 @@ export function convert_five_to_seven_redis(items: doc | fivedoc) { }) as doc; } -export const fivesevenredis = { +export const fivesevenexistingredis = { from: versions.Five, to: versions.Seven, translate: (items: doc | fivedocredis) => @@ -52,9 +52,15 @@ export const fivesevenredis = { return { id: i.channel, data: i.senddata, plugin: i.plugin }; }), id: `oldeditsupport-${msg_id}`, - messages: val.map(i => i.id), + messages: val.map(i => { + return { + channel: i.channel, + id: i.id, + plugin: i.plugin + } + }), use_rawname: false - } + } as bridge_document ] ]; } diff --git a/packages/lightning/src/plugins.ts b/packages/lightning/src/plugins.ts index 0821080..0e9d394 100644 --- a/packages/lightning/src/plugins.ts +++ b/packages/lightning/src/plugins.ts @@ -9,7 +9,7 @@ import type { } from './types.ts'; /** - * lightning plugins are used to add support for new platforms to lightning + * lightning plugins are used to add support for new chat apps to lightning * @module */ @@ -43,7 +43,7 @@ export abstract class plugin extends EventEmitter { /** this is used to bridge a NEW message */ abstract create_message( - message: message, + message: message, channel: bridge_channel, edit_id?: string, reply_id?: string @@ -51,7 +51,7 @@ export abstract class plugin extends EventEmitter { /** this is used to bridge an EDITED message */ abstract edit_message( - message: message, + message: message, channel: bridge_channel, edit_id: string, reply_id?: string @@ -59,7 +59,7 @@ export abstract class plugin extends EventEmitter { /** this is used to bridge a DELETED message */ abstract delete_message( - message: deleted_message, + message: deleted_message, channel: bridge_channel, delete_id: string, reply_id?: string diff --git a/packages/lightning/src/tests/error_handling.ts b/packages/lightning/src/tests/error_handling.ts index df0669d..ce43d16 100644 --- a/packages/lightning/src/tests/error_handling.ts +++ b/packages/lightning/src/tests/error_handling.ts @@ -3,7 +3,7 @@ import { log_error } from '../utils.ts'; const temporal_instant = Temporal.Instant.from('2021-01-01T00:00:00Z'); -globalThis.Temporal.Now.instant = () => { +Temporal.Now.instant = () => { return temporal_instant; }; @@ -46,7 +46,6 @@ const error_id = () => 'test'; const error_return = { e: err, - cause: err.cause, uuid: 'test', extra: { test: 'test' }, message: { @@ -62,10 +61,7 @@ const error_return = { id: '', reply: async () => {}, timestamp: Temporal.Instant.from('2021-01-01T00:00:00Z'), - platform: { - name: 'lightning', - message: undefined - } + plugin: 'lightning' } }; diff --git a/packages/lightning/src/tests/migrations.ts b/packages/lightning/src/tests/migrations.ts index 525fbf7..023fad5 100644 --- a/packages/lightning/src/tests/migrations.ts +++ b/packages/lightning/src/tests/migrations.ts @@ -1,18 +1,18 @@ import { assertEquals } from '../../deps.ts'; -import { fivesevenredis } from '../migrations.ts'; +import { fivesevenexistingredis } from '../migrations.ts'; import { versions } from '../types.ts'; import { apply_migrations, get_migrations } from '../utils.ts'; Deno.test('get a migration', () => { const migrations = get_migrations(versions.Five, versions.Seven); - assertEquals(migrations, [fivesevenredis]); -}) + assertEquals(migrations, [fivesevenexistingredis]); +}); Deno.test('apply migrations', () => { - const result = apply_migrations([fivesevenredis], migrations_five); + const result = apply_migrations([fivesevenexistingredis], migrations_five); assertEquals(result, migrations_seven as [string, unknown][]); -}) +}); const migrations_five = [ [ diff --git a/packages/lightning/src/tests/utils.ts b/packages/lightning/src/tests/utils.ts index 54c7ec3..ca78ff7 100644 --- a/packages/lightning/src/tests/utils.ts +++ b/packages/lightning/src/tests/utils.ts @@ -3,7 +3,7 @@ import { create_message, define_config } from '../utils.ts'; const temporal_instant = Temporal.Instant.from('2021-01-01T00:00:00Z'); -globalThis.Temporal.Now.instant = () => { +Temporal.Now.instant = () => { return temporal_instant; }; @@ -33,15 +33,11 @@ const msg = { id: '', reply: async () => {}, timestamp: Temporal.Instant.from('2021-01-01T00:00:00Z'), - platform: { - name: 'lightning', - message: undefined - } + plugin: 'lightning' }; const cfg = { plugins: [], - commands: [], redis_host: 'localhost', redis_port: 6379 }; diff --git a/packages/lightning/src/types.ts b/packages/lightning/src/types.ts index 4ed121b..70882c4 100644 --- a/packages/lightning/src/types.ts +++ b/packages/lightning/src/types.ts @@ -36,8 +36,8 @@ export interface bridge_message { id: string; /** the id of the channel the message was sent in */ channel: string; - /** the platform the message was sent on */ - platform: string; + /** the plugin the message was sent using */ + plugin: string; } /** the representation of a bridge */ @@ -91,14 +91,14 @@ export interface command_arguments { subcmd?: string; /** the channel its being run in */ channel: string; - /** the platform its being run on */ - platform: string; + /** the plugin its being run on */ + plugin: string; /** timestamp given */ timestamp: Temporal.Instant; /** options passed by the user */ opts: Record; /** the function to reply to the command */ - reply: (message: message, optional?: unknown) => Promise; + reply: (message: message, optional?: unknown) => Promise; } /** options when parsing a command */ @@ -124,13 +124,13 @@ export interface command { } /** a representation of a message that has been deleted */ -export interface deleted_message { +export interface deleted_message { /** the message's id */ id: string; /** the channel the message was sent in */ channel: string; - /** the platform the message was sent on */ - platform: platform; + /** the plugin that recieved the message */ + plugin: string; /** * the time the message was sent/edited as a temporal instant * @see https://tc39.es/proposal-temporal/docs/instant.html @@ -199,21 +199,19 @@ export interface embed { /** a site linked to by the embed */ url?: string; /** a video inside of the embed */ - video?: Omit & { url?: string }; + video?: embed_media; } /** the error returned from log_error */ export interface err { /** the original error */ e: Error; - /** the cause of the error */ - cause: unknown; /** extra information about the error */ extra: Record; /** the uuid associated with the error */ uuid: string; /** the message associated with the error */ - message: message; + message: message; } /** the author of a message */ @@ -226,15 +224,14 @@ export interface message_author { profile?: string; /** a url pointing to the authors banner */ banner?: string; - /** the author's id on their platform */ + /** the author's id */ id: string; /** the color of an author */ color?: string; } /** a message recieved by a plugin */ -export interface message - extends deleted_message { +export interface message extends deleted_message { /** the attachments sent with the message */ attachments?: attachment[]; /** the author of the message */ @@ -244,9 +241,9 @@ export interface message /** discord-style embeds */ embeds?: embed[]; /** a function to reply to a message */ - reply: (message: message, optional?: unknown) => Promise; + reply: (message: message, optional?: unknown) => Promise; /** the id of the message replied to */ - replytoid?: string; + reply_id?: string; } /** the type of a migration */ @@ -259,28 +256,16 @@ export interface migration { translate: (data: [string, unknown][]) => [string, unknown][]; } -/** the platform that recieved a message */ -export interface platform { - /** the name of a plugin */ - name: string; - /** the platforms representation of a message */ - message: message_type; - /** the webhook the message was sent with */ - webhookid?: string; -} - /** the events emitted by a plugin */ export type plugin_events = { /** when a message is created */ - create_message: [message]; - /** when a command is run (not a text command) */ - create_command: [Omit]; + create_message: [message]; /** when a message isn't already bridged (don't emit outside of core) */ - create_nonbridged_message: [message]; + create_nonbridged_message: [message]; /** when a message is edited */ - edit_message: [message]; + edit_message: [message]; /** when a message is deleted */ - delete_message: [deleted_message]; + delete_message: [deleted_message]; }; /** all of the versions with migrations to/from them */ diff --git a/packages/lightning/src/utils.ts b/packages/lightning/src/utils.ts index 81907d0..e05d4ed 100644 --- a/packages/lightning/src/utils.ts +++ b/packages/lightning/src/utils.ts @@ -1,4 +1,4 @@ -import { fivesevenredis } from './migrations.ts'; +import { fivesevenexistingredis } from './migrations.ts'; import type { config, err, message, migration, versions } from './types.ts'; /** @@ -6,8 +6,8 @@ import type { config, err, message, migration, versions } from './types.ts'; * @module */ -/** - * apply many migrations given data +/** + * apply many migrations given data * @param migrations the migrations to apply * @param data the data to apply the migrations to */ @@ -22,7 +22,7 @@ export function apply_migrations( * creates a message that can be sent using lightning * @param text the text of the message (can be markdown) */ -export function create_message(text: string): message { +export function create_message(text: string): message { const data = { author: { username: 'lightning', @@ -35,13 +35,13 @@ export function create_message(text: string): message { id: '', reply: async () => {}, timestamp: Temporal.Now.instant(), - platform: { name: 'lightning', message: undefined } + plugin: 'lightning' }; return data; } -/** - * a function that returns a config object when given a partial config object +/** + * a function that returns a config object when given a partial config object * @param config a partial config object */ export function define_config(config?: Partial): config { @@ -49,18 +49,17 @@ export function define_config(config?: Partial): config { plugins: [], redis_host: 'localhost', redis_port: 6379, - commands: [], ...(config || {}) }; } -/** +/** * get migrations that can then be applied using apply_migrations * @param from the version that the data is currently in * @param to the version that the data will be migrated to */ export function get_migrations(from: versions, to: versions): migration[] { - const migrations: migration[] = [fivesevenredis]; + const migrations: migration[] = [fivesevenexistingredis]; return migrations.slice( migrations.findIndex(i => i.from === from), migrations.findLastIndex(i => i.to === to) + 1 @@ -100,5 +99,5 @@ export async function log_error( `Something went wrong! [Look here](https://williamhorning.dev/bolt) for help.\n\`\`\`\n${e.message}\n${uuid}\n\`\`\`` ); - return { e, cause: e.cause, uuid, extra, message }; + return { e, uuid, extra, message }; }