start adding command handlers

This commit is contained in:
Cadence Ember 2023-08-24 17:09:25 +12:00
parent 40c3ef8e83
commit c56e92ccfb
3 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,81 @@
// @ts-check
const assert = require("assert").strict
const util = require("util")
const DiscordTypes = require("discord-api-types/v10")
const {discord, sync, db} = require("../passthrough")
/** @type {import("../matrix/api")}) */
const api = sync.require("../matrix/api")
const prefix = "/"
/**
* @callback CommandExecute
* @param {DiscordTypes.GatewayMessageCreateDispatchData} message
* @param {DiscordTypes.APIGuildTextChannel} channel
* @param {DiscordTypes.APIGuild} guild
* @param {any} [ctx]
*/
/**
* @typedef Command
* @property {string[]} aliases
* @property {(message: DiscordTypes.GatewayMessageCreateDispatchData, channel: DiscordTypes.APIGuildTextChannel, guild: DiscordTypes.APIGuild) => Promise<any>} execute
*/
/** @param {CommandExecute} execute */
function replyctx(execute) {
/** @type {CommandExecute} */
return function(message, channel, guild, ctx = {}) {
ctx.message_reference = {
message_id: message.id,
channel_id: channel.id,
guild_id: guild.id,
fail_if_not_exists: false
}
return execute(message, channel, guild, ctx)
}
}
/** @type {Command[]} */
const commands = [{
aliases: ["icon", "avatar", "roomicon", "roomavatar", "channelicon", "channelavatar"],
execute: replyctx(
async (message, channel, guild, ctx) => {
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
if (!roomID) return discord.snow.channel.createMessage(channel.id, {
...ctx,
content: "This channel isn't bridged to the other side."
})
const avatarEvent = await api.getStateEvent(roomID, "m.room.avatar", "")
const avatarURL = avatarEvent?.url
return discord.snow.channel.createMessage(channel.id, {
...ctx,
content: `Current room avatar: ${avatarURL}`
})
}
)
}, {
aliases: ["invite"],
execute: replyctx(
async (message, channel, guild, ctx) => {
discord.snow.channel.createMessage(channel.id, {
...ctx,
content: "This command isn't implemented yet."
})
}
)
}]
/** @type {CommandExecute} */
async function execute(message, channel, guild) {
if (!message.content.startsWith(prefix)) return
const words = message.content.split(" ")
const commandName = words[0]
const command = commands.find(c => c.aliases.includes(commandName))
if (!command) return
await command.execute(message, channel, guild)
}
module.exports.execute = execute

View File

@ -18,6 +18,8 @@ const createRoom = sync.require("./actions/create-room")
const createSpace = sync.require("./actions/create-space")
/** @type {import("../matrix/api")}) */
const api = sync.require("../matrix/api")
/** @type {import("./discord-command-handler")}) */
const discordCommandHandler = sync.require("./discord-command-handler")
let lastReportedEvent = 0
@ -156,7 +158,11 @@ module.exports = {
if (!channel.guild_id) return // Nothing we can do in direct messages.
const guild = client.guilds.get(channel.guild_id)
if (!isGuildAllowed(guild.id)) return
await sendMessage.sendMessage(message, guild)
await Promise.all([
sendMessage.sendMessage(message, guild),
discordCommandHandler.execute(message, channel, guild)
])
},
/**

View File

@ -190,6 +190,7 @@ module.exports.inviteToRoom = inviteToRoom
module.exports.leaveRoom = leaveRoom
module.exports.getEvent = getEvent
module.exports.getAllState = getAllState
module.exports.getStateEvent = getStateEvent
module.exports.getJoinedMembers = getJoinedMembers
module.exports.sendState = sendState
module.exports.sendEvent = sendEvent