Fix bridging interaction responses and edits
This commit is contained in:
		
							parent
							
								
									e2dffe457e
								
							
						
					
					
						commit
						7790d98c66
					
				
					 5 changed files with 201 additions and 3 deletions
				
			
		| 
						 | 
					@ -12,6 +12,8 @@ const api = sync.require("../../matrix/api")
 | 
				
			||||||
const registerUser = sync.require("./register-user")
 | 
					const registerUser = sync.require("./register-user")
 | 
				
			||||||
/** @type {import("../actions/create-room")} */
 | 
					/** @type {import("../actions/create-room")} */
 | 
				
			||||||
const createRoom = sync.require("../actions/create-room")
 | 
					const createRoom = sync.require("../actions/create-room")
 | 
				
			||||||
 | 
					/** @type {import("../../discord/utils")} */
 | 
				
			||||||
 | 
					const dUtils = sync.require("../../discord/utils")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
 | 
					 * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
 | 
				
			||||||
| 
						 | 
					@ -21,7 +23,7 @@ async function sendMessage(message, guild) {
 | 
				
			||||||
	const roomID = await createRoom.ensureRoom(message.channel_id)
 | 
						const roomID = await createRoom.ensureRoom(message.channel_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let senderMxid = null
 | 
						let senderMxid = null
 | 
				
			||||||
	if (!message.webhook_id) {
 | 
						if (!dUtils.isWebhookMessage(message)) {
 | 
				
			||||||
		if (message.member) { // available on a gateway message create event
 | 
							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, message.guild_id, roomID)
 | 
				
			||||||
		} else { // well, good enough...
 | 
							} else { // well, good enough...
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,7 @@ async function editToChanges(message, guild, api) {
 | 
				
			||||||
	const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get()
 | 
						const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get()
 | 
				
			||||||
	assert(roomID)
 | 
						assert(roomID)
 | 
				
			||||||
	/** @type {string?} Null if we don't have a sender in the room, which will happen if it's a webhook's message. The bridge bot will do the edit instead. */
 | 
						/** @type {string?} Null if we don't have a sender in the room, which will happen if it's a webhook's message. The bridge bot will do the edit instead. */
 | 
				
			||||||
	const senderMxid = from("sim").join("sim_member", "mxid").where({user_id: message.author.id}).pluck("mxid").get() || null
 | 
						const senderMxid = from("sim").join("sim_member", "mxid").where({user_id: message.author.id, room_id: roomID}).pluck("mxid").get() || null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const oldEventRows = select("event_message", ["event_id", "event_type", "event_subtype", "part"], {message_id: message.id}).all()
 | 
						const oldEventRows = select("event_message", ["event_id", "event_type", "event_subtype", "part"], {message_id: message.id}).all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -141,7 +141,6 @@ const utils = {
 | 
				
			||||||
					await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true)
 | 
										await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				} else if (message.t === "MESSAGE_CREATE") {
 | 
									} else if (message.t === "MESSAGE_CREATE") {
 | 
				
			||||||
					console.log(message.d)
 | 
					 | 
				
			||||||
					await eventDispatcher.onMessageCreate(client, message.d)
 | 
										await eventDispatcher.onMessageCreate(client, message.d)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				} else if (message.t === "MESSAGE_UPDATE") {
 | 
									} else if (message.t === "MESSAGE_UPDATE") {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,4 +47,14 @@ function getPermissions(userRoles, guildRoles, userID, channelOverwrites) {
 | 
				
			||||||
	return allowed
 | 
						return allowed
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Command interaction responses have a webhook_id for some reason, but still have real author info of a real bot user in the server.
 | 
				
			||||||
 | 
					 * @param {DiscordTypes.APIMessage} message
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function isWebhookMessage(message) {
 | 
				
			||||||
 | 
						const isInteractionResponse = message.type === 20
 | 
				
			||||||
 | 
						return message.webhook_id && !isInteractionResponse
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports.getPermissions = getPermissions
 | 
					module.exports.getPermissions = getPermissions
 | 
				
			||||||
 | 
					module.exports.isWebhookMessage = isWebhookMessage
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										187
									
								
								test/data.js
									
										
									
									
									
								
							
							
						
						
									
										187
									
								
								test/data.js
									
										
									
									
									
								
							| 
						 | 
					@ -2007,6 +2007,193 @@ module.exports = {
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			attachments: [],
 | 
								attachments: [],
 | 
				
			||||||
			guild_id: "1100319549670301727"
 | 
								guild_id: "1100319549670301727"
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							bridge_echo_webhook: {
 | 
				
			||||||
 | 
								webhook_id: "1160692755144654970",
 | 
				
			||||||
 | 
								type: 0,
 | 
				
			||||||
 | 
								tts: false,
 | 
				
			||||||
 | 
								timestamp: "2023-10-09T21:15:58.866000+00:00",
 | 
				
			||||||
 | 
								pinned: false,
 | 
				
			||||||
 | 
								mentions: [],
 | 
				
			||||||
 | 
								mention_roles: [],
 | 
				
			||||||
 | 
								mention_everyone: false,
 | 
				
			||||||
 | 
								id: "1161049444674973706",
 | 
				
			||||||
 | 
								flags: 0,
 | 
				
			||||||
 | 
								embeds: [],
 | 
				
			||||||
 | 
								edited_timestamp: null,
 | 
				
			||||||
 | 
								content: "ready when you are",
 | 
				
			||||||
 | 
								components: [],
 | 
				
			||||||
 | 
								channel_id: "497161350934560778",
 | 
				
			||||||
 | 
								author: {
 | 
				
			||||||
 | 
									username: "cadence [they]",
 | 
				
			||||||
 | 
									id: "1160692755144654970",
 | 
				
			||||||
 | 
									discriminator: "0000",
 | 
				
			||||||
 | 
									bot: true,
 | 
				
			||||||
 | 
									avatar: "af0ead3b92cf6e448fdad80b4e7fc9e5"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								attachments: [],
 | 
				
			||||||
 | 
								application_id: "684280192553844747",
 | 
				
			||||||
 | 
								guild_id: "497159726455455754"
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							crosspost_announcement: {
 | 
				
			||||||
 | 
								id: "1152745817678028840",
 | 
				
			||||||
 | 
								type: 0,
 | 
				
			||||||
 | 
								content: "All text based commands are now inactive on Chewey Bot\nTo continue using commands you'll need to use them as slash commands",
 | 
				
			||||||
 | 
								channel_id: "500454381414514688",
 | 
				
			||||||
 | 
								author: {
 | 
				
			||||||
 | 
									id: "748007224353226832",
 | 
				
			||||||
 | 
									username: "Chewey Bot Official Server #announcements",
 | 
				
			||||||
 | 
									avatar: "427b2893c574b90f1c6bb54da2c609cb",
 | 
				
			||||||
 | 
									discriminator: "0000",
 | 
				
			||||||
 | 
									public_flags: 0,
 | 
				
			||||||
 | 
									flags: 0,
 | 
				
			||||||
 | 
									bot: true
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								attachments: [],
 | 
				
			||||||
 | 
								embeds: [],
 | 
				
			||||||
 | 
								mentions: [],
 | 
				
			||||||
 | 
								mention_roles: [],
 | 
				
			||||||
 | 
								pinned: false,
 | 
				
			||||||
 | 
								mention_everyone: false,
 | 
				
			||||||
 | 
								tts: false,
 | 
				
			||||||
 | 
								timestamp: "2023-09-16T23:20:19.916000+00:00",
 | 
				
			||||||
 | 
								edited_timestamp: null,
 | 
				
			||||||
 | 
								flags: 2,
 | 
				
			||||||
 | 
								components: [],
 | 
				
			||||||
 | 
								webhook_id: "748007224353226832",
 | 
				
			||||||
 | 
								message_reference: {
 | 
				
			||||||
 | 
									channel_id: "372274661439832065",
 | 
				
			||||||
 | 
									message_id: "1152745799596384263",
 | 
				
			||||||
 | 
									guild_id: "372271956562542592"
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						interaction_message: {
 | 
				
			||||||
 | 
							thinking_interaction_without_bot_user: {
 | 
				
			||||||
 | 
								webhook_id: "1109360903096369153",
 | 
				
			||||||
 | 
								type: 20,
 | 
				
			||||||
 | 
								tts: false,
 | 
				
			||||||
 | 
								timestamp: "2023-10-09T21:16:11.673000+00:00",
 | 
				
			||||||
 | 
								pinned: false,
 | 
				
			||||||
 | 
								nonce: "1161049469261709312",
 | 
				
			||||||
 | 
								mentions: [],
 | 
				
			||||||
 | 
								mention_roles: [],
 | 
				
			||||||
 | 
								mention_everyone: false,
 | 
				
			||||||
 | 
								interaction: {
 | 
				
			||||||
 | 
									user: {
 | 
				
			||||||
 | 
										username: "papiophidian",
 | 
				
			||||||
 | 
										public_flags: 4194880,
 | 
				
			||||||
 | 
										id: "320067006521147393",
 | 
				
			||||||
 | 
										global_name: "PapiOphidian",
 | 
				
			||||||
 | 
										discriminator: "0",
 | 
				
			||||||
 | 
										avatar_decoration_data: null,
 | 
				
			||||||
 | 
										avatar: "5fc4ad85c1ea876709e9a7d3374a78a1"
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									type: 2,
 | 
				
			||||||
 | 
									name: "stats",
 | 
				
			||||||
 | 
									member: {
 | 
				
			||||||
 | 
										roles: [],
 | 
				
			||||||
 | 
										premium_since: null,
 | 
				
			||||||
 | 
										pending: false,
 | 
				
			||||||
 | 
										nick: "Brad",
 | 
				
			||||||
 | 
										mute: false,
 | 
				
			||||||
 | 
										joined_at: "2018-10-03T21:35:50.974000+00:00",
 | 
				
			||||||
 | 
										flags: 0,
 | 
				
			||||||
 | 
										deaf: false,
 | 
				
			||||||
 | 
										communication_disabled_until: null,
 | 
				
			||||||
 | 
										avatar: null
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									id: "1161049497724534825"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								id: "1161049498391425196",
 | 
				
			||||||
 | 
								flags: 128,
 | 
				
			||||||
 | 
								embeds: [],
 | 
				
			||||||
 | 
								edited_timestamp: null,
 | 
				
			||||||
 | 
								content: "",
 | 
				
			||||||
 | 
								components: [],
 | 
				
			||||||
 | 
								channel_id: "497161350934560778",
 | 
				
			||||||
 | 
								author: {
 | 
				
			||||||
 | 
									username: "Amanda 🎵",
 | 
				
			||||||
 | 
									public_flags: 524288,
 | 
				
			||||||
 | 
									id: "1109360903096369153",
 | 
				
			||||||
 | 
									global_name: null,
 | 
				
			||||||
 | 
									discriminator: "2192",
 | 
				
			||||||
 | 
									bot: true,
 | 
				
			||||||
 | 
									avatar_decoration_data: null,
 | 
				
			||||||
 | 
									avatar: "e4a45abe5f8ee44f0b59b79a08bdb2ac"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								attachments: [],
 | 
				
			||||||
 | 
								application_id: "1109360903096369153",
 | 
				
			||||||
 | 
								guild_id: "497159726455455754"
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							thinking_interaction: {
 | 
				
			||||||
 | 
								webhook_id: "1109360903096369153",
 | 
				
			||||||
 | 
								type: 20,
 | 
				
			||||||
 | 
								tts: false,
 | 
				
			||||||
 | 
								timestamp: "2023-10-09T21:18:45.002000+00:00",
 | 
				
			||||||
 | 
								pinned: false,
 | 
				
			||||||
 | 
								nonce: "1161050112089128960",
 | 
				
			||||||
 | 
								mentions: [],
 | 
				
			||||||
 | 
								mention_roles: [],
 | 
				
			||||||
 | 
								mention_everyone: false,
 | 
				
			||||||
 | 
								member: {
 | 
				
			||||||
 | 
									roles: [ "604073998749270046" ],
 | 
				
			||||||
 | 
									premium_since: null,
 | 
				
			||||||
 | 
									pending: false,
 | 
				
			||||||
 | 
									nick: null,
 | 
				
			||||||
 | 
									mute: false,
 | 
				
			||||||
 | 
									joined_at: "2023-10-09T21:18:30.600000+00:00",
 | 
				
			||||||
 | 
									flags: 1,
 | 
				
			||||||
 | 
									deaf: false,
 | 
				
			||||||
 | 
									communication_disabled_until: null,
 | 
				
			||||||
 | 
									avatar: null
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								interaction: {
 | 
				
			||||||
 | 
									user: {
 | 
				
			||||||
 | 
										username: "papiophidian",
 | 
				
			||||||
 | 
										public_flags: 4194880,
 | 
				
			||||||
 | 
										id: "320067006521147393",
 | 
				
			||||||
 | 
										global_name: "PapiOphidian",
 | 
				
			||||||
 | 
										discriminator: "0",
 | 
				
			||||||
 | 
										avatar_decoration_data: null,
 | 
				
			||||||
 | 
										avatar: "5fc4ad85c1ea876709e9a7d3374a78a1"
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									type: 2,
 | 
				
			||||||
 | 
									name: "stats",
 | 
				
			||||||
 | 
									member: {
 | 
				
			||||||
 | 
										roles: [],
 | 
				
			||||||
 | 
										premium_since: null,
 | 
				
			||||||
 | 
										pending: false,
 | 
				
			||||||
 | 
										nick: "Brad",
 | 
				
			||||||
 | 
										mute: false,
 | 
				
			||||||
 | 
										joined_at: "2018-10-03T21:35:50.974000+00:00",
 | 
				
			||||||
 | 
										flags: 0,
 | 
				
			||||||
 | 
										deaf: false,
 | 
				
			||||||
 | 
										communication_disabled_until: null,
 | 
				
			||||||
 | 
										avatar: null
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									id: "1161050140640018472"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								id: "1161050141499863120",
 | 
				
			||||||
 | 
								flags: 128,
 | 
				
			||||||
 | 
								embeds: [],
 | 
				
			||||||
 | 
								edited_timestamp: null,
 | 
				
			||||||
 | 
								content: "",
 | 
				
			||||||
 | 
								components: [],
 | 
				
			||||||
 | 
								channel_id: "497161350934560778",
 | 
				
			||||||
 | 
								author: {
 | 
				
			||||||
 | 
									username: "Amanda 🎵",
 | 
				
			||||||
 | 
									public_flags: 524288,
 | 
				
			||||||
 | 
									id: "1109360903096369153",
 | 
				
			||||||
 | 
									global_name: null,
 | 
				
			||||||
 | 
									discriminator: "2192",
 | 
				
			||||||
 | 
									bot: true,
 | 
				
			||||||
 | 
									avatar_decoration_data: null,
 | 
				
			||||||
 | 
									avatar: "e4a45abe5f8ee44f0b59b79a08bdb2ac"
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								attachments: [],
 | 
				
			||||||
 | 
								application_id: "1109360903096369153",
 | 
				
			||||||
 | 
								guild_id: "497159726455455754"
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue