Skip to content

Commit

Permalink
remove most references to platform(s) and simplify err and message types
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed May 6, 2024
1 parent 9aa4bd3 commit 12d298c
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 237 deletions.
23 changes: 10 additions & 13 deletions packages/bolt-discord/conv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type discord_message = Omit<update_data, 'mentions'>;
type webhook_message = wh_query & wh_token & { files?: RawFile[]; wait: true };

export async function to_discord(
message: message<unknown>,
message: message,
replied_message?: discord_message
): Promise<webhook_message> {
if (message.replytoid && replied_message) {
if (message.reply_id && replied_message) {
if (!message.embeds) message.embeds = [];
message.embeds.push(
{
Expand All @@ -46,7 +46,8 @@ export async function to_discord(
...(replied_message.embeds || []).map(i => {
return {
...i,
timestamp: i.timestamp ? Number(i.timestamp) : undefined
timestamp: i.timestamp ? Number(i.timestamp) : undefined,
video : i.video ? { ...i.video, url: i.video.url || "" } : undefined
};
})
);
Expand Down Expand Up @@ -80,7 +81,7 @@ export async function to_discord(
export async function to_core(
api: API,
message: discord_message
): Promise<message<discord_message>> {
): Promise<message> {
if (message.flags && message.flags & 128) message.content = 'Loading...';
if (message.type === 7) message.content = '*joined on discord*';
if (message.sticker_items) {
Expand Down Expand Up @@ -132,7 +133,7 @@ export async function to_core(
timestamp: i.timestamp ? Number(i.timestamp) : undefined
};
}),
reply: async (msg: message<unknown>) => {
reply: async (msg: message) => {
if (!data.author.id || data.author.id == '') return;
await api.channels.createMessage(message.channel_id, {
...(await to_discord(msg)),
Expand All @@ -141,11 +142,7 @@ export async function to_core(
}
});
},
platform: {
name: 'bolt-discord',
message,
webhookid: message.webhook_id
},
plugin: 'bolt-discord',
attachments: message.attachments?.map(i => {
return {
file: i.url,
Expand All @@ -154,9 +151,9 @@ export async function to_core(
size: i.size / 1000000
};
}),
replytoid: message.referenced_message?.id
reply_id: message.referenced_message?.id
};
return data;
return data as message;
}

export function to_command(interaction: { api: API; data: APIInteraction }) {
Expand All @@ -180,7 +177,7 @@ export function to_command(interaction: { api: API; data: APIInteraction }) {
);
},
channel: interaction.data.channel.id,
platform: 'bolt-discord',
plugin: 'bolt-discord',
opts,
timestamp: to_instant(interaction.data.id)
} as command_arguments;
Expand Down
20 changes: 8 additions & 12 deletions packages/bolt-discord/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class discord_plugin extends plugin<discord_config> {

this.bot.on(GatewayDispatchEvents.InteractionCreate, interaction => {
const cmd = conv.to_command(interaction);
if (cmd) this.emit('create_command', cmd);
if (cmd) this.lightning.run_command(cmd);
});
}

Expand All @@ -86,28 +86,24 @@ export class discord_plugin extends plugin<discord_config> {
}

create_message(
msg: message<unknown>,
msg: message,
bridge: to.channel,
_?: undefined,
replytoid?: string
reply_id?: string
): Promise<string> {
return to.send_to_discord(this.bot.api, msg, bridge, _, replytoid);
return to.send_to_discord(this.bot.api, msg, bridge, _, reply_id);
}

edit_message(
msg: message<unknown>,
msg: message,
bridge: to.channel,
edit_id: string,
replytoid?: string
reply_id?: string
): Promise<string> {
return to.send_to_discord(this.bot.api, msg, bridge, edit_id, replytoid);
return to.send_to_discord(this.bot.api, msg, bridge, edit_id, reply_id);
}

delete_message(
_msg: deleted_message<unknown>,
bridge: to.channel,
id: string
) {
delete_message(_msg: deleted_message, bridge: to.channel, id: string) {
return to.delete_on_discord(this.bot.api, bridge, id);
}
}
8 changes: 4 additions & 4 deletions packages/bolt-discord/to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export async function webhook_on_discord(api: API, channel: string) {

export async function send_to_discord(
api: API,
message: message<unknown>,
message: message,
channel: channel,
edit_id?: string,
replytoid?: string
reply_id?: string
) {
let replied_message;

if (replytoid) {
if (reply_id) {
try {
replied_message = await api.channels.getMessage(channel.id, replytoid);
replied_message = await api.channels.getMessage(channel.id, reply_id);
} catch {
// safe to ignore
}
Expand Down
1 change: 0 additions & 1 deletion packages/bolt-guilded/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ export {
type lightning,
type message
} from '../lightning/mod.ts';

12 changes: 6 additions & 6 deletions packages/bolt-guilded/guilded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function create_webhook(
type guilded_msg = RESTPostWebhookBody & { replyMessageIds?: string[] };

export async function convert_msg(
msg: message<unknown>,
msg: message,
channel?: string,
plugin?: guilded_plugin
): Promise<guilded_msg> {
Expand All @@ -74,7 +74,7 @@ export async function convert_msg(
]
} as guilded_msg;

if (msg.replytoid) message.replyMessageIds = [msg.replytoid];
if (msg.reply_id) message.replyMessageIds = [msg.reply_id];

if (msg.attachments?.length) {
if (!message.embeds) message.embeds = [];
Expand All @@ -94,7 +94,7 @@ export async function convert_msg(
return message;
}

function get_valid_username(msg: message<unknown>) {
function get_valid_username(msg: message) {
function valid(e: string) {
if (!e || e.length === 0 || e.length > 32) return false;
return /^[a-zA-Z0-9_ ()-]*$/gms.test(e);
Expand All @@ -110,15 +110,15 @@ function get_valid_username(msg: message<unknown>) {
}

async function get_reply_embeds(
msg: message<unknown>,
msg: message,
channel?: string,
plugin?: guilded_plugin
) {
if (!msg.replytoid || !channel || !plugin) return [];
if (!msg.reply_id || !channel || !plugin) return [];
try {
const msg_replied_to = await plugin.bot.messages.fetch(
channel,
msg.replytoid
msg.reply_id
);
let author;
if (!msg_replied_to.createdByWebhookId) {
Expand Down
14 changes: 5 additions & 9 deletions packages/bolt-guilded/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { guilded_plugin } from './mod.ts';
export async function tocore(
message: Message,
plugin: guilded_plugin
): Promise<message<Message> | undefined> {
): Promise<message | undefined> {
if (!message.serverId) return;
let author;
if (!message.createdByWebhookId && message.authorId !== 'Ann6LewA') {
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function tocore(
name: embed.author.name || 'embed author',
iconUrl: embed.author.iconURL || undefined,
url: embed.author.url || undefined
}
}
: undefined,
image: embed.image || undefined,
thumbnail: embed.thumbnail || undefined,
Expand All @@ -52,15 +52,11 @@ export async function tocore(
video: embed.video || undefined
};
}),
platform: {
name: 'bolt-guilded',
message,
webhookid: message.createdByWebhookId || undefined
},
reply: async (msg: message<unknown>) => {
plugin: 'bolt-guilded',
reply: async (msg: message) => {
await message.reply(await convert_msg(msg));
},
content: update_content,
replytoid: message.isReply ? message.replyMessageIds[0] : undefined
reply_id: message.isReply ? message.replyMessageIds[0] : undefined
};
}
12 changes: 6 additions & 6 deletions packages/bolt-guilded/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class guilded_plugin extends plugin<{ token: string }> {
this.emit('delete_message', {
channel: del.channelId,
id: del.id,
platform: { message: del, name: 'bolt-guilded' },
plugin: 'bolt-guilded',
timestamp: Temporal.Instant.from(del.deletedAt)
});
});
Expand All @@ -47,24 +47,24 @@ export class guilded_plugin extends plugin<{ token: string }> {
}

async create_message(
message: message<unknown>,
message: message,
channel: bridge_channel,
_?: undefined,
replytoid?: string
reply_id?: string
) {
const { id } = await new WebhookClient(
channel.data as { token: string; id: string }
).send(await convert_msg({ ...message, replytoid }, channel.id, this));
).send(await convert_msg({ ...message, reply_id }, channel.id, this));
return id;
}

// deno-lint-ignore require-await
async edit_message(_: message<unknown>, __: bridge_channel, edit_id: string) {
async edit_message(_: message, __: bridge_channel, edit_id: string) {
return edit_id;
}

async delete_message(
_message: deleted_message<unknown>,
_message: deleted_message,
channel: bridge_channel,
id: string
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/bolt-matrix/deps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export {
lightning,
plugin,
type bridge_platform,
type bridge_channel,
type message
} from 'jsr:@jersey/lightning@0.6.2';
} from '../lightning/mod.ts';
export { existsSync } from 'jsr:@std/[email protected]/exists';
export { Buffer } from 'node:buffer';
export {
Expand Down
16 changes: 8 additions & 8 deletions packages/bolt-matrix/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function onEvent(
if (event.type === 'm.room.redaction') {
this.emit('delete_message', {
id: event.redacts as string,
platform: { name: 'bolt-matrix', message: event },
plugin: 'bolt-matrix',
channel: event.room_id,
timestamp: Temporal.Instant.fromEpochMilliseconds(event.origin_server_ts)
});
Expand All @@ -45,7 +45,7 @@ export async function messageToCore(
event: WeakEvent,
intent: Intent,
homeserverUrl: string
): Promise<message<WeakEvent>> {
): Promise<message> {
console.log(event.content.formatted_body);
const sender = await intent.getProfileInfo(event.sender);
const is_reply = event.content['m.relates_to']
Expand All @@ -62,7 +62,7 @@ export async function messageToCore(
? `${sender.avatar_url?.replace(
'mxc://',
`${homeserverUrl}/_matrix/media/v3/thumbnail/`
)}?width=96&height=96&method=scale`
)}?width=96&height=96&method=scale`
: undefined
},
channel: event.room_id,
Expand All @@ -75,20 +75,20 @@ export async function messageToCore(
? (event.content.formatted_body as string).replace(
/<mx-reply>(.*?)<\/mx-reply>/i,
''
)
)
: ((event.content['m.new_content']?.body ||
event.content.body) as string),
reply: async (msg: message<unknown>) => {
reply: async (msg: message) => {
await intent.sendMessage(event.room_id, coreToMessage(msg));
},
replytoid: event.content['m.relates_to']
reply_id: event.content['m.relates_to']
? event.content['m.relates_to']['m.in_reply_to']?.event_id
: undefined,
platform: { name: 'bolt-matrix', message: event }
plugin: 'bolt-matrix'
};
}

export function coreToMessage(msg: message<unknown>) {
export function coreToMessage(msg: message) {
return {
body: msg.content
? msg.content
Expand Down
Loading

0 comments on commit 12d298c

Please sign in to comment.