bridge kinda works
This commit is contained in:
parent
cc4f18a9b0
commit
7379b0106a
4 changed files with 81 additions and 7 deletions
|
@ -8,8 +8,10 @@ import BridgedMessage from "../types/BridgedMessage";
|
|||
client.on('messageCreate', async message => {
|
||||
try {
|
||||
logger.debug(`[M] Discord: ${message.content}`);
|
||||
|
||||
const bridgeCfg = await BRIDGE_CONFIG.findOne({ discord: message.channelId });
|
||||
if (message.webhookId && bridgeCfg?.discordWebhook?.id == message.webhookId) {
|
||||
return logger.debug(`Discord: Message has already been bridged; ignoring`);
|
||||
}
|
||||
if (!bridgeCfg?.revolt) return logger.debug(`Discord: No Revolt channel associated`);
|
||||
|
||||
const channel = revoltClient.channels.get(bridgeCfg.revolt);
|
||||
|
@ -50,7 +52,7 @@ client.on('messageCreate', async message => {
|
|||
revolt: {
|
||||
messageId: res.data._id,
|
||||
},
|
||||
} as BridgedMessage);
|
||||
});
|
||||
})
|
||||
.catch(e => {
|
||||
console.error(`Failed to send message`, e.response.data)
|
||||
|
|
|
@ -3,13 +3,16 @@ import Log75, { LogLevel } from 'log75';
|
|||
import { getDb } from './db';
|
||||
import { login as loginRevolt } from './revolt/client';
|
||||
import { login as loginDiscord } from './discord/client';
|
||||
import { ICollection } from 'monk';
|
||||
import BridgeConfig from './types/BridgeConfig';
|
||||
import BridgedMessage from './types/BridgedMessage';
|
||||
|
||||
config();
|
||||
|
||||
const logger: Log75 = new (Log75 as any).default(LogLevel.Debug);
|
||||
const db = getDb();
|
||||
const BRIDGED_MESSAGES = db.get('bridged_messages');
|
||||
const BRIDGE_CONFIG = db.get('bridge_config');
|
||||
const BRIDGED_MESSAGES: ICollection<BridgedMessage> = db.get('bridged_messages');
|
||||
const BRIDGE_CONFIG: ICollection<BridgeConfig> = db.get('bridge_config');
|
||||
|
||||
for (const v of [ 'REVOLT_TOKEN', 'DISCORD_TOKEN', 'DB_STRING' ]) {
|
||||
if (!process.env[v]) {
|
||||
|
|
|
@ -1,6 +1,62 @@
|
|||
import { logger } from "..";
|
||||
import axios from "axios";
|
||||
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
|
||||
import { client } from "./client";
|
||||
import { client as discordClient } from "../discord/client";
|
||||
|
||||
client.on('message', message => {
|
||||
client.on('message', async message => {
|
||||
try {
|
||||
logger.debug(`[M] Revolt: ${message.content}`);
|
||||
|
||||
const [ bridgedMsg, bridgeCfg ] = await Promise.all([
|
||||
BRIDGED_MESSAGES.findOne({ "revolt.messageId": message._id }),
|
||||
BRIDGE_CONFIG.findOne({ revolt: message.channel_id }),
|
||||
]);
|
||||
|
||||
if (bridgedMsg) return logger.debug(`Revolt: Message has already been bridged; ignoring`);
|
||||
if (!bridgeCfg?.discord) return logger.debug(`Revolt: No Discord channel associated`);
|
||||
if (!bridgeCfg.discordWebhook) {
|
||||
// Todo: Create a new webhook instead of exiting
|
||||
return logger.debug(`Revolt: No Discord webhook stored`);
|
||||
}
|
||||
|
||||
await BRIDGED_MESSAGES.insert({
|
||||
origin: 'revolt',
|
||||
discord: {},
|
||||
revolt: {
|
||||
messageId: message._id,
|
||||
},
|
||||
});
|
||||
|
||||
axios.post(
|
||||
`https://discord.com/api/v9/webhooks/${bridgeCfg.discordWebhook.id}/${bridgeCfg.discordWebhook.token}?wait=true`,
|
||||
{
|
||||
content: message.content,
|
||||
username: message.author?.username ?? 'Unknown user',
|
||||
avatar_url: message.author?.generateAvatarURL({ max_side: 128 }),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Authorization": `Bot ${discordClient.token}`
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(async res => {
|
||||
await BRIDGED_MESSAGES.update({
|
||||
revolt: {
|
||||
messageId: message._id
|
||||
}
|
||||
}, {
|
||||
$set: {
|
||||
discord: {
|
||||
messageId: res.data._id
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(e => {
|
||||
console.error('Failed to execute webhook', e.response.data);
|
||||
});
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
|
|
13
bridge/src/types/BridgeConfig.ts
Normal file
13
bridge/src/types/BridgeConfig.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
export default class {
|
||||
// Revolt channel ID
|
||||
revolt?: string;
|
||||
|
||||
// Discord channel ID
|
||||
discord?: string;
|
||||
|
||||
// Discord webhook
|
||||
discordWebhook?: {
|
||||
id: string;
|
||||
token: string;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue