OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / hyper-social / bridge-discord / index.js

74 lines · javascript ref:
import { Bridge } from './lib/bridge.js'

const args = parseArgs(typeof Pear !== 'undefined' ? Pear.config.args : process.argv.slice(2))

if (args.help) {
  console.log(`
bridge-discord — Discord ↔ Keet bidirectional bridge

Usage:
  bridge-discord --token <bot-token> --channel <channelId> --keet-room <roomId>

Options:
  --token <token>        Discord bot token (required)
  --channel <id>         Discord channel ID to bridge (required)
  --guild <id>           Discord guild/server ID (optional)
  --keet-room <roomId>   Keet room ID to bridge (required)
  --keet-port <port>     Keet daemon port (default: 3117)
  --help                 Show this help

Environment:
  DISCORD_TOKEN          Alternative to --token flag

Setup:
  1. Create a Discord bot at https://discord.com/developers/applications
  2. Enable MESSAGE CONTENT intent in Bot settings
  3. Invite bot to your server with permissions: Send Messages, Read Message History
  4. Get channel ID (right-click channel → Copy Channel ID with Developer Mode on)
`)
  process.exit(0)
}

const token = args.token || process.env.DISCORD_TOKEN
if (!token || !args.channel || !args['keet-room']) {
  console.error('Error: --token (or DISCORD_TOKEN env), --channel, and --keet-room are required')
  console.error('Run with --help for usage')
  process.exit(1)
}

const bridge = new Bridge({
  discordToken: token,
  discordChannel: args.channel,
  discordGuild: args.guild,
  keetRoom: args['keet-room'],
  keetPort: parseInt(args['keet-port'] || '3117')
})

bridge.start().catch((err) => {
  console.error('Bridge failed to start:', err.message)
  process.exit(1)
})

process.on('SIGINT', () => bridge.stop().then(() => process.exit(0)))
process.on('SIGTERM', () => bridge.stop().then(() => process.exit(0)))

function parseArgs (argv) {
  const result = {}
  for (let i = 0; i < argv.length; i++) {
    const arg = argv[i]
    if (arg === '--help') {
      result.help = true
    } else if (arg.startsWith('--')) {
      const key = arg.slice(2)
      const next = argv[i + 1]
      if (next && !next.startsWith('--')) {
        result[key] = next
        i++
      } else {
        result[key] = true
      }
    }
  }
  return result
}