From 0f1cf7a20c9b22ef5801146e852f8e3b3483701f Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 7 Mar 2024 09:13:25 +1300 Subject: [PATCH] Fix calls to syncUser/registerUser --- d2m/actions/register-user.js | 12 ++++++++---- d2m/actions/send-message.js | 12 +++++++----- d2m/event-dispatcher.js | 2 +- discord/utils.js | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/d2m/actions/register-user.js b/d2m/actions/register-user.js index 7e1d9f9..68b1a88 100644 --- a/d2m/actions/register-user.js +++ b/d2m/actions/register-user.js @@ -1,6 +1,6 @@ // @ts-check -const assert = require("assert") +const assert = require("assert").strict const reg = require("../../matrix/read-registration") const DiscordTypes = require("discord-api-types/v10") const mixin = require("mixin-deep") @@ -180,11 +180,12 @@ function _hashProfileContent(content, powerLevel) { * 5. If the state content or power level have changed, send them to Matrix and update them in the database for next time * @param {DiscordTypes.APIUser} user * @param {Omit} member - * @param {DiscordTypes.APIGuild} guild * @param {DiscordTypes.APIGuildChannel} channel + * @param {DiscordTypes.APIGuild} guild + * @param {string} roomID * @returns {Promise} mxid of the updated sim */ -async function syncUser(user, member, guild, channel, roomID) { +async function syncUser(user, member, channel, guild, roomID) { const mxid = await ensureSimJoined(user, roomID) const content = await memberToStateContent(user, member, guild.id) const powerLevel = memberToPowerLevel(user, member, guild, channel) @@ -204,6 +205,9 @@ async function syncUser(user, member, guild, channel, roomID) { return mxid } +/** + * @param {string} roomID + */ async function syncAllUsersInRoom(roomID) { const mxids = select("sim_member", "mxid", {room_id: roomID}).pluck().all() @@ -228,7 +232,7 @@ async function syncAllUsersInRoom(roomID) { assert.ok(user) console.log(`[user sync] to matrix: ${user.username} in ${channel.name}`) - await syncUser(user, member, guild, channel, roomID) + await syncUser(user, member, channel, guild, roomID) } } diff --git a/d2m/actions/send-message.js b/d2m/actions/send-message.js index 8c26f07..a1290e0 100644 --- a/d2m/actions/send-message.js +++ b/d2m/actions/send-message.js @@ -1,6 +1,7 @@ // @ts-check -const assert = require("assert") +const assert = require("assert").strict +const DiscordTypes = require("discord-api-types/v10") const passthrough = require("../../passthrough") const { discord, sync, db } = passthrough @@ -18,17 +19,18 @@ const createRoom = sync.require("../actions/create-room") const dUtils = sync.require("../../discord/utils") /** - * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message - * @param {import("discord-api-types/v10").APIGuild} guild + * @param {DiscordTypes.GatewayMessageCreateDispatchData} message + * @param {DiscordTypes.APIGuildChannel} channel + * @param {DiscordTypes.APIGuild} guild * @param {{speedbump_id: string, speedbump_webhook_id: string} | null} row data about the webhook which is proxying messages in this channel */ -async function sendMessage(message, guild, row) { +async function sendMessage(message, channel, guild, row) { const roomID = await createRoom.ensureRoom(message.channel_id) let senderMxid = null if (!dUtils.isWebhookMessage(message)) { if (message.member) { // available on a gateway message create event - senderMxid = await registerUser.syncUser(message.author, message.member, message.guild_id, roomID) + senderMxid = await registerUser.syncUser(message.author, message.member, channel, guild, roomID) } else { // well, good enough... senderMxid = await registerUser.ensureSimJoined(message.author, roomID) } diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 3db1d45..2c4dcd5 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -248,7 +248,7 @@ module.exports = { if (affected) return // @ts-ignore - await sendMessage.sendMessage(message, guild, row), + await sendMessage.sendMessage(message, channel, guild, row), await discordCommandHandler.execute(message, channel, guild) }, diff --git a/discord/utils.js b/discord/utils.js index ff1f215..6b6b602 100644 --- a/discord/utils.js +++ b/discord/utils.js @@ -63,7 +63,7 @@ function getPermissions(userRoles, guildRoles, userID, channelOverwrites) { */ function hasPermission(resolvedPermissions, permissionToCheckFor) { // Make sure permissionToCheckFor has exactly one permission in it - assert.equal(permissionToCheckFor.toString(2).match(/1/g), 1) + assert.equal(permissionToCheckFor.toString(2).match(/1/g)?.length, 1) // Do the actual calculation return (resolvedPermissions & permissionToCheckFor) === permissionToCheckFor }