Skip to content

Commit

Permalink
replies work same as before now, also fix guilded webhooks frfrfr thi…
Browse files Browse the repository at this point in the history
…s time
  • Loading branch information
williamhorning committed May 6, 2024
1 parent 3edf079 commit 9aa4bd3
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/bolt-commands/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { command } from '../lightning/src/types.ts';

export default [
export const bolt_commands = [
[
'help',
{
Expand Down
6 changes: 3 additions & 3 deletions packages/bolt-guilded/guilded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export async function create_webhook(
});
}
const wh = await new_wh.json();
if (!wh.token) {
if (!wh.webhook.token) {
throw new Error('Webhook lacks token!', {
cause: JSON.stringify(wh)
});
}
return { id: wh.id, token: wh.token };
return { id: wh.webhook.id, token: wh.webhook.token };
}

type guilded_msg = RESTPostWebhookBody & { replyMessageIds?: string[] };
Expand Down Expand Up @@ -97,7 +97,7 @@ export async function convert_msg(
function get_valid_username(msg: message<unknown>) {
function valid(e: string) {
if (!e || e.length === 0 || e.length > 32) return false;
return /^[a-zA-Z0-9_ ()]*$/gms.test(e);
return /^[a-zA-Z0-9_ ()-]*$/gms.test(e);
}

if (valid(msg.author.username)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt-guilded/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function tocore(
): Promise<message<Message> | undefined> {
if (!message.serverId) return;
let author;
if (!message.createdByWebhookId) {
if (!message.createdByWebhookId && message.authorId !== 'Ann6LewA') {
author = await plugin.bot.members.fetch(message.serverId, message.authorId);
}
const update_content = message.content.replaceAll('\n```\n```\n', '\n');
Expand Down
11 changes: 8 additions & 3 deletions packages/lightning/src/bridges/command_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export async function join(
}

const plugin = l.plugins.get(opts.platform);

const bridge = (await l.bridges.get_bridge({
id: `lightning-bridge-${id}`
id
})) || {
allow_editing: false,
channels: [],
id: `lightning-bridge-${id}`,
id,
use_rawname: false
};

Expand All @@ -39,6 +40,8 @@ export async function join(

await l.bridges.set_bridge(bridge);

await l.redis.sendCommand(['SET', `lightning-bchannel-${opts.channel}`, bridge.id])

return [true, 'Joined a bridge!'];
}

Expand All @@ -61,6 +64,8 @@ export async function leave(
)
});

await l.redis.sendCommand(['DEL', `lightning-bchannel-${opts.channel}`])

return [true, 'Left a bridge!'];
}

Expand Down Expand Up @@ -102,7 +107,7 @@ export async function toggle(opts: command_arguments, l: lightning) {
}

export async function status(args: command_arguments, l: lightning) {
const current = await l.bridges.get_bridge(args);
const current = await l.bridges.get_bridge({ channel: args.channel });

if (!current) {
return "You're not in any bridges right now.";
Expand Down
43 changes: 24 additions & 19 deletions packages/lightning/src/bridges/handle_message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { lightning } from '../lightning.ts';
import type { bridge_channel, deleted_message, message } from '../types.ts';
import type {
bridge_channel,
bridge_message,
deleted_message,
message
} from '../types.ts';
import { log_error } from '../utils.ts';

export async function handle_message(
Expand All @@ -16,11 +21,16 @@ export async function handle_message(

if (type !== 'create_message' && bridge.allow_editing !== true) return;

if (!bridge.channels || bridge.channels.length < 1) return;
const channels = bridge.channels.filter(
i => i.id !== msg.channel && i.plugin !== msg.platform.name
);

const messages = [];
if (channels.length < 1) return;

for (const index in bridge.channels) {
const messages = [] as bridge_message[];

for (const channel of channels) {
const index = bridge.channels.indexOf(channel);
const platform = bridge.channels[index];
const bridged_id = bridge.messages?.[index];

Expand Down Expand Up @@ -48,7 +58,7 @@ export async function handle_message(
dat = await plugin[type](
msg as message<unknown>,
platform,
bridged_id!,
bridged_id?.id!,
reply_id
);
} catch (e) {
Expand All @@ -57,7 +67,7 @@ export async function handle_message(
try {
const err_msg = (await log_error(e, { platform, bridged_id })).message;

dat = await plugin[type](err_msg, platform, bridged_id!, reply_id);
dat = await plugin[type](err_msg, platform, bridged_id?.id!, reply_id);
} catch (e) {
await log_error(
new Error(
Expand All @@ -76,13 +86,13 @@ export async function handle_message(
'1'
]);

messages.push(dat);
messages.push({ id: dat, channel: platform.id, platform: platform.plugin });
}

for (const i of messages) {
await lightning.redis.sendCommand([
'SET',
`lightning-bridged-${i}`,
`lightning-bridged-${i.id}`,
JSON.stringify({ ...bridge, messages })
]);
}
Expand All @@ -101,23 +111,18 @@ async function get_reply_id(
) {
if ('replytoid' in msg && msg.replytoid) {
try {
const bridge_from_msg = await lightning.bridges.get_bridge_message(
msg.replytoid
);
const bridged = await lightning.bridges.get_bridge_message(msg.replytoid);

if (!bridge_from_msg) return;
if (!bridged) return;

const bridge_channel = bridge_from_msg.channels.find(
i => i.id === channel.id && i.plugin === channel.plugin
const bridge_channel = bridged.messages?.find(
i => i.channel === channel.id && i.platform === channel.plugin
);

if (!bridge_channel) return;

return bridge_from_msg.messages![
bridge_from_msg.channels.indexOf(bridge_channel)
];
return bridge_channel?.id;
} catch {
return;
}
}
return;
}
39 changes: 22 additions & 17 deletions packages/lightning/src/bridges/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ export class bridges {
constructor(l: lightning) {
this.l = l;
l.on('create_message', async msg => {
await new Promise(res => setTimeout(res, 250));
await new Promise(res => setTimeout(res, 15));
if (await this.is_bridged(msg.id)) return;
l.emit('create_nonbridged_message', msg);
handle_message(l, msg, 'create_message');
});
l.on('edit_message', async msg => {
await new Promise(res => setTimeout(res, 250));
await new Promise(res => setTimeout(res, 15));
if (await this.is_bridged(msg.id)) return;
handle_message(l, msg, 'edit_message');
});
l.on('delete_message', async msg => {
await new Promise(res => setTimeout(res, 250));
await new Promise(res => setTimeout(res, 15));
handle_message(l, msg, 'delete_message');
});
l.commands.set('bridge', bridge_commands(l));
}

/**
* get all the platforms a message was bridged to
* get all the platforms a message was bridged to
* @param id the id of the message to get the platforms for
*/
async get_bridge_message(id: string): Promise<bridge_document | undefined> {
Expand All @@ -42,20 +42,19 @@ export class bridges {
return JSON.parse(rdata as string) as bridge_document;
}

/**
* check if a message was bridged
/**
* check if a message was bridged
* @param id the id of the message to check
*/
async is_bridged(id: string): Promise<boolean> {
return Boolean(
Number(
await this.l.redis.sendCommand(['EXISTS', `lightning-isbridged-${id}`])
)
);
return Boolean(await this.l.redis.sendCommand([
'EXISTS',
`lightning-bridged-${id}`
]));
}

/**
* check if a channel is in a bridge
/**
* check if a channel is in a bridge
* @param channel the channel to check
*/
async is_in_bridge(channel: string): Promise<boolean> {
Expand All @@ -69,8 +68,8 @@ export class bridges {
);
}

/**
* get a bridge using the bridges name or a channel in it
/**
* get a bridge using the bridges name or a channel in it
* @param param0 the id or channel of the bridge
*/
async get_bridge({
Expand All @@ -80,16 +79,22 @@ export class bridges {
id?: string;
channel?: string;
}): Promise<bridge_document | undefined> {
if (channel) {
id = (await this.l.redis.sendCommand([
'GET',
`lightning-bchannel-${channel}`
])) as string;
}
return JSON.parse(
(await this.l.redis.sendCommand([
'GET',
`lightning-${id ? 'bridge' : 'bchannel'}-${id || channel}`
`lightning-bridge-${id}`
])) as string
);
}

/**
* update a bridge in a database
* update a bridge in a database
* @param bridge the bridge to update
*/
async set_bridge(bridge: bridge_document): Promise<void> {
Expand Down
16 changes: 8 additions & 8 deletions packages/lightning/src/cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ if (cmd === 'version') {
migrations();
} else {
console.log('lightning v0.6.0 - cross-platform bot connecting communities');
console.log('Usage: lightning [subcommand] <options>');
console.log('Subcommands:');
console.log('help: show this');
console.log('run: run an of lightning using the settings in config.ts');
console.log('migrations: run migration script');
console.log('version: shows version');
console.log('Options:');
console.log('--config <string>: absolute path to config file');
console.log(' Usage: lightning [subcommand] <options>');
console.log(' Subcommands:');
console.log(' help: show this');
console.log(' run: run an of lightning using the settings in config.ts');
console.log(' migrations: run migration script');
console.log(' version: shows version');
console.log(' Options:');
console.log(' --config <string>: absolute path to config file');
}
12 changes: 11 additions & 1 deletion packages/lightning/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export interface bridge_channel<data_type = unknown> {
plugin: string;
}

/** bridged messages */
export interface bridge_message {
/** the id of the 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 representation of a bridge */
export interface bridge_document {
/** whether or not to allow editing */
Expand All @@ -39,7 +49,7 @@ export interface bridge_document {
/** the id of the bridge */
id: string;
/** messages bridged using these channels */
messages?: string[];
messages?: bridge_message[];
/** whether or not to use nicknames */
use_rawname: boolean;
}
Expand Down

0 comments on commit 9aa4bd3

Please sign in to comment.