Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.8.0 develop #72

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
run: cd packages/lightning
- name: publish to jsr
run: |
deno publish
cd packages/lightning
deno publish
cd packages/lightning
- name: setup docker metadata
id: metadata
uses: docker/metadata-action@v5
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.env
/config
/config.ts
packages/postgres
2 changes: 2 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
},
"workspace": [
"./packages/lightning",
// TODO(jersey): contribute upstream
"./packages/postgres",
"./packages/lightning-plugin-telegram",
"./packages/lightning-plugin-revolt",
"./packages/lightning-plugin-guilded",
Expand Down
12 changes: 6 additions & 6 deletions packages/lightning-plugin-discord/deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@jersey/lightning-plugin-discord",
"version": "0.7.4",
"version": "0.8.0",
"exports": "./src/mod.ts",
"imports": {
"@jersey/lightning": "jsr:@jersey/lightning@^0.7.3",
"@discordjs/core": "npm:@discordjs/core@^1.2.0",
"@discordjs/rest": "npm:@discordjs/rest@^2.3.0",
"@discordjs/ws": "npm:@discordjs/ws@^1.1.1",
"discord-api-types": "npm:[email protected].83/v10"
"@jersey/lightning": "jsr:@jersey/lightning@^0.8.0",
"@discordjs/core": "npm:@discordjs/core@^2.0.0",
"@discordjs/rest": "npm:@discordjs/rest@^2.4.0",
"@discordjs/ws": "npm:@discordjs/ws@^2.0.0",
"discord-api-types": "npm:[email protected].97/v10"
}
}
82 changes: 82 additions & 0 deletions packages/lightning-plugin-discord/src/bridge_to_discord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { create_opts, delete_opts, edit_opts } from '@jersey/lightning';
import { message_to_discord } from './discord_message/mod.ts';
import { error_handler } from './error_handler.ts';
import type { API } from '@discordjs/core';

type data = { id: string; token: string };

export async function setup_bridge(api: API, channel: string) {
try {
const { id, token } = await api.channels.createWebhook(
channel,
{
name: 'lightning bridge',
},
);

return { id, token };
} catch (e) {
return error_handler(e, channel, 'setting up channel');
}
}

export async function create_message(api: API, opts: create_opts) {
const data = opts.channel.data as data;
const transformed = await message_to_discord(
opts.msg,
api,
opts.channel.id,
opts.reply_id,
);

try {
const res = await api.webhooks.execute(
data.id,
data.token,
transformed,
);

return [res.id];
} catch (e) {
return error_handler(e, opts.channel.id, 'creating message');
}
}

export async function edit_message(api: API, opts: edit_opts) {
const data = opts.channel.data as data;
const transformed = await message_to_discord(
opts.msg,
api,
opts.channel.id,
opts.reply_id,
);

try {
await api.webhooks.editMessage(
data.id,
data.token,
opts.edit_ids[0],
transformed,
);

return opts.edit_ids;
} catch (e) {
return error_handler(e, opts.channel.id, 'editing message');
}
}

export async function delete_message(api: API, opts: delete_opts) {
const data = opts.channel.data as data;

try {
await api.webhooks.deleteMessage(
data.id,
data.token,
opts.edit_ids[0],
);

return opts.edit_ids;
} catch (e) {
return error_handler(e, opts.channel.id, 'editing message');
}
}
69 changes: 0 additions & 69 deletions packages/lightning-plugin-discord/src/commands.ts

This file was deleted.

83 changes: 0 additions & 83 deletions packages/lightning-plugin-discord/src/discord.ts

This file was deleted.

31 changes: 31 additions & 0 deletions packages/lightning-plugin-discord/src/discord_message/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { attachment } from '@jersey/lightning';
import type { RawFile } from '@discordjs/rest';

export async function files_up_to_25MiB(attachments: attachment[] | undefined) {
if (!attachments) return;

const files: RawFile[] = [];
const total_size = 0;

for (const attachment of attachments) {
if (attachment.size >= 25) continue;
if (total_size + attachment.size >= 25) break;

try {
const data = new Uint8Array(
await (await fetch(attachment.file, {
signal: AbortSignal.timeout(5000),
})).arrayBuffer(),
);

files.push({
name: attachment.name ?? attachment.file.split('/').pop()!,
data,
});
} catch {
continue;
}
}

return files;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { GatewayMessageUpdateDispatchData } from 'discord-api-types';
import type { API } from '@discordjs/core';
import { calculateUserDefaultAvatarIndex } from '@discordjs/rest';

export async function get_author(
api: API,
message: GatewayMessageUpdateDispatchData,
) {
let name = message.author?.global_name || message.author?.username ||
'discord user';
let avatar = message.author?.avatar
? `https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.png`
: `https://cdn.discordapp.com/embed/avatars/${
calculateUserDefaultAvatarIndex(
message.author?.id || '360005875697582081',
)
}.png`;

const channel = await api.channels.get(message.channel_id);

if ('guild_id' in channel && channel.guild_id && message.author) {
try {
const member = await api.guilds.getMember(
channel.guild_id,
message.author.id,
);

if (member.nick !== null && member.nick !== undefined) {
name = member.nick;
}
avatar = member.avatar
? `https://cdn.discordapp.com/guilds/${channel.guild_id}/users/${message.author.id}/avatars/${member.avatar}.png`
: avatar;
} catch {
// safe to ignore
}
}

return { name, avatar };
}
Loading