diff --git a/src/discord/interactions/invite.js b/src/discord/interactions/invite.js index 0afc6d8a..89403635 100644 --- a/src/discord/interactions/invite.js +++ b/src/discord/interactions/invite.js @@ -12,6 +12,8 @@ const createRoom = sync.require("../../d2m/actions/create-room") const createSpace = sync.require("../../d2m/actions/create-space") /** @type {import("../../matrix/api")} */ const api = sync.require("../../matrix/api") +/** @type {import("../../matrix/read-registration")} */ +const {reg} = sync.require("../../matrix/read-registration") /** * @param {DiscordTypes.APIChatInputApplicationCommandGuildInteraction & {channel: DiscordTypes.APIGuildTextChannel}} interaction @@ -19,6 +21,16 @@ const api = sync.require("../../matrix/api") * @returns {AsyncGenerator<{[k in keyof InteractionMethods]?: Parameters[2]}>} */ async function* _interact({data, channel, guild_id}, {api}) { + // Check guild exists - it might not exist if the application was added with applications.commands scope and not bot scope + const guild = discord.guilds.get(guild_id) + if (!guild) return yield {createInteractionResponse: { + type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource, + data: { + content: `I can't perform actions in this server because there is no bot presence in the server. You should try re-adding this bot to the server, making sure that it has bot scope (not just commands).\nIf you add the bot from ${reg.ooye.bridge_origin} this should work automatically.`, + flags: DiscordTypes.MessageFlags.Ephemeral + } + }} + // Get named MXID /** @type {DiscordTypes.APIApplicationCommandInteractionDataStringOption[] | undefined} */ // @ts-ignore const options = data.options @@ -32,9 +44,6 @@ async function* _interact({data, channel, guild_id}, {api}) { } }} - const guild = discord.guilds.get(guild_id) - assert(guild) - // Ensure guild and room are bridged db.prepare("INSERT OR IGNORE INTO guild_active (guild_id, autocreate) VALUES (?, 1)").run(guild_id) const existing = createRoom.existsOrAutocreatable(channel, guild_id) diff --git a/src/discord/interactions/invite.test.js b/src/discord/interactions/invite.test.js index 1890b762..8290718c 100644 --- a/src/discord/interactions/invite.test.js +++ b/src/discord/interactions/invite.test.js @@ -43,6 +43,21 @@ test("invite: checks for invalid matrix ID", async t => { t.equal(msgs[0].createInteractionResponse.data.content, "You have to say the Matrix ID of the person you want to invite. Matrix IDs look like this: `@username:example.org`") }) +test("invite: checks if guild exists", async t => { // it might not exist if the application was added with applications.commands scope and not bot scope + const msgs = await fromAsync(_interact({ + data: { + options: [{ + name: "user", + type: DiscordTypes.ApplicationCommandOptionType.String, + value: "@cadence:cadence.moe" + }] + }, + channel: discord.channels.get("0"), + guild_id: "0" + }, {})) + t.match(msgs[0].createInteractionResponse.data.content, /there is no bot presence in the server/) +}) + test("invite: checks if channel exists or is autocreatable", async t => { db.prepare("UPDATE guild_active SET autocreate = 0").run() const msgs = await fromAsync(_interact({