Add Repeat Roll button

This commit is contained in:
Ean Milligan 2025-07-17 04:56:43 -04:00
parent 50d9c78b45
commit a4ddce0bc8
3 changed files with 68 additions and 17 deletions

View File

@ -1,4 +1,4 @@
import { botId, DiscordenoMessage, Embed, FileContent, sendDirectMessage, sendMessage } from '@discordeno';
import { botId, ButtonStyles, DiscordenoMessage, Embed, FileContent, MessageComponentTypes, sendDirectMessage, sendMessage } from '@discordeno';
import { log, LogTypes as LT } from '@Log4Deno';
import config from '~config';
@ -13,15 +13,18 @@ import { ApiResolveMap, TestResolveMap } from 'artigen/managers/resolveManager.t
import { generateCountDetailsEmbed, generateDMFailed, generateRollDistsEmbed, generateRollEmbed, toggleWebView } from 'artigen/utils/embeds.ts';
import { loggingEnabled } from 'artigen/utils/logFlag.ts';
import { basicReducer } from 'artigen/utils/reducers.ts';
import dbClient from 'db/client.ts';
import { queries } from 'db/common.ts';
import { infoColor1 } from 'embeds/colors.ts';
import stdResp from 'endpoints/stdResponses.ts';
import { InteractionValueSeparator } from 'events/interactionCreate.ts';
import utils from 'utils/utils.ts';
import { infoColor1 } from 'embeds/colors.ts';
import { basicReducer } from 'artigen/utils/reducers.ts';
const getUserIdForEmbed = (rollRequest: QueuedRoll): bigint => {
if (rollRequest.apiRoll) return rollRequest.api.userId;
@ -29,6 +32,8 @@ const getUserIdForEmbed = (rollRequest: QueuedRoll): bigint => {
return 0n;
};
export const repeatRollCustomId = 'repeatRoll';
export const onWorkerComplete = async (workerMessage: MessageEvent<SolvedRoll>, workerTimeout: number, rollRequest: QueuedRoll) => {
const apiResolve = rollRequest.apiRoll ? ApiResolveMap.get(rollRequest.resolve as string) : undefined;
const testResolve = rollRequest.testRoll ? TestResolveMap.get(rollRequest.resolve as string) : undefined;
@ -183,6 +188,20 @@ export const onWorkerComplete = async (workerMessage: MessageEvent<SolvedRoll>,
} else {
newMsg = await rollRequest.dd.myResponse.edit({
embeds: pubEmbeds,
components: [
{
type: MessageComponentTypes.ActionRow,
components: [
{
type: MessageComponentTypes.Button,
label: 'Repeat Roll',
customId: `${repeatRollCustomId}${InteractionValueSeparator}${getUserIdForEmbed(rollRequest).toString()}`,
style: ButtonStyles.Secondary,
emoji: '🎲',
},
],
},
],
});
}
@ -240,9 +259,9 @@ Please click on "<@${botId}> *Click to see attachment*" above this message to se
}
: {
details: pubEmbedDetails,
},
),
),
}
)
)
);
}
} catch (e) {
@ -253,13 +272,12 @@ Please click on "<@${botId}> *Click to see attachment*" above this message to se
(
await generateRollEmbed(
rollRequest.dd.originalMessage.authorId,
<SolvedRoll> {
<SolvedRoll>{
error: true,
errorMsg:
`Something weird went wrong, likely the requested roll is too complex and caused the response to be too large for Discord. Try breaking the request down into smaller messages and try again.\n\nIf this error continues to come up, please \`${config.prefix}report\` this to my developer.`,
errorMsg: `Something weird went wrong, likely the requested roll is too complex and caused the response to be too large for Discord. Try breaking the request down into smaller messages and try again.\n\nIf this error continues to come up, please \`${config.prefix}report\` this to my developer.`,
errorCode: 'UnhandledWorkerComplete',
},
<RollModifiers> {},
<RollModifiers>{}
)
).embed,
],

View File

@ -3,6 +3,7 @@ import {
DiscordenoMessage,
DiscordMessageComponentTypes,
editMessage,
getMessage,
Interaction,
InteractionResponseTypes,
MessageFlags,
@ -12,12 +13,16 @@ import {
} from '@discordeno';
import { log, LogTypes as LT } from '@Log4Deno';
import { repeatRollCustomId } from 'artigen/managers/handler/workerComplete.ts';
import { toggleWebView, webViewCustomId } from 'artigen/utils/embeds.ts';
import { generateHelpMessage, helpCustomId } from 'commands/helpLibrary/generateHelpMessage.ts';
import { failColor } from 'embeds/colors.ts';
import { messageCreateHandler } from 'events/messageCreate.ts';
import utils from 'utils/utils.ts';
export const InteractionValueSeparator = '\u205a';
@ -43,7 +48,7 @@ export const interactionCreateHandler = async (interaction: Interaction) => {
return;
}
if (parsedData.customId.startsWith(webViewCustomId) && parsedData.componentType === DiscordMessageComponentTypes.Button && interaction.message) {
if (parsedData.customId.startsWith(webViewCustomId) && interaction.message) {
const ownerId = parsedData.customId.split(InteractionValueSeparator)[1] ?? 'missingOwnerId';
const userInteractingId = interaction.member?.user.id ?? interaction.user?.id ?? 'missingUserId';
if (ownerId === userInteractingId) {
@ -70,6 +75,35 @@ export const interactionCreateHandler = async (interaction: Interaction) => {
return;
}
if (parsedData.customId.startsWith(repeatRollCustomId) && interaction.message) {
const ownerId = parsedData.customId.split(InteractionValueSeparator)[1] ?? 'missingOwnerId';
const userInteractingId = interaction.member?.user.id ?? interaction.user?.id ?? 'missingUserId';
if (ownerId === userInteractingId) {
ackInteraction(interaction);
const botMsg: DiscordenoMessage = await structures.createDiscordenoMessage(interaction.message);
const rollMsg: DiscordenoMessage = await getMessage(
BigInt(botMsg.messageReference?.channelId ?? '0'),
BigInt(botMsg.messageReference?.messageId ?? '0'),
);
messageCreateHandler(rollMsg);
} else {
sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource,
data: {
flags: MessageFlags.Empheral,
embeds: [
{
color: failColor,
title: 'Not Allowed!',
description: 'Only the original user that requested this roll can repeat it.',
},
],
},
}).catch((e) => utils.commonLoggers.messageSendError('interactionCreate.ts:96', interaction, e));
}
return;
}
log(LT.WARN, `UNHANDLED INTERACTION!!! data: ${JSON.stringify(interaction.data)} | Full Interaction: ${JSON.stringify(interaction)}`);
} else {
log(LT.WARN, `UNHANDLED INTERACTION!!! Missing data! ${JSON.stringify(interaction)}`);

View File

@ -23,8 +23,7 @@ export const messageCreateHandler = (message: DiscordenoMessage) => {
// Handle inline guilds if allowed
if (inlineList.includes(message.guildId) && message.content.includes(config.prefix) && message.content.includes(config.prefix)) {
const argSpaces = message.content.trim().split(/([ \n]+)/g);
commands.roll(message, argSpaces, '');
commands.roll(message, message.content.trim().split(/([ \n]+)/g), '');
}
// return as we are done handling this message
return;