Skip to content

Commit

Permalink
Merge pull request #145 from d0ctr/test
Browse files Browse the repository at this point in the history
Add /genius and menus
  • Loading branch information
d0ctr authored Dec 30, 2023
2 parents 71c9fde + 1690fba commit 2d017d9
Show file tree
Hide file tree
Showing 15 changed files with 907 additions and 173 deletions.
12 changes: 6 additions & 6 deletions .env.vault
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ DOTENV_VAULT_CI="ak47RmJUZdJCnDXqzbXPNg9VAc7WotMtW1NF6ZaWgeW9IDO3IaTO0oXEsKjASp2
DOTENV_VAULT_CI_VERSION=19

# production
DOTENV_VAULT_PRODUCTION="vtZ0315NaQbVOsnS8rJY4xqZF7dD7bNrUZ/iehvv2pTn2MRffSeHzBJZ/R+Uo+NIbL4AgTzxTBrHcNvsx/XgSHpOVOzyG7d8WspGwDpa5nFZyuzeDFv3PsgdP7Omj5v9OL7cT4E3uwnh9iTi80YcW64qiagfC5HURdfpAE2PzV0Q+0TpZMmeX6U08VqhqKf5bUydVLkVOiTM0pxon4YSrjT+9qhnScUlzIe8DbfdhsHmJTuRBASkGtVE1v9bLk3zxcWCU0iZhAuyBPWnDjYWq7i8MYoNkGWahYdgrUjtrFjR99prz4vxg9uijDokOzO/I0Z0NtsZ2OtNM+RfHvcRXkzv1/MoZD+xW5PkX9YC7OYWkJQv9VkGaXqpSWxqIt/pW+xNeuliSbnX7OjY+laSdDWI1H6O3BhzTZSn1eYseVyEqZlcOtdJ5Ac5jb4vDKi1Faht8ws4buVkFIhbo9pSkEP26vcnfBabKYxxNr1m+IEOuY8FRVGtQTnVA0Vl0gQNHLa2oIV5aeJ18pNqdSOTCTrUnRyj363gUjDape42f4WhHobfq5+bxiTMfZlzjzkF52M7qctF+qD513JMdbmi8wUkSiHZqmfEXXTUs/2m1m95O4lT/8NSPyUdOvpRRlkYBBxtF3hosYPAcMNKwhp1EyZWLMStivtA3ZeTN7QPFJTmolWiLNmArHAa+p4jblG/hlaeb/R3rh9vx3CfkZjl5jkUS6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sIKyEkGDI5yqb18nHiLlH6/zsO/wf2FzNPQp591qVNer8ndKyt7zMNYIewwT/Fph1tlae01xWCwg7CNBFJJwHOMUV9dY5RtGPgj6ZgsZxBBECSEd3T4SavQkXRVhLr+m2SBT9Sf6cxglXzGZmyiSrK6UAEOb3ZRPLvujnXx958yzK9LDKH7jR4MrRAWcgy/3R5Yc="
DOTENV_VAULT_DEVELOPMENT_VERSION=43

#/----------------settings/metadata-----------------/
DOTENV_VAULT="vlt_7b9007f3078ad4bddb4f05ddba592d88ca09adf1d1fda7b5e5c1231595dbcb76"
Expand Down
185 changes: 160 additions & 25 deletions commands/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,49 @@ const { EmbedBuilder, ButtonBuilder, ActionRowBuilder, ButtonStyle, MessagePaylo

const turndownService = new TurndownService({
codeBlockStyle: 'fenced',
br: ' '
});

turndownService.addRule('a', {
filter: ['a'],
filter: 'a',
replacement: (content, node) => {
const href = node.getAttribute('href');
return `[${content}](<${href}>)`;
}
});

/** @typedef {import('discord.js').Interaction} Interaction */

/**
* @typedef {{
* platform: 'discord',
* command_name: string?,
* text: string,
* args: any[]?,
* from: {
* id: string?,
* username: string?,
* name: string?
* },
* space: {
* type: 'guild',
* id: string,
* title: string
* } | {
* id: string?,
* username: string?
* },
* id: string,
* data: string?
* }} DiscordInteraction
*/

/**
*
* @param {Interaction} interaction
* @param {*} definition
* @returns {DiscordInteraction}
*/
function commonizeInteraction(interaction, definition) {
let common_interaction = {
platform: 'discord',
Expand Down Expand Up @@ -48,10 +81,48 @@ function commonizeInteraction(interaction, definition) {
}

common_interaction.id = interaction.id;
common_interaction.data = interaction.customId;

return common_interaction;
}

function transformOverrides(response) {
if (response?.overrides?.followup) {
const { text, url } = response.overrides.followup;
const components = [
new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setLabel(text)
.setURL(url)
.setStyle(ButtonStyle.Link)
)
];
response.components = components;
}

if (response.overrides?.buttons) {
if (!response.components) response.components = [];

response.components.push(...response.overrides.buttons.map(row => {
const actionRow = new ActionRowBuilder();
actionRow.addComponents(...row.map(button => {
return new ButtonBuilder()
.setLabel(button.name)
.setCustomId(button.callback)
.setStyle(ButtonStyle.Primary);
}));
return actionRow;
}));
}

if (response?.overrides?.embeded_image) {
response.embeds = [new EmbedBuilder().setImage(response?.overrides?.embeded_image)];
}

return response;
}

function replyWithText(interaction, response, logger) {
logger.info(`Replying with text`, { response });

Expand All @@ -63,7 +134,7 @@ function replyWithText(interaction, response, logger) {
// response.embeds = [embed];
// }

interaction.editReply({ content: response.text, components: response?.components })
interaction.editReply({ content: response.text, components: response?.components, embeds: response?.embeds })
.then((messsage) => {
logger.debug('Replied!', { message_id: messsage.id });
}).catch(err => {
Expand All @@ -83,7 +154,7 @@ function replyWithText(interaction, response, logger) {
}

function replyWithEmbed(interaction, response, logger) {
const payload = {};
const payload = { embeds: [] };

const embed = new EmbedBuilder();

Expand All @@ -102,7 +173,10 @@ function replyWithEmbed(interaction, response, logger) {
else {
logger.info(`Replying with media of type: ${response.type}`);
embed.setImage(response.media);
payload.embeds = [embed];
payload.embeds.push(embed);
}
if (response?.embeds) {
payload.embeds.unshift(...response.embeds);
}

interaction.editReply(payload)
Expand All @@ -122,7 +196,7 @@ function replyWithEmbed(interaction, response, logger) {
}

function replyWithFile(interaction, response, logger) {
const payload = {};
const payload = { embeds: [] };

if (response.text) {
payload.content = response.text;
Expand All @@ -136,6 +210,9 @@ function replyWithFile(interaction, response, logger) {
logger.info(`Replying with file of type: ${response.type}`);
payload.files = [{ name: response.filename, attachment: response.media }];
}
if (response?.embeds) {
payload.embeds.unshift(...response.embeds);
}

interaction.editReply(payload)
.then((messsage) => {
Expand All @@ -154,21 +231,6 @@ function replyWithFile(interaction, response, logger) {
}

function reply(interaction, response, logger) {

if (response?.overrides?.followup) {
const { text, url } = response.overrides.followup;
const components = [
new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setLabel(text)
.setURL(url)
.setStyle(ButtonStyle.Link)
)
];
response.components = components;
}

while (response?.text?.length >= 2000) response.text = response.text.split('\n').slice(0, -1).join('\n');

if (['text', 'error'].includes(response.type)) {
Expand Down Expand Up @@ -211,24 +273,27 @@ function reply(interaction, response, logger) {
);
}

function handleCommand(interaction, handler ,definition) {
function handleCommand(interaction, handler, definition) {
const common_interaction = commonizeInteraction(interaction, definition);
const log_meta = {
module: 'discord-common-interface-handler',
module: 'discord-common-command-handler',
command_name: common_interaction.command_name,
platform: common_interaction.platform,
interaction: common_interaction
};
const logger = require('../logger').child(log_meta);
common_interaction.logger = logger.child({ ...log_meta, module: `common-handler-${common_interaction.command_name}` });
common_interaction.logger = logger.child({ ...log_meta, module: `common-command-${common_interaction.command_name}` });

logger.info(`Received command: ${common_interaction.text}`);

interaction.deferReply()
.then(() => handler(common_interaction))
.then(transformOverrides)
.then(response => {
if (response.text) {
response.text = turndownService.turndown(response.text.replace(/(\n|\\n)/gm, '<br/>'));
response.text = response.text.replace(/\n|\\n/gm, '<br/>');
response.text = turndownService.turndown(response.text);
response.text = response.text.replace(/( *\n *){2,}/gm, '\n\n')
}
reply(interaction, response, logger);
}).catch(err => {
Expand All @@ -244,7 +309,77 @@ function handleCommand(interaction, handler ,definition) {
});
};

/**
*
* @param {import('discord.js').Interaction} interaction
* @param {*} response
*/
async function answerCallback(interaction, response) {
if (response.type === 'error') {
return interaction.followUp(response.text);
}

switch(response.type) {
case 'edit_text':
case 'edit_caption':
return interaction.editReply({
content: response.text,
components: response.components,
embeds: response.embeds
});
case 'edit_media':
if (response.filename) {
response.files = [{ name: response.filename, attachment: response.media }];
}
else {
if (!response.embdes) response.embeds = [];
response.embeds.push(new EmbedBuilder().setImage(response.media));
}
return interaction.editReply({
embeds: response.embeds,
components: response.components,
files: response.files,
})
case 'delete_buttons':
return interaction.followUp(response.text)
.then(() => interaction.editReply({
components: []
}));
case 'edit_buttons':
return interaction.editReply({
components: response.components
});
}
}

async function handleCallback(interaction, handle) {
const common_interaction = commonizeInteraction(interaction);
const log_meta = {
module: 'discord-common-interface-handler',
command_name: common_interaction.command_name,
platform: common_interaction.platform,
interaction: common_interaction
};
const logger = require('../logger').child(log_meta);
common_interaction.logger = logger.child({ ...log_meta, module: `common-handler-${common_interaction.command_name}` });

logger.info(`Received callback: ${common_interaction.data}`);

interaction.deferUpdate()
.then(() => handle(common_interaction))
.then(transformOverrides)
.then(response => {
if (response.text) {
response.text = response.text.replace(/\n|\\n/gm, '<br/>');
response.text = turndownService.turndown(response.text);
response.text = response.text.replace(/( *\n *){2,}/gm, '\n\n')
}
return answerCallback(interaction, response);
})
}

module.exports = {
commonizeInteraction,
handleCommand
handleCommand,
handleCallback,
};
5 changes: 4 additions & 1 deletion commands/handlers-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const commands = [];
const definitions = [];
const handlers = [];
const conditions = [];
const callbacks = [];

fs.readdirSync(path.join(__dirname, 'handlers')).forEach(handler_file => {
if (!handler_file.endsWith('.js')) {
Expand All @@ -16,12 +17,14 @@ fs.readdirSync(path.join(__dirname, 'handlers')).forEach(handler_file => {
commands.push(temp_import.definition.command_name);
handlers.push(temp_import.handler);
conditions.push(temp_import.condition);
callbacks.push(temp_import.callback)
});

module.exports = {
commands,
definitions,
handlers,
conditions
conditions,
callbacks
};

Loading

0 comments on commit 2d017d9

Please sign in to comment.