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 () => { const client = new Client('http://localhost:3000', { 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; await discordWebhook.send(e.content, { username: e.author.username }); }); }); }; main();