When inviting bot, check it has bot scope

This commit is contained in:
Cadence Ember 2024-11-02 19:22:30 +13:00
parent 14115c0e06
commit 15e5b17b0d
2 changed files with 27 additions and 3 deletions

View file

@ -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<InteractionMethods[k]>[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)

View file

@ -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({