bridge replies from revolt
This commit is contained in:
parent
45a9021832
commit
62c3c08adf
2 changed files with 108 additions and 4 deletions
|
@ -2,9 +2,10 @@ import axios from "axios";
|
||||||
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
|
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
|
||||||
import { client } from "./client";
|
import { client } from "./client";
|
||||||
import { client as discordClient } from "../discord/client";
|
import { client as discordClient } from "../discord/client";
|
||||||
import { WebhookClient } from "discord.js";
|
import { MessageEmbed, WebhookClient } from "discord.js";
|
||||||
import GenericEmbed from "../types/GenericEmbed";
|
import GenericEmbed from "../types/GenericEmbed";
|
||||||
import { SendableEmbed } from "revolt-api";
|
import { SendableEmbed } from "revolt-api";
|
||||||
|
import { clipText, discordFetchMessage, discordFetchUser, revoltFetchMessage, revoltFetchUser } from "../util";
|
||||||
|
|
||||||
client.on('message', async message => {
|
client.on('message', async message => {
|
||||||
try {
|
try {
|
||||||
|
@ -14,7 +15,7 @@ client.on('message', async message => {
|
||||||
const [ bridgeCfg, bridgedMsg, ...repliedMessages ] = await Promise.all([
|
const [ bridgeCfg, bridgedMsg, ...repliedMessages ] = await Promise.all([
|
||||||
BRIDGE_CONFIG.findOne({ revolt: message.channel_id }),
|
BRIDGE_CONFIG.findOne({ revolt: message.channel_id }),
|
||||||
BRIDGED_MESSAGES.findOne({ "revolt.nonce": message.nonce }),
|
BRIDGED_MESSAGES.findOne({ "revolt.nonce": message.nonce }),
|
||||||
//...(message.reply_ids?.map(id => BRIDGED_MESSAGES.findOne({ "revolt.messageId": id })) ?? [])
|
...(message.reply_ids?.map(id => BRIDGED_MESSAGES.findOne({ "revolt.messageId": id })) ?? [])
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (bridgedMsg) return logger.debug(`Revolt: Message has already been bridged; ignoring`);
|
if (bridgedMsg) return logger.debug(`Revolt: Message has already been bridged; ignoring`);
|
||||||
|
@ -46,7 +47,7 @@ client.on('message', async message => {
|
||||||
token: bridgeCfg.discordWebhook.token,
|
token: bridgeCfg.discordWebhook.token,
|
||||||
});
|
});
|
||||||
|
|
||||||
client.send({
|
const payload = {
|
||||||
content: `${message.content}`,
|
content: `${message.content}`,
|
||||||
username: message.author?.username ?? 'Unknown user',
|
username: message.author?.username ?? 'Unknown user',
|
||||||
avatarURL: message.author?.generateAvatarURL({ max_side: 128 }),
|
avatarURL: message.author?.generateAvatarURL({ max_side: 128 }),
|
||||||
|
@ -55,7 +56,44 @@ client.on('message', async message => {
|
||||||
.filter(e => e.type == "Text")
|
.filter(e => e.type == "Text")
|
||||||
.map(e => new GenericEmbed(e as SendableEmbed).toDiscord())
|
.map(e => new GenericEmbed(e as SendableEmbed).toDiscord())
|
||||||
: undefined,
|
: undefined,
|
||||||
})
|
};
|
||||||
|
|
||||||
|
if (repliedMessages.length) {
|
||||||
|
const embed = new MessageEmbed().setColor('#2f3136');
|
||||||
|
|
||||||
|
if (repliedMessages.length == 1) {
|
||||||
|
const replyMsg = await discordFetchMessage(repliedMessages[0]?.discord.messageId, bridgeCfg.discord);
|
||||||
|
const author = replyMsg?.author;
|
||||||
|
|
||||||
|
embed.setAuthor({
|
||||||
|
name: `@${author?.username ?? 'Unknown'}`, // todo: check if @pinging was enabled for reply
|
||||||
|
iconURL: author?.displayAvatarURL({ size: 64, dynamic: true }),
|
||||||
|
url: replyMsg?.url,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (replyMsg?.content) embed.setDescription('>>> ' + clipText(replyMsg.content, 200));
|
||||||
|
} else {
|
||||||
|
const replyMsgs = await Promise.all(
|
||||||
|
repliedMessages.map(m => discordFetchMessage(m?.discord.messageId, bridgeCfg.discord))
|
||||||
|
);
|
||||||
|
|
||||||
|
embed.setAuthor({ name: repliedMessages.length + ' replies' });
|
||||||
|
|
||||||
|
for (const msg of replyMsgs) {
|
||||||
|
embed.addField(
|
||||||
|
`@${msg?.author.username ?? 'Unknown'}`,
|
||||||
|
(msg ? `[Link](${msg.url})\n` : '') +
|
||||||
|
'>>> ' + clipText(msg?.content ?? '\u200b', 100),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (payload.embeds) payload.embeds.unshift(embed);
|
||||||
|
else payload.embeds = [ embed ];
|
||||||
|
}
|
||||||
|
|
||||||
|
client.send(payload)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
await BRIDGED_MESSAGES.update({
|
await BRIDGED_MESSAGES.update({
|
||||||
"revolt.messageId": message._id
|
"revolt.messageId": message._id
|
||||||
|
|
66
bridge/src/util.ts
Normal file
66
bridge/src/util.ts
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import { Channel } from "@janderedev/revolt.js/dist/maps/Channels";
|
||||||
|
import { Message } from "@janderedev/revolt.js/dist/maps/Messages";
|
||||||
|
import { User } from "@janderedev/revolt.js/dist/maps/Users";
|
||||||
|
import { Message as DiscordMessage, TextChannel, User as DiscordUser } from "discord.js";
|
||||||
|
import { client as discordClient } from "./discord/client";
|
||||||
|
import { client as revoltClient } from "./revolt/client"
|
||||||
|
|
||||||
|
// Grab user from cache or fetch, return undefined if error
|
||||||
|
async function revoltFetchUser(id?: string): Promise<User|undefined> {
|
||||||
|
if (!id) return undefined;
|
||||||
|
|
||||||
|
let user = revoltClient.users.get(id);
|
||||||
|
if (user) return user;
|
||||||
|
|
||||||
|
try { user = await revoltClient.users.fetch(id) } catch(_) { }
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function revoltFetchMessage(id: string, channel: Channel): Promise<Message|undefined> {
|
||||||
|
let message = revoltClient.messages.get(id);
|
||||||
|
if (message) return message;
|
||||||
|
|
||||||
|
try { message = await channel.fetchMessage(id) } catch(_) { }
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function discordFetchMessage(id?: string, channelId?: string): Promise<DiscordMessage|undefined> {
|
||||||
|
if (!id || !channelId) return undefined;
|
||||||
|
|
||||||
|
const channel = discordClient.channels.cache.get(channelId);
|
||||||
|
if (!channel || !(channel instanceof TextChannel)) return undefined;
|
||||||
|
|
||||||
|
let message = channel.messages.cache.get(id);
|
||||||
|
if (message) return message;
|
||||||
|
|
||||||
|
try { message = await channel.messages.fetch(id) } catch(_) { }
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// doesnt seem to work idk
|
||||||
|
async function discordFetchUser(id?: string): Promise<DiscordUser|undefined> {
|
||||||
|
if (!id) return undefined;
|
||||||
|
|
||||||
|
let user = discordClient.users.cache.get(id);
|
||||||
|
if (user) return user;
|
||||||
|
|
||||||
|
try { user = await discordClient.users.fetch(id) } catch(_) { }
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clipText(text: string, limit: number) {
|
||||||
|
if (text.length < limit) return text;
|
||||||
|
else return text.substring(0, limit-4) + ' ...';
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
revoltFetchUser,
|
||||||
|
revoltFetchMessage,
|
||||||
|
discordFetchMessage,
|
||||||
|
discordFetchUser,
|
||||||
|
clipText,
|
||||||
|
}
|
Loading…
Reference in a new issue