From 708955c0629f0e589c7a8dd967835dced5223e41 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 2 Oct 2023 21:48:06 +1300 Subject: [PATCH] Add "addbot" feature --- addbot.js | 15 +++++++++++++++ addbot.sh | 3 +++ discord/discord-command-handler.js | 11 +++++++++++ package.json | 1 + readme.md | 7 +++++++ stdin.js | 4 +++- 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 addbot.js create mode 100755 addbot.sh diff --git a/addbot.js b/addbot.js new file mode 100644 index 0000000..667fbab --- /dev/null +++ b/addbot.js @@ -0,0 +1,15 @@ +// @ts-check + +const config = require("./config") + +function addbot() { + const token = config.discordToken + const id = Buffer.from(token.split(".")[0], "base64") + return `Open this link to add the bot to a Discord server:\nhttps://discord.com/oauth2/authorize?client_id=${id}&scope=bot&permissions=1610883072 ` +} + +if (process.argv.find(a => a.endsWith("addbot") || a.endsWith("addbot.js"))) { + console.log(addbot()) +} + +module.exports.addbot = addbot diff --git a/addbot.sh b/addbot.sh new file mode 100755 index 0000000..6c3ff4b --- /dev/null +++ b/addbot.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env sh +echo "Open this link to add the bot to a Discord server:" +echo "https://discord.com/oauth2/authorize?client_id=$(grep discordToken config.js | sed -E 's!.*: ["'\'']([A-Za-z0-9+=/_-]*).*!\1!g' | base64 -d)&scope=bot&permissions=1610883072" diff --git a/discord/discord-command-handler.js b/discord/discord-command-handler.js index f364f63..8fb6aec 100644 --- a/discord/discord-command-handler.js +++ b/discord/discord-command-handler.js @@ -4,6 +4,7 @@ const assert = require("assert").strict const util = require("util") const DiscordTypes = require("discord-api-types/v10") const reg = require("../matrix/read-registration") +const {addbot} = require("../addbot") const {discord, sync, db, select} = require("../passthrough") /** @type {import("../matrix/api")}) */ @@ -201,6 +202,16 @@ const commands = [{ }) } ) +}, { + aliases: ["addbot"], + execute: replyctx( + async (message, channel, guild, ctx) => { + return discord.snow.channel.createMessage(channel.id, { + ...ctx, + content: addbot() + }) + } + ) }] /** @type {CommandExecute} */ diff --git a/package.json b/package.json index ad89303..5dc1792 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "tap-dot": "github:cloudrac3r/tap-dot#9dd7750ececeae3a96afba91905be812b6b2cc2d" }, "scripts": { + "addbot": "node addbot.js", "test": "cross-env FORCE_COLOR=true supertape --no-check-assertions-count --format tap test/test.js | tap-dot", "test-slow": "cross-env FORCE_COLOR=true supertape --no-check-assertions-count --format tap test/test.js -- --slow | tap-dot", "cover": "c8 --skip-full -r html -r text supertape --no-check-assertions-count --format fail test/test.js" diff --git a/readme.md b/readme.md index 75a73ab..6155342 100644 --- a/readme.md +++ b/readme.md @@ -73,6 +73,13 @@ Follow these steps: 7. Start the bridge: `node start.js` +8. Add the bot to a server - use any *one* of the following commands for an invite link: + * (in the REPL) `addbot` + * (in a chat) `//addbot` + * $ `node addbot.js` + * $ `npm run addbot` + * $ `./addbot.sh` + # Development information * Be sure to install dependencies with `--save-dev` so you can run the tests. diff --git a/stdin.js b/stdin.js index a687c6c..c99a68b 100644 --- a/stdin.js +++ b/stdin.js @@ -2,9 +2,10 @@ const repl = require("repl") const util = require("util") +const {addbot} = require("./addbot") const passthrough = require("./passthrough") -const { discord, config, sync, db } = passthrough +const {discord, config, sync, db} = passthrough const data = sync.require("./test/data") const createSpace = sync.require("./d2m/actions/create-space") @@ -39,6 +40,7 @@ setImmediate(() => { // assign after since old extraContext data will get remove async function customEval(input, _context, _filename, callback) { let depth = 0 if (input === "exit\n") return process.exit() + if (input === "addbot\n") return callback(null, addbot()) if (input.startsWith(":")) { const depthOverwrite = input.split(" ")[0] depth = +depthOverwrite.slice(1)