(interaction, commandFunction, errorEmbed, options = {})
| 267 | } |
| 268 | |
| 269 | static async safeExecute(interaction, commandFunction, errorEmbed, options = {}) { |
| 270 | const autoDeferDefault = !interaction._isPrefixCommand; |
| 271 | const { autoDefer = autoDeferDefault, deferOptions = { flags: MessageFlags.Ephemeral } } = options; |
| 272 | |
| 273 | if (!this.isInteractionValid(interaction)) { |
| 274 | logger.warn(`Interaction ${interaction.id} has expired, ignoring`); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | const coordinator = this.getCoordinator(interaction); |
| 279 | if (coordinator?.isUsageFinalized()) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (autoDefer && !interaction.replied && !interaction.deferred) { |
| 284 | const deferStartTime = Date.now(); |
| 285 | const deferSuccess = await this.safeDefer(interaction, deferOptions); |
| 286 | |
| 287 | if (Date.now() - deferStartTime > 3000) { |
| 288 | logger.warn(`Interaction ${interaction.id} defer took too long (${Date.now() - deferStartTime}ms), command may expire`); |
| 289 | } |
| 290 | |
| 291 | if (!deferSuccess) { |
| 292 | logger.warn(`Interaction ${interaction.id} defer failed, skipping command execution`); |
| 293 | return; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | try { |
| 298 | await commandFunction(); |
| 299 | } catch (error) { |
| 300 | logger.error('Error executing command:', error); |
| 301 | |
| 302 | if (coordinator?.isUsageFinalized()) { |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | const errorToHandle = typeof errorEmbed === 'string' |
| 307 | ? createError(error.message || 'Command failed', ErrorTypes.UNKNOWN, errorEmbed, { expected: true }) |
| 308 | : error; |
| 309 | |
| 310 | await handleInteractionError(interaction, errorToHandle, { source: 'interactionHelper.safeExecute' }); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static async universalReply(interaction, options) { |
| 315 | const coordinator = this.getCoordinator(interaction); |
no test coverage detected