From d88e91c1cd29328a607eee6fe9e0d25638692536 Mon Sep 17 00:00:00 2001 From: Jersey Date: Tue, 13 Aug 2024 23:30:42 -0400 Subject: [PATCH] remove most of the unnecessary plugin name handling --- packages/bolt-discord/src/commands.ts | 3 +- packages/bolt-discord/src/process_message.ts | 4 - packages/bolt-guilded/src/mod.ts | 6 - packages/bolt-revolt/src/messages.ts | 8 +- packages/bolt-revolt/src/mod.ts | 5 - packages/bolt-revolt/src/permissions.ts | 137 +++++++++--------- packages/bolt-telegram/src/mod.ts | 4 - .../lightning/src/bridges/handle_message.ts | 14 +- packages/lightning/src/types.ts | 4 - 9 files changed, 85 insertions(+), 100 deletions(-) diff --git a/packages/bolt-discord/src/commands.ts b/packages/bolt-discord/src/commands.ts index f320831..1915396 100644 --- a/packages/bolt-discord/src/commands.ts +++ b/packages/bolt-discord/src/commands.ts @@ -1,5 +1,6 @@ -import type { API, APIInteraction } from '@discordjs/core'; +import type { API } from '@discordjs/core'; import type { command, command_arguments } from '@jersey/lightning'; +import type { APIInteraction } from 'discord-api-types'; import { to_discord } from './discord.ts'; import { instant } from './lightning.ts'; diff --git a/packages/bolt-discord/src/process_message.ts b/packages/bolt-discord/src/process_message.ts index 58f43b4..c8068b7 100644 --- a/packages/bolt-discord/src/process_message.ts +++ b/packages/bolt-discord/src/process_message.ts @@ -44,7 +44,6 @@ export async function process_message(api: API, opts: message_options) { return { id: [wh.id], channel: opts.channel, - plugin: 'bolt-discord', }; } catch (e) { if (e.status === 404 && opts.action !== 'edit') { @@ -52,7 +51,6 @@ export async function process_message(api: API, opts: message_options) { channel: opts.channel, error: e, disable: true, - plugin: 'bolt-discord', }; } else { throw e; @@ -68,7 +66,6 @@ export async function process_message(api: API, opts: message_options) { return { id: opts.edit_id, channel: opts.channel, - plugin: 'bolt-discord', }; } } catch (e) { @@ -76,7 +73,6 @@ export async function process_message(api: API, opts: message_options) { channel: opts.channel, error: e, disable: false, - plugin: 'bolt-discord', }; } } diff --git a/packages/bolt-guilded/src/mod.ts b/packages/bolt-guilded/src/mod.ts index 8566449..47716f5 100644 --- a/packages/bolt-guilded/src/mod.ts +++ b/packages/bolt-guilded/src/mod.ts @@ -71,7 +71,6 @@ export class guilded_plugin extends plugin { return { id: [id], channel: opts.channel, - plugin: this.name, }; } catch (e) { if (e.response.status === 404) { @@ -79,14 +78,12 @@ export class guilded_plugin extends plugin { channel: opts.channel, disable: true, error: new Error('webhook not found!'), - plugin: this.name, }; } else if (e.response.status === 403) { return { channel: opts.channel, disable: true, error: new Error('no permission to send messages!'), - plugin: this.name, }; } else { throw e; @@ -102,13 +99,11 @@ export class guilded_plugin extends plugin { return { channel: opts.channel, - plugin: this.name, id: opts.edit_id, }; } else { return { channel: opts.channel, - plugin: this.name, id: opts.edit_id, }; } @@ -118,7 +113,6 @@ export class guilded_plugin extends plugin { channel: opts.channel, error: e, disable: false, - plugin: this.name, }; } } diff --git a/packages/bolt-revolt/src/messages.ts b/packages/bolt-revolt/src/messages.ts index 40ff1e0..02eabff 100644 --- a/packages/bolt-revolt/src/messages.ts +++ b/packages/bolt-revolt/src/messages.ts @@ -33,8 +33,7 @@ export async function torvapi( embeds: message.embeds?.map((embed) => { if (embed.fields) { for (const field of embed.fields) { - embed.description += - `\n\n**${field.name}**\n${field.value}`; + embed.description += `\n\n**${field.name}**\n${field.value}`; } } return { @@ -84,7 +83,7 @@ export async function fromrvapi( undefined, ) as Member : undefined; - + return { author: { id: message.author, @@ -107,8 +106,7 @@ export async function fromrvapi( plugin: 'bolt-revolt', attachments: message.attachments?.map((i) => { return { - file: - `https://autumn.revolt.chat/attachments/${i._id}/${i.filename}`, + file: `https://autumn.revolt.chat/attachments/${i._id}/${i.filename}`, name: i.filename, size: i.size, }; diff --git a/packages/bolt-revolt/src/mod.ts b/packages/bolt-revolt/src/mod.ts index fb343af..10a76d3 100644 --- a/packages/bolt-revolt/src/mod.ts +++ b/packages/bolt-revolt/src/mod.ts @@ -68,7 +68,6 @@ export class revolt_plugin extends plugin { return { channel: opts.channel, id: [msg._id], - plugin: this.name, }; } catch (e) { if (e.cause.status === 403 || e.cause.status === 404) { @@ -76,7 +75,6 @@ export class revolt_plugin extends plugin { channel: opts.channel, disable: true, error: e, - plugin: this.name, }; } else { throw e; @@ -97,7 +95,6 @@ export class revolt_plugin extends plugin { return { channel: opts.channel, id: opts.edit_id, - plugin: this.name, }; } else { await this.bot.request( @@ -109,7 +106,6 @@ export class revolt_plugin extends plugin { return { channel: opts.channel, id: opts.edit_id, - plugin: this.name, }; } } catch (e) { @@ -117,7 +113,6 @@ export class revolt_plugin extends plugin { channel: opts.channel, disable: false, error: e, - plugin: this.name, }; } } diff --git a/packages/bolt-revolt/src/permissions.ts b/packages/bolt-revolt/src/permissions.ts index b45b437..8655e79 100644 --- a/packages/bolt-revolt/src/permissions.ts +++ b/packages/bolt-revolt/src/permissions.ts @@ -1,82 +1,89 @@ import type { Client } from '@jersey/rvapi'; -import type { Channel, User, Server, Member, Role } from '@jersey/revolt-api-types'; +import type { + Channel, + Member, + Role, + Server, + User, +} from '@jersey/revolt-api-types'; export async function revolt_perms( - client: Client, - channel: string, + client: Client, + channel: string, ) { - const ch = await client.request( - 'get', - `/channels/${channel}`, - undefined, - ) as Channel; + const ch = await client.request( + 'get', + `/channels/${channel}`, + undefined, + ) as Channel; - const self_user = await client.request( - 'get', - '/users/@me', - undefined, - ) as User; + const self_user = await client.request( + 'get', + '/users/@me', + undefined, + ) as User; - const permissions_to_check = [ - 1 << 23, // ManageMessages - 1 << 28, // Masquerade - ]; + const permissions_to_check = [ + 1 << 23, // ManageMessages + 1 << 28, // Masquerade + ]; - // see https://developers.revolt.chat/assets/api/Permission%20Hierarchy.svg - const permissions = permissions_to_check.reduce((a, b) => a | b, 0); + // see https://developers.revolt.chat/assets/api/Permission%20Hierarchy.svg + const permissions = permissions_to_check.reduce((a, b) => a | b, 0); - if (ch.channel_type === 'Group') { - if (ch.owner === self_user._id) return channel; - if (ch.permissions && ch.permissions & permissions) return channel; - } else if (ch.channel_type === 'TextChannel') { - const srvr = await client.request( - 'get', - `/servers/${ch.server}`, - undefined, - ) as Server; + if (ch.channel_type === 'Group') { + if (ch.owner === self_user._id) return channel; + if (ch.permissions && ch.permissions & permissions) return channel; + } else if (ch.channel_type === 'TextChannel') { + const srvr = await client.request( + 'get', + `/servers/${ch.server}`, + undefined, + ) as Server; - const member = await client.request( - 'get', - `/servers/${ch.server}/members/${self_user._id}`, - undefined, - ) as Member; + const member = await client.request( + 'get', + `/servers/${ch.server}/members/${self_user._id}`, + undefined, + ) as Member; - // check server permissions - if (srvr.owner === self_user._id) return channel; + // check server permissions + if (srvr.owner === self_user._id) return channel; - let perms = srvr.default_permissions; + let perms = srvr.default_permissions; - for (const role of (member.roles || [])) { - const { permissions: role_perms } = await client.request( - 'get', - `/servers/${ch.server}/roles/${role}`, - undefined, - ) as Role; + for (const role of (member.roles || [])) { + const { permissions: role_perms } = await client.request( + 'get', + `/servers/${ch.server}/roles/${role}`, + undefined, + ) as Role; - perms |= role_perms.a || 0; - perms &= ~role_perms.d || 0; - } + perms |= role_perms.a || 0; + perms &= ~role_perms.d || 0; + } - // apply default allow/denies - if (ch.default_permissions) { - perms |= ch.default_permissions.a; - perms &= ~ch.default_permissions.d; - } + // apply default allow/denies + if (ch.default_permissions) { + perms |= ch.default_permissions.a; + perms &= ~ch.default_permissions.d; + } - // apply role permissions - if (ch.role_permissions) { - for (const role of (member.roles || [])) { - perms |= ch.role_permissions[role]?.a || 0; - perms &= ~ch.role_permissions[role]?.d || 0; - } - } + // apply role permissions + if (ch.role_permissions) { + for (const role of (member.roles || [])) { + perms |= ch.role_permissions[role]?.a || 0; + perms &= ~ch.role_permissions[role]?.d || 0; + } + } - // check permissions - if (perms & permissions) return channel; - } else { - throw new Error(`Unsupported channel type: ${ch.channel_type}`); - } + // check permissions + if (perms & permissions) return channel; + } else { + throw new Error(`Unsupported channel type: ${ch.channel_type}`); + } - throw new Error('Insufficient permissions! Please enable ManageMessages and Masquerade permissions.'); - -} \ No newline at end of file + throw new Error( + 'Insufficient permissions! Please enable ManageMessages and Masquerade permissions.', + ); +} diff --git a/packages/bolt-telegram/src/mod.ts b/packages/bolt-telegram/src/mod.ts index fc0b3bc..02bb3c9 100644 --- a/packages/bolt-telegram/src/mod.ts +++ b/packages/bolt-telegram/src/mod.ts @@ -70,7 +70,6 @@ export class telegram_plugin extends plugin { return { id: opts.edit_id, channel: opts.channel, - plugin: this.name, }; } else if (opts.action === 'edit') { const content = from_lightning(opts.message)[0]; @@ -87,7 +86,6 @@ export class telegram_plugin extends plugin { return { id: opts.edit_id, channel: opts.channel, - plugin: this.name, }; } else if (opts.action === 'create') { const content = from_lightning(opts.message); @@ -113,7 +111,6 @@ export class telegram_plugin extends plugin { return { id: messages, channel: opts.channel, - plugin: this.name, }; } else { throw new Error('unknown action'); @@ -124,7 +121,6 @@ export class telegram_plugin extends plugin { error: e, id: [opts.message.id], channel: opts.channel, - plugin: this.name, }; } } diff --git a/packages/lightning/src/bridges/handle_message.ts b/packages/lightning/src/bridges/handle_message.ts index bb92e01..cf29079 100644 --- a/packages/lightning/src/bridges/handle_message.ts +++ b/packages/lightning/src/bridges/handle_message.ts @@ -89,7 +89,6 @@ export async function handle_message( channel, disable: false, error: e, - plugin: channel.plugin, }; if (type === 'delete_message') continue; @@ -114,11 +113,14 @@ export async function handle_message( bridge, ); - await log_error(new Error(`disabled channel ${channel.id} on ${channel.plugin}`), { - channel, - dat, - bridged_id, - }); + await log_error( + new Error(`disabled channel ${channel.id} on ${channel.plugin}`), + { + channel, + dat, + bridged_id, + }, + ); continue; } diff --git a/packages/lightning/src/types.ts b/packages/lightning/src/types.ts index 0c339bf..ae5b43f 100644 --- a/packages/lightning/src/types.ts +++ b/packages/lightning/src/types.ts @@ -220,8 +220,6 @@ export interface processed_message { id: string[]; /** the channel the message was sent to */ channel: bridge_channel; - /** the plugin the message was sent using */ - plugin: string; } /** messages not processed */ @@ -232,8 +230,6 @@ export interface unprocessed_message { disable: boolean; /** the error causing this */ error: Error; - /** the plugin the message was sent using */ - plugin: string; } /** process result */