Skip to content

Commit

Permalink
update version strings, simplify a ton
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed May 10, 2024
1 parent 898ca77 commit bdd9f5b
Show file tree
Hide file tree
Showing 22 changed files with 324 additions and 629 deletions.
2 changes: 1 addition & 1 deletion packages/bolt-guilded/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { tocore } from './messages.ts';
export class guilded_plugin extends plugin<{ token: string }> {
bot: Client;
name = 'bolt-guilded';
version = '0.6.1';
version = '0.7.0';

constructor(l: lightning, config: { token: string }) {
super(l, config);
Expand Down
2 changes: 1 addition & 1 deletion packages/bolt-revolt/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { tocore, torevolt } from './messages.ts';
export class revolt_plugin extends plugin<{ token: string }> {
bot: Client;
name = 'bolt-revolt';
version = '0.6.1';
version = '0.7.0';

constructor(l: lightning, config: { token: string }) {
super(l, config);
Expand Down
6 changes: 3 additions & 3 deletions packages/lightning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ apps via plugins.
## example config

```ts
import { define_config } from 'jsr:@jersey/[email protected]';
import type { config } from 'jsr:@jersey/[email protected]';
import { discord_plugin } from 'https://williamhorning.dev/bolt/x/bolt-discord/0.7.0/mod.ts';

export default define_config({
export default {
redis_host: 'localhost',
redis_port: 6379,
plugins: [
discord_plugin.new({
// ...
})
]
});
} as config;
```
7 changes: 1 addition & 6 deletions packages/lightning/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"name": "@jersey/lightning",
"version": "0.7.0",
"exports": {
".": "./mod.ts",
"./plugins": "./src/plugins.ts",
"./types": "./src/types.ts",
"./utils": "./src/utils.ts"
},
"exports": "./mod.ts",
"publish": {
"exclude": ["./src/tests/*"]
},
Expand Down
1 change: 0 additions & 1 deletion packages/lightning/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @module
*/

export { bridges } from './src/bridges/mod.ts';
export { lightning } from './src/lightning.ts';
export { plugin } from './src/plugins.ts';
export * from './src/types.ts';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { lightning } from '../lightning.ts';
import type { command_arguments } from '../types.ts';
import {
del_key,
exists,
get_bridge,
get_channel_bridge,
set_bridge
} from './functions.ts';

export async function join(
opts: command_arguments,
l: lightning
opts: command_arguments
): Promise<[boolean, string]> {
if (await l.bridges.is_in_bridge(opts.channel)) {
if (await exists(opts.lightning, `lightning-bchannel-${opts.channel}`)) {
return [
false,
"To do this, you can't be in a bridge. Try leaving your bridge first."
Expand All @@ -21,11 +26,9 @@ export async function join(
];
}

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

const bridge = (await l.bridges.get_bridge({
id
})) || {
const bridge = (await get_bridge(opts.lightning, id)) || {
allow_editing: false,
channels: [],
id,
Expand All @@ -38,56 +41,47 @@ export async function join(
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 set_bridge(opts.lightning, bridge);

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

export async function leave(
opts: command_arguments,
l: lightning
opts: command_arguments
): Promise<[boolean, string]> {
const bridge = await l.bridges.get_bridge({
channel: opts.channel
});
const bridge = await get_channel_bridge(opts.lightning, opts.channel);

if (!bridge) {
return [true, "You're not in a bridge, so try joining a bridge first."];
}

await l.bridges.set_bridge({
await set_bridge(opts.lightning, {
...bridge,
channels: bridge.channels.filter(
i => i.id !== opts.channel && i.plugin !== opts.plugin
)
});

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

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

export async function reset(opts: command_arguments, l: lightning) {
export async function reset(opts: command_arguments) {
if (!opts.opts.name)
opts.opts.name =
(await l.bridges.get_bridge({ channel: opts.channel }))?.id ||
(await get_channel_bridge(opts.lightning, opts.channel))?.id ||
opts.channel;

let [ok, text] = await leave(opts, l);
let [ok, text] = await leave(opts);
if (!ok) return text;
[ok, text] = await join(opts, l);
[ok, text] = await join(opts);
if (!ok) return text;
return 'Reset this bridge!';
}

export async function toggle(opts: command_arguments, l: lightning) {
const bridge = await l.bridges.get_bridge({ channel: opts.channel });
export async function toggle(opts: command_arguments) {
const bridge = await get_channel_bridge(opts.lightning, opts.channel);

if (!bridge) {
return "You're not in a bridge right now. Try joining one first.";
Expand All @@ -105,26 +99,21 @@ export async function toggle(opts: command_arguments, l: lightning) {

bridge[setting] = !bridge[setting];

await l.bridges.set_bridge(bridge);
await set_bridge(opts.lightning, bridge);

return 'Toggled that setting!';
}

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

if (!current) {
return "You're not in any bridges right now.";
}

const editing_text = current.allow_editing
? 'with editing enabled'
: 'with editing disabled';
const rawname_text = current.use_rawname
? 'and nicknames disabled'
: 'and nicknames enabled';

return `This channel is connected to \`${current.id}\`, a bridge with ${
current.channels.length - 1
} other channels connected to it, ${editing_text} ${rawname_text}`;
} other channels connected to it, with editing ${
current.allow_editing ? 'enabled' : 'disabled'
} and nicknames ${current.use_rawname ? 'disabled' : 'enabled'}`;
}
43 changes: 0 additions & 43 deletions packages/lightning/src/bridges/commands.ts

This file was deleted.

48 changes: 48 additions & 0 deletions packages/lightning/src/bridges/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { lightning } from '../lightning.ts';
import type { bridge_document } from '../types.ts';

export async function exists(l: lightning, key: string) {
return Boolean(await l.redis.sendCommand(['EXISTS', key]));
}

export async function get_json<T = unknown>(
l: lightning,
key: string
): Promise<T | undefined> {
const reply = await l.redis.sendCommand(['GET', key]);
if (!reply || reply === 'OK') return;
return JSON.parse(reply as string) as T;
}

export async function del_key(l: lightning, key: string) {
await l.redis.sendCommand(['DEL', key]);
}

export async function set_json(l: lightning, key: string, value: unknown) {
await l.redis.sendCommand(['SET', key, JSON.stringify(value)]);
}

export async function get_bridge(l: lightning, id: string) {
return await get_json<bridge_document>(l, `lightning-bridge-${id}`);
}

export async function get_channel_bridge(l: lightning, id: string) {
const ch = await l.redis.sendCommand(['GET', `lightning-bchannel-${id}`]);
return await get_bridge(l, ch as string);
}

export async function get_message_bridge(l: lightning, id: string) {
return await get_json<bridge_document>(l, `lightning-bridged-${id}`);
}

export async function set_bridge(l: lightning, bridge: bridge_document) {
set_json(l, `lightning-bridge-${bridge.id}`, bridge);

for (const channel of bridge.channels) {
await l.redis.sendCommand([
'SET',
`lightning-bchannel-${channel.id}`,
bridge.id
]);
}
}
35 changes: 17 additions & 18 deletions packages/lightning/src/bridges/handle_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type {
message
} from '../types.ts';
import { log_error } from '../utils.ts';
import {
get_channel_bridge,
get_message_bridge,
set_json
} from './functions.ts';

export async function handle_message(
lightning: lightning,
Expand All @@ -14,8 +19,8 @@ export async function handle_message(
): Promise<void> {
const bridge =
type === 'create_message'
? await lightning.bridges.get_bridge(msg)
: await lightning.bridges.get_bridge_message(msg.id);
? await get_channel_bridge(lightning, msg.channel)
: await get_message_bridge(lightning, msg.id);

if (!bridge) return;

Expand Down Expand Up @@ -75,28 +80,22 @@ export async function handle_message(
}
}

await lightning.redis.sendCommand([
'SET',
`lightning-isbridged-${dat}`,
'1'
]);
await set_json(lightning, `lightning-isbridged-${dat}`, '1');

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

for (const i of messages) {
await lightning.redis.sendCommand([
'SET',
`lightning-bridged-${i.id}`,
JSON.stringify({ ...bridge, messages })
]);
await set_json(lightning, `lightning-bridged-${i.id}`, {
...bridge,
messages
});
}

await lightning.redis.sendCommand([
'SET',
`lightning-bridged-${msg.id}`,
JSON.stringify({ ...bridge, messages })
]);
await set_json(lightning, `lightning-bridged-${msg.id}`, {
...bridge,
messages
});
}

async function get_reply_id(
Expand All @@ -106,7 +105,7 @@ async function get_reply_id(
) {
if (msg.reply_id) {
try {
const bridged = await lightning.bridges.get_bridge_message(msg.reply_id);
const bridged = await get_message_bridge(lightning, msg.reply_id);

if (!bridged) return;

Expand Down
Loading

0 comments on commit bdd9f5b

Please sign in to comment.