brainlet/libbrainlet/bridgebot.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

const Client = require('./index');
const secret = require('./bridgebotsecret');
const fetch = require('node-fetch');
const Discord = require('discord.js');
const LISTEN_ON = '5fce94aa37c98b23bcd140a0';
const DISCORD_LISTEN_ON = '785494888766373909';
const PREFIX = '::';
const ADMIN_ID = '5fc828ea4e96e00725c17fd7';
let discordWebhook;
const main = async () => {
2020-12-08 01:21:10 +02:00
const client = new Client('https://b.hippoz.xyz', {
throwErrors: true
});
const discord = new Discord.Client();
const res = await fetch(`${client.url}/api/v1/users/token/create`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: secret.brainlet.user.username,
password: secret.brainlet.user.password
})
});
const json = await res.json();
if (!res.ok || json.error) throw new Error('Failed to generate token from API endpoint');
await client.setToken(json.token);
await client.gatewayConnect();
client.gateway.on('connect', () => {
discord.on('ready', async () => {
const channel = discord.channels.cache.get(DISCORD_LISTEN_ON);
const webhooks = await channel.fetchWebhooks();
discordWebhook = webhooks.first();
if (!discordWebhook) throw new Error('No webhook in selected channel');
console.log(`[*] [DISCORD] Logged in as ${discord.user.tag}!`);
});
discord.login(secret.discord.token);
const category = client.gateway.subscribeToCategoryChat(LISTEN_ON);
discord.on('message', (e) => {
if (e.webhookID) return;
if (e.author.bot) return;
client.gateway.sendMessage(LISTEN_ON, e.content, {
nickAuthor: { username: e.author.username }
});
});
client.gateway.on('message', async (e) => {
if (e.author._id === client.user._id) return;
2020-12-08 01:21:10 +02:00
e.content = e.content.replace(/@/g, '');
await discordWebhook.send(e.content, {
username: e.author.username
});
});
});
};
main();