Skip to content

Commit ac02c72

Browse files
redo error handling, centralize cacher, change schema handler, improve node support, fix revolt embed colors, fix guilded reply handling, and bump versions?
fixes #78
1 parent 7f92248 commit ac02c72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+470
-537
lines changed

.github/workflows/publish.yml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,20 @@ permissions:
1111
id-token: write
1212

1313
jobs:
14-
publish_jsr:
15-
name: publish to jsr
14+
publish:
15+
name: publish to jsr and ghcr
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: checkout
1919
uses: actions/checkout@v4
2020
- name: setup deno
21-
uses: denoland/setup-deno@v1
21+
uses: denoland/setup-deno@v2
2222
with:
23-
deno-version: v2.2.11
23+
deno-version: v2.3.3
2424
- name: publish to jsr
2525
run: deno publish
26-
publish_docker:
27-
name: publish to ghcr
28-
runs-on: ubuntu-latest
29-
needs: publish_jsr
30-
steps:
31-
- name: checkout
32-
uses: actions/checkout@v4
3326
- name: Set up QEMU
34-
uses: docker/setup-qemu-action@v1
27+
uses: docker/setup-qemu-action@v3
3528
- name: login to ghcr
3629
uses: redhat-actions/podman-login@v1
3730
with:

containerfile

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
FROM denoland/deno:alpine-2.2.11@sha256:c6c801a49a98f295f46176fba6172a1a656decd7dfb086a499fe863f595b922b
2-
3-
# metadata
4-
LABEL org.opencontainers.image.authors=Jersey
5-
LABEL org.opencontainers.image.url=https://github.com/williamhorning/lightning
6-
LABEL org.opencontainers.image.source=https://github.com/williamhorning/lightning
7-
LABEL org.opencontainers.image.documentation=https://williamhorning.eu.org/lightning
8-
LABEL org.opencontainers.image.version=0.8.0-alpha.2
9-
LABEL org.opencontainers.image.licenses=MIT
10-
LABEL org.opencontainers.image.title=lightning
1+
FROM denoland/deno:alpine-2.3.3
112

123
# make a deno cache directory
134
RUN ["mkdir", "/deno_dir"]
145
ENV DENO_DIR=/deno_dir
156

167
# install lightning
17-
RUN ["deno", "install", "--global", "--name", "lightning", "--allow-all", "--unstable-temporal", "--unstable-net", "jsr:@lightning/lightning@0.8.0-alpha.2"]
8+
RUN ["deno", "install", "-gA", "--unstable-temporal", "--unstable-net", "jsr:@lightning/lightning@0.8.0-alpha.3"]
189
RUN ["chown", "--recursive", "1001:1001", "/deno_dir"]
1910

2011
# run as user instead of root
2112
USER 1001:1001
2213

2314
# the volume containing your lightning.toml file
2415
VOLUME [ "/data" ]
16+
WORKDIR /data
2517

2618
# this is the lightning command line
2719
ENTRYPOINT [ "lightning" ]
2820

2921
# run the bot using the user-provided lightning.toml file
30-
CMD [ "run", "/data/lightning.toml" ]
22+
CMD [ "run" ]
File renamed without changes.

packages/discord/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ you do that, you will need to add the following to your `lightning.toml` file:
99

1010
```toml
1111
[[plugins]]
12-
plugin = "jsr:@lightning/discord@0.8.0-alpha.2"
12+
plugin = "jsr:@lightning/discord@0.8.0-alpha.3"
1313
config.token = "your_bot_token"
1414
```

packages/discord/deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@lightning/discord",
3-
"version": "0.8.0-alpha.2",
3+
"version": "0.8.0-alpha.3",
44
"license": "MIT",
55
"exports": "./src/mod.ts",
66
"imports": {
77
"@discordjs/core": "npm:@discordjs/core@^2.1.0",
88
"@discordjs/rest": "npm:@discordjs/rest@^2.5.0",
99
"@discordjs/ws": "npm:@discordjs/ws@^2.0.2",
10-
"@lightning/lightning": "jsr:@lightning/lightning@0.8.0-alpha.2"
10+
"@lightning/lightning": "jsr:@lightning/lightning@0.8.0-alpha.3"
1111
}
1212
}

packages/discord/src/commands.ts

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,32 @@ export async function setup_commands(
55
api: API,
66
commands: command[],
77
): Promise<void> {
8-
await api.applicationCommands.bulkOverwriteGlobalCommands(
9-
(await api.applications.getCurrent()).id,
10-
commands.map((command) => {
11-
const opts = [];
12-
13-
if (command.arguments) {
14-
for (const argument of command.arguments) {
15-
opts.push({
16-
name: argument.name,
17-
description: argument.description,
18-
type: 3,
19-
required: argument.required,
20-
});
21-
}
22-
}
8+
const format_arguments = (args: command['arguments']) =>
9+
args?.map((arg) => ({
10+
name: arg.name,
11+
description: arg.description,
12+
type: 3,
13+
required: arg.required,
14+
})) || [];
2315

24-
if (command.subcommands) {
25-
for (const subcommand of command.subcommands) {
26-
opts.push({
27-
name: subcommand.name,
28-
description: subcommand.description,
29-
type: 1,
30-
options: subcommand.arguments?.map((opt) => ({
31-
name: opt.name,
32-
description: opt.description,
33-
type: 3,
34-
required: opt.required,
35-
})),
36-
});
37-
}
38-
}
16+
const format_subcommands = (subcommands: command['subcommands']) =>
17+
subcommands?.map((subcommand) => ({
18+
name: subcommand.name,
19+
description: subcommand.description,
20+
type: 1,
21+
options: format_arguments(subcommand.arguments),
22+
})) || [];
3923

40-
return {
41-
name: command.name,
42-
type: 1,
43-
description: command.description,
44-
options: opts,
45-
};
46-
}),
24+
await api.applicationCommands.bulkOverwriteGlobalCommands(
25+
(await api.applications.getCurrent()).id,
26+
commands.map((cmd) => ({
27+
name: cmd.name,
28+
type: 1,
29+
description: cmd.description,
30+
options: [
31+
...format_arguments(cmd.arguments),
32+
...format_subcommands(cmd.subcommands),
33+
],
34+
})),
4735
);
4836
}

packages/discord/src/errors.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
import { DiscordAPIError } from '@discordjs/rest';
22
import { log_error } from '@lightning/lightning';
33

4+
const errors = [
5+
[30007, 'Too many webhooks in channel, try deleting some', false],
6+
[30058, 'Too many webhooks in guild, try deleting some', false],
7+
[50013, 'Missing permissions to make webhook', false],
8+
[10003, 'Unknown channel, disabling channel', true],
9+
[10015, 'Unknown message, disabling channel', true],
10+
[50027, 'Invalid webhook token, disabling channel', true],
11+
[0, 'Unknown DiscordAPIError, not disabling channel', false],
12+
] as const;
13+
414
export function handle_error(
515
err: unknown,
616
channel: string,
717
edit?: boolean,
818
) {
919
if (err instanceof DiscordAPIError) {
10-
if (err.code === 30007 || err.code === 30058) {
11-
log_error(err, {
12-
message: 'too many webhooks in channel/guild. try deleting some',
13-
extra: { channel },
14-
});
15-
} else if (err.code === 50013) {
16-
log_error(err, {
17-
message: 'missing permissions to create webhook. check bot permissions',
18-
extra: { channel },
19-
});
20-
} else if (err.code === 10003 || err.code === 10015 || err.code === 50027) {
21-
log_error(err, {
22-
disable: true,
23-
message: `disabling channel due to error code ${err.code}`,
24-
extra: { channel },
25-
});
26-
} else if (edit && err.code === 10008) {
27-
return []; // message already deleted or non-existent
28-
} else {
29-
log_error(err, {
30-
message: `unknown discord api error`,
31-
extra: { channel, code: err.code },
32-
});
33-
}
20+
if (edit && err.code === 10008) return []; // message already deleted or non-existent
21+
22+
const extra = { channel, code: err.code };
23+
const [, message, disable] = errors.find((e) => e[0] === err.code) ??
24+
errors[errors.length - 1];
25+
26+
log_error(err, { disable, message, extra });
3427
} else {
3528
log_error(err, {
3629
message: `unknown discord error`,

packages/discord/src/incoming.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function get_deleted_message(
2626
}
2727

2828
async function fetch_author(api: API, data: GatewayMessageUpdateDispatchData) {
29-
let profile = data.author.avatar !== null
29+
let profile = data.author.avatar
3030
? `https://cdn.discordapp.com/avatars/${data.author.id}/${data.author.avatar}.png`
3131
: `https://cdn.discordapp.com/embed/avatars/${
3232
Number(BigInt(data.author.id) >> 22n) % 6
@@ -84,17 +84,9 @@ export async function get_incoming_message(
8484
{ api, data }: { api: API; data: GatewayMessageUpdateDispatchData },
8585
): Promise<message | undefined> {
8686
// normal messages, replies, and user joins
87-
if (
88-
data.type !== 0 &&
89-
data.type !== 7 &&
90-
data.type !== 19 &&
91-
data.type !== 20 &&
92-
data.type !== 23
93-
) {
94-
return;
95-
}
87+
if (![0, 7, 19, 20, 23].includes(data.type)) return;
9688

97-
const message: message = {
89+
return {
9890
attachments: [
9991
...data.attachments?.map(
10092
(i: typeof data['attachments'][0]) => {
@@ -135,8 +127,6 @@ export async function get_incoming_message(
135127
Number(BigInt(data.id) >> 22n) + 1420070400000,
136128
),
137129
};
138-
139-
return message;
140130
}
141131

142132
export function get_incoming_command(

packages/discord/src/mod.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { WebSocketManager } from '@discordjs/ws';
44
import {
55
type bridge_message_opts,
66
type command,
7+
type config_schema,
78
type deleted_message,
8-
log_error,
99
type message,
1010
plugin,
1111
} from '@lightning/lightning';
@@ -24,16 +24,11 @@ export type discord_config = {
2424
token: string;
2525
};
2626

27-
/** check if something is actually a config object, return if it is */
28-
export function parse_config(v: unknown): discord_config {
29-
if (typeof v !== 'object' || v === null) {
30-
log_error("discord config isn't an object!", { without_cause: true });
31-
}
32-
if (!('token' in v) || typeof v.token !== 'string') {
33-
log_error("discord token isn't a string", { without_cause: true });
34-
}
35-
return { token: v.token };
36-
}
27+
/** the config schema for the class */
28+
export const schema: config_schema = {
29+
name: 'bolt-discord',
30+
keys: { token: { type: 'string', required: true } },
31+
};
3732

3833
/** discord support for lightning */
3934
export default class discord extends plugin {

packages/guilded/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ your `lightning.toml` file:
88

99
```toml
1010
[[plugins]]
11-
plugin = "jsr:@lightning/guilded@0.8.0-alpha.2"
11+
plugin = "jsr:@lightning/guilded@0.8.0-alpha.3"
1212
config.token = "your_bot_token"
1313
```

packages/guilded/deno.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "@lightning/guilded",
3-
"version": "0.8.0-alpha.2",
3+
"version": "0.8.0-alpha.3",
44
"license": "MIT",
55
"exports": "./src/mod.ts",
66
"imports": {
7-
"@lightning/lightning": "jsr:@lightning/lightning@0.8.0-alpha.2",
8-
"@jersey/guildapi": "jsr:@jersey/guildapi@^0.0.4",
7+
"@lightning/lightning": "jsr:@lightning/lightning@0.8.0-alpha.3",
8+
"@jersey/guildapi": "jsr:@jersey/guildapi@^0.0.5",
99
"@jersey/guilded-api-types": "jsr:@jersey/guilded-api-types@^0.0.2"
1010
}
1111
}

packages/guilded/src/errors.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import { RequestError } from '@jersey/guilded-api-types';
22
import { log_error } from '@lightning/lightning';
33

4+
const errors = [
5+
[403, 'No permission to send/delete messages! Check permissions', true],
6+
[404, 'Not found! This might be a Guilded problem if making a bridge', true],
7+
[0, 'Unknown Guilded error, not disabling channel', false],
8+
] as const;
9+
410
export function handle_error(err: unknown, channel: string): never {
511
if (err instanceof RequestError) {
6-
if (err.cause.status === 404) {
7-
log_error(err, {
8-
message:
9-
"resource not found! if you're trying to make a bridge, this is likely an issue with Guilded",
10-
extra: { channel_id: channel, response: err.cause },
11-
disable: true,
12-
});
13-
} else if (err.cause.status === 403) {
14-
log_error(err, {
15-
message: 'no permission to send/delete messages! check bot permissions',
16-
extra: { channel_id: channel, response: err.cause },
17-
disable: true,
18-
});
19-
} else {
20-
log_error(err, {
21-
message: `unknown guilded error with status code ${err.cause.status}`,
22-
extra: { channel_id: channel, response: err.cause },
23-
});
24-
}
12+
const [, message, disable] = errors.find((e) =>
13+
e[0] === err.cause.status
14+
) ??
15+
errors[errors.length - 1];
16+
17+
log_error(err, {
18+
disable,
19+
extra: { channel_id: channel, response: err.cause },
20+
message,
21+
});
2522
} else {
2623
log_error(err, {
2724
message: `unknown error`,

0 commit comments

Comments
 (0)