Skip to content

Commit

Permalink
remove most of the unnecessary plugin name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed Aug 14, 2024
1 parent 9706716 commit d88e91c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 100 deletions.
3 changes: 2 additions & 1 deletion packages/bolt-discord/src/commands.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 0 additions & 4 deletions packages/bolt-discord/src/process_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ 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') {
return {
channel: opts.channel,
error: e,
disable: true,
plugin: 'bolt-discord',
};
} else {
throw e;
Expand All @@ -68,15 +66,13 @@ export async function process_message(api: API, opts: message_options) {
return {
id: opts.edit_id,
channel: opts.channel,
plugin: 'bolt-discord',
};
}
} catch (e) {
return {
channel: opts.channel,
error: e,
disable: false,
plugin: 'bolt-discord',
};
}
}
6 changes: 0 additions & 6 deletions packages/bolt-guilded/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,19 @@ export class guilded_plugin extends plugin<guilded_config> {
return {
id: [id],
channel: opts.channel,
plugin: this.name,
};
} catch (e) {
if (e.response.status === 404) {
return {
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;
Expand All @@ -102,13 +99,11 @@ export class guilded_plugin extends plugin<guilded_config> {

return {
channel: opts.channel,
plugin: this.name,
id: opts.edit_id,
};
} else {
return {
channel: opts.channel,
plugin: this.name,
id: opts.edit_id,
};
}
Expand All @@ -118,7 +113,6 @@ export class guilded_plugin extends plugin<guilded_config> {
channel: opts.channel,
error: e,
disable: false,
plugin: this.name,
};
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/bolt-revolt/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export async function torvapi(
embeds: message.embeds?.map<SendableEmbed>((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 {
Expand Down Expand Up @@ -84,7 +83,7 @@ export async function fromrvapi(
undefined,
) as Member
: undefined;

return {
author: {
id: message.author,
Expand All @@ -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,
};
Expand Down
5 changes: 0 additions & 5 deletions packages/bolt-revolt/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ export class revolt_plugin extends plugin<revolt_config> {
return {
channel: opts.channel,
id: [msg._id],
plugin: this.name,
};
} catch (e) {
if (e.cause.status === 403 || e.cause.status === 404) {
return {
channel: opts.channel,
disable: true,
error: e,
plugin: this.name,
};
} else {
throw e;
Expand All @@ -97,7 +95,6 @@ export class revolt_plugin extends plugin<revolt_config> {
return {
channel: opts.channel,
id: opts.edit_id,
plugin: this.name,
};
} else {
await this.bot.request(
Expand All @@ -109,15 +106,13 @@ export class revolt_plugin extends plugin<revolt_config> {
return {
channel: opts.channel,
id: opts.edit_id,
plugin: this.name,
};
}
} catch (e) {
return {
channel: opts.channel,
disable: false,
error: e,
plugin: this.name,
};
}
}
Expand Down
137 changes: 72 additions & 65 deletions packages/bolt-revolt/src/permissions.ts
Original file line number Diff line number Diff line change
@@ -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.');

}
throw new Error(
'Insufficient permissions! Please enable ManageMessages and Masquerade permissions.',
);
}
4 changes: 0 additions & 4 deletions packages/bolt-telegram/src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export class telegram_plugin extends plugin<telegram_config> {
return {
id: opts.edit_id,
channel: opts.channel,
plugin: this.name,
};
} else if (opts.action === 'edit') {
const content = from_lightning(opts.message)[0];
Expand All @@ -87,7 +86,6 @@ export class telegram_plugin extends plugin<telegram_config> {
return {
id: opts.edit_id,
channel: opts.channel,
plugin: this.name,
};
} else if (opts.action === 'create') {
const content = from_lightning(opts.message);
Expand All @@ -113,7 +111,6 @@ export class telegram_plugin extends plugin<telegram_config> {
return {
id: messages,
channel: opts.channel,
plugin: this.name,
};
} else {
throw new Error('unknown action');
Expand All @@ -124,7 +121,6 @@ export class telegram_plugin extends plugin<telegram_config> {
error: e,
id: [opts.message.id],
channel: opts.channel,
plugin: this.name,
};
}
}
Expand Down
14 changes: 8 additions & 6 deletions packages/lightning/src/bridges/handle_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export async function handle_message(
channel,
disable: false,
error: e,
plugin: channel.plugin,
};

if (type === 'delete_message') continue;
Expand All @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/lightning/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down

0 comments on commit d88e91c

Please sign in to comment.