From a83dde15c3b2f4398f9b09bbac61e9e66399631b Mon Sep 17 00:00:00 2001 From: jmhayes3 <22490346+jmhayes3@users.noreply.github.com> Date: Fri, 23 Aug 2024 21:02:46 -0500 Subject: [PATCH] use embeddings in command replies --- cache.js | 22 ++++ commands/core/profile.js | 14 --- commands/core/role.js | 38 ++++++ commands/core/server.js | 27 +++++ commands/core/user.js | 26 +++++ commands/core/userContextMenu.js | 26 +++++ commands/core/who.js | 14 --- index.js | 2 +- package-lock.json | 195 +++++++++++++++++++++++++++++-- package.json | 2 + 10 files changed, 330 insertions(+), 36 deletions(-) create mode 100644 cache.js delete mode 100644 commands/core/profile.js create mode 100644 commands/core/role.js create mode 100644 commands/core/server.js create mode 100644 commands/core/user.js create mode 100644 commands/core/userContextMenu.js delete mode 100644 commands/core/who.js diff --git a/cache.js b/cache.js new file mode 100644 index 0000000..b1fb801 --- /dev/null +++ b/cache.js @@ -0,0 +1,22 @@ +import Keyv from 'keyv'; +import KeyvRedis from '@keyv/redis'; + +const keyvRedis = new KeyvRedis('redis://127.0.0.1:6379'); +console.log(keyvRedis); + +const cache = new Keyv({ store: keyvRedis, namespace: 'cache' }); +console.log(cache); + +const users = new Keyv(new KeyvRedis('redis://localhost:6379'), { namespace: 'users' }); +console.log(users); + +await users.set("foo", "bar"); +console.log(await users.get('foo')); + +await cache.set('testing', false); +await cache.set('foo', 'foobar'); +await cache.set('bar', 'barfoo'); +console.log(await cache.get('testing')); +console.log(await cache.get('foo')); +console.log(await cache.get('bar')); +await cache.clear(); diff --git a/commands/core/profile.js b/commands/core/profile.js deleted file mode 100644 index ac7901d..0000000 --- a/commands/core/profile.js +++ /dev/null @@ -1,14 +0,0 @@ -import { ContextMenuCommandBuilder, ApplicationCommandType } from 'discord.js'; - -export const data = new ContextMenuCommandBuilder().setName('Profile').setType(ApplicationCommandType.User); - -export async function execute(interaction) { - if (!interaction.isUserContextMenuCommand()) return; - - await interaction.deferReply({ ephemeral: true }); - - const username = interaction.user.username; - console.log(username); - - await interaction.followUp({ content: username, ephemeral: true }); -}; diff --git a/commands/core/role.js b/commands/core/role.js new file mode 100644 index 0000000..3564f60 --- /dev/null +++ b/commands/core/role.js @@ -0,0 +1,38 @@ +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; + +export const data = new SlashCommandBuilder().setName('role').setDescription('Display role info').addRoleOption(option => option.setName('role').setDescription("The role you want to get the info of").setRequired(true)); + +export async function execute(interaction) { + const { options } = interaction; + + const role = options.getRole('role'); + + if (!role || !role.id) return interaction.reply({ content: `That role **doesn't** seem to exist in ${interaction.guild.name}`, ephemeral: true }); + if (role.name === "@everyone") return interaction.reply({ content: `You **cannot** get the info of the \`\`@everyone\`\``, ephemeral: true }); + if (role.name === "@here") return interaction.reply({ content: `You **cannot** get the info of the \`\`@here\`\``, ephemeral: true }); + + const created = parseInt(role.createdTimestamp / 1000); + const isMentionable = role.isMentionable ? "true" : "false"; + const isManaged = role.isManaged ? "true" : "false"; + const isHigher = role.isHigher ? "true" : "false"; + const position = role.position; + const isBotRole = role.isBotRole ? "true" : "false"; + + const roleEmbed = new EmbedBuilder() + .setColor(role.color) + .setTitle("Role info") + .addFields( + { name: "Name", value: `> ${role.name}` }, + { name: "Color", value: `> ${role.hexColor}` }, + { name: "Mention", value: `> @${role.name}` }, + { name: "Hoisted", value: `> ${isHigher}` }, + { name: "Position", value: `> ${position}` }, + { name: "Mentionable", value: `> ${isMentionable}` }, + { name: "Managed", value: `> ${isManaged}` }, + { name: "Bot", value: `> ${isBotRole}` }, + { name: "Created", value: `> ` }) + .setFooter({ text: `Role ID: ${role.id}` }) + .setThumbnail(role.iconURL()); + + await interaction.reply({ embeds: [roleEmbed], ephemeral: true }) +} diff --git a/commands/core/server.js b/commands/core/server.js new file mode 100644 index 0000000..67307c6 --- /dev/null +++ b/commands/core/server.js @@ -0,0 +1,27 @@ +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; + +export const data = new SlashCommandBuilder().setName('server').setDescription('Display server info'); + +export async function execute(interaction) { + if (!interaction.isChatInputCommand()) return; + + await interaction.deferReply({ ephemeral: true }); + + const server = interaction.guild; + const members = interaction.guild.memberCount; + const created = parseInt(interaction.guild.createdTimestamp / 1000); + + const serverEmbed = new EmbedBuilder() + .setColor('#777777') + .setTitle("Server info") + .addFields( + { name: "Name", value: `> ${server.name}` }, + { name: "Members", value: `> ${members}` }, + { name: "Owner ID", value: `> ${server.ownerId}` }, + { name: "Created", value: `> ` }) + .setFooter({ text: `Server ID: ${server.id}` }) + .setThumbnail(server.iconURL()) + + const reply = { embeds: [serverEmbed], ephemeral: true }; + await interaction.followUp(reply); +}; diff --git a/commands/core/user.js b/commands/core/user.js new file mode 100644 index 0000000..6094e69 --- /dev/null +++ b/commands/core/user.js @@ -0,0 +1,26 @@ +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; + +export const data = new SlashCommandBuilder().setName('user').setDescription('Display user info'); + +export async function execute(interaction) { + if (!interaction.isChatInputCommand()) return; + + await interaction.deferReply({ ephemeral: true }); + + const user = interaction.user; + const created = parseInt(interaction.user.createdTimestamp / 1000); + + const userEmbed = new EmbedBuilder() + .setColor('#195ece') + .setTitle("User info") + .addFields( + { name: "Username", value: `> ${user.username}` }, + { name: "Joined", value: `> ` }) + .setFooter({ text: `User ID: ${user.id}` }) + .setThumbnail(user.displayAvatarURL()); + + const reply = { embeds: [userEmbed], ephemeral: true }; + console.log(reply); + + await interaction.followUp(reply); +}; diff --git a/commands/core/userContextMenu.js b/commands/core/userContextMenu.js new file mode 100644 index 0000000..faac85d --- /dev/null +++ b/commands/core/userContextMenu.js @@ -0,0 +1,26 @@ +import { ContextMenuCommandBuilder, ApplicationCommandType, EmbedBuilder } from 'discord.js'; + +export const data = new ContextMenuCommandBuilder().setName('User Info').setType(ApplicationCommandType.User); + +export async function execute(interaction) { + if (!interaction.isUserContextMenuCommand()) return; + + await interaction.deferReply({ ephemeral: true }); + + const user = interaction.user; + const created = parseInt(interaction.user.createdTimestamp / 1000); + + const userContextMenuEmbed = new EmbedBuilder() + .setColor('#195ece') + .setTitle("User info") + .addFields( + { name: "Username", value: `> ${user.username}` }, + { name: "Joined", value: `> ` }) + .setFooter({ text: `User ID: ${user.id}` }) + .setThumbnail(user.displayAvatarURL()); + + const reply = { embeds: [userContextMenuEmbed], ephemeral: true }; + console.log(reply); + + await interaction.followUp(reply); +}; diff --git a/commands/core/who.js b/commands/core/who.js deleted file mode 100644 index 7eb4a76..0000000 --- a/commands/core/who.js +++ /dev/null @@ -1,14 +0,0 @@ -import { ContextMenuCommandBuilder, ApplicationCommandType } from 'discord.js'; - -export const data = new ContextMenuCommandBuilder().setName('Who').setType(ApplicationCommandType.Message); - -export async function execute(interaction) { - if (!interaction.isMessageContextMenuCommand()) return; - - await interaction.deferReply({ ephemeral: true }); - - const username = interaction.user.username; - console.log(username); - - await interaction.followUp({ content: username, ephemeral: true }); -}; diff --git a/index.js b/index.js index 52aa4c4..6600bbb 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ for (const folder of commandFolders) { client.once(Events.ClientReady, () => { console.log(`Ready! Logged in as ${client.user.tag}`); - client.user.setPresence({ activities: [], status: 'invisible' }); + client.user.setPresence({ activities: [], status: 'online' }); }); client.on(Events.InteractionCreate, async (interaction) => { diff --git a/package-lock.json b/package-lock.json index 3d969ba..fd184d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,10 @@ "version": "0.1.0", "license": "MIT", "dependencies": { + "@keyv/redis": "^3.0.1", "discord.js": "^14.15.3", "dotenv": "^16.4.5", + "keyv": "^5.0.1", "openai": "^4.53.2" }, "devDependencies": { @@ -285,6 +287,33 @@ "url": "https://github.com/sponsors/nzakas" } }, + "node_modules/@ioredis/commands": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.2.0.tgz", + "integrity": "sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg==", + "license": "MIT" + }, + "node_modules/@keyv/redis": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@keyv/redis/-/redis-3.0.1.tgz", + "integrity": "sha512-eyqzomQC76LjUOEkPP8rdR2Fk4eZBSS0Ma47i7CNiQuv8NCw3trZvghx8L5Xruk7XPEj/eRAMrAxP//xQFOPdQ==", + "license": "MIT", + "dependencies": { + "ioredis": "^5.4.1" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@keyv/serialize": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@keyv/serialize/-/serialize-1.0.1.tgz", + "integrity": "sha512-kKXeynfORDGPUEEl2PvTExM2zs+IldC6ZD8jPcfvI351MDNtfMlw9V9s4XZXuJNDK2qR5gbEKxRyoYx3quHUVQ==", + "license": "MIT", + "dependencies": { + "buffer": "^6.0.3" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -504,6 +533,26 @@ "dev": true, "license": "MIT" }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -515,6 +564,30 @@ "concat-map": "0.0.1" } }, + "node_modules/buffer": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.2.1" + } + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -542,6 +615,15 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/cluster-key-slot": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz", + "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -600,7 +682,6 @@ "version": "4.3.6", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz", "integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==", - "dev": true, "license": "MIT", "dependencies": { "ms": "2.1.2" @@ -618,7 +699,6 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true, "license": "MIT" }, "node_modules/deep-is": { @@ -637,6 +717,15 @@ "node": ">=0.4.0" } }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10" + } + }, "node_modules/discord-api-types": { "version": "0.37.83", "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.83.tgz", @@ -931,6 +1020,16 @@ "node": ">=16" } }, + "node_modules/flat-cache/node_modules/keyv": { + "version": "4.5.4", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", + "integrity": "sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==", + "dev": true, + "license": "MIT", + "dependencies": { + "json-buffer": "3.0.1" + } + }, "node_modules/flatted": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.1.tgz", @@ -1016,6 +1115,26 @@ "ms": "^2.0.0" } }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "BSD-3-Clause" + }, "node_modules/ignore": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", @@ -1053,6 +1172,30 @@ "node": ">=0.8.19" } }, + "node_modules/ioredis": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.4.1.tgz", + "integrity": "sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==", + "license": "MIT", + "dependencies": { + "@ioredis/commands": "^1.1.1", + "cluster-key-slot": "^1.1.0", + "debug": "^4.3.4", + "denque": "^2.1.0", + "lodash.defaults": "^4.2.0", + "lodash.isarguments": "^3.1.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0", + "standard-as-callback": "^2.1.0" + }, + "engines": { + "node": ">=12.22.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/ioredis" + } + }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", @@ -1128,13 +1271,12 @@ "license": "MIT" }, "node_modules/keyv": { - "version": "4.5.4", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", - "integrity": "sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==", - "dev": true, + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/keyv/-/keyv-5.0.1.tgz", + "integrity": "sha512-NH+3ditq1O5uTSQiiHrGOTkwUniRox/lZ8tHARdsu5Skyv0AhZca0OCycWfR1fTECvSRftMQnXqx7cBpxo8G1g==", "license": "MIT", "dependencies": { - "json-buffer": "3.0.1" + "@keyv/serialize": "*" } }, "node_modules/levn": { @@ -1173,6 +1315,18 @@ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", "license": "MIT" }, + "node_modules/lodash.defaults": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", + "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==", + "license": "MIT" + }, + "node_modules/lodash.isarguments": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", + "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==", + "license": "MIT" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -1443,6 +1597,27 @@ ], "license": "MIT" }, + "node_modules/redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==", + "license": "MIT", + "dependencies": { + "redis-errors": "^1.0.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -1511,6 +1686,12 @@ "node": ">=8" } }, + "node_modules/standard-as-callback": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.1.0.tgz", + "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==", + "license": "MIT" + }, "node_modules/strip-ansi": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", diff --git a/package.json b/package.json index cd833c5..dcb7978 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,10 @@ "register": "node deploy-commands.js" }, "dependencies": { + "@keyv/redis": "^3.0.1", "discord.js": "^14.15.3", "dotenv": "^16.4.5", + "keyv": "^5.0.1", "openai": "^4.53.2" }, "devDependencies": {