From a61fa8f4f262c5792892876e675449cb8ee938e6 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Tue, 4 Apr 2023 20:50:02 -0500 Subject: [PATCH] Added /drawtriangle command --- README.md | 16 +++--- commands/drawTriangle.ts | 120 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 commands/drawTriangle.ts diff --git a/README.md b/README.md index 07affd5..53ef878 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,17 @@ # NoMoreAcronyms -This is a bot that identifies abbreviations in a sentence and replaces them with the written out phrase!
-Example:
+This is a bot that identifies abbreviations in a sentence and replaces them with the written out phrase!\n +Example:\n ![Example](images/example.png) -
+\n To run an instance of this bot, define your bot token, guildId (Server ID), and client ID in key.json, then run: -
+\n `npm install discord.js` -
+\n After that, register the bot commands: -
+\n `node deploy-commands.js` -
+\n And now all you need to do is start the bot! -
+\n `node main.ts` diff --git a/commands/drawTriangle.ts b/commands/drawTriangle.ts new file mode 100644 index 0000000..c257fd0 --- /dev/null +++ b/commands/drawTriangle.ts @@ -0,0 +1,120 @@ +const { SlashCommandBuilder, Discord, codeBlock } = require('discord.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('drawdesign') + .setDescription('Draws a design in a codeblock') + .addStringOption(option => + option.setName('design') + .setDescription("Pick a design. (number 1-3)") + .setRequired(true) + ) + .addStringOption(option => + option.setName('string') + .setDescription("String to feed into input") + .setRequired(true) + ), + async execute(interaction, client) { + var inputString = interaction.options.getString('string'); + var design = interaction.options.getString('string'); + if (design < 1 || design > 3) { + await interaction.reply('Invalid design choice.'); + } + if (inputString.length > 1000) { + await interaction.reply('String is too big. Resultant output would be too big to send to Discord.') + } + switch (design) { + case 1: + var output = design1(inputString); + break; + case 2: + var output = design2(inputString); + break; + case 3: + var output = design3(inputString); + break; + + } + await interaction.reply(codeBlock("", output)); + console.log("User " + interaction.user.tag + " ran /drawtriangle"); + }, +}; +function design1(input) { + var outputString; + var startEnd = ""; + for (let i = 0; i < input.length + 2; i++) { + startEnd += "*"; + } + outputString = startEnd + "\n"; + outputString += "*" + "\n"; + outputString += "* " + input + "\n"; + outputString += "*" + "\n"; + outputString += startEnd; + return outputString; +} +function design2(input) { + var outputString; + var height = (input.length * 2) + 6; + var mid = Math.trunc(height / 2) + 1; + if (height % 2 == 0) { + height++; + } + outputString = ""; + for (let i = 0; i < height; i++) { + if (mid == i) { //Determine if we're in the middle of the triangle + outputString += "**" + input + "**"; + } else { + for (let j = 0; j < i; j++) { + outputString += "*"; + } + } + outputString += "\n"; + } + return outputString; +} +function design3(input) { + var outputString; + var height = (input.length * 2) + 6; + var mid = Math.trunc(height / 2) + 1; + if (height % 2 == 0) { + height++; + } + outputString = ""; + if (mid % 2 == 1) { // Skip a line if odd + var subOffset = 1; + var addOffset = 0; + } else { + var subOffset = 0; + var addOffset = 1; + } + for (let i = 0; i < mid - addOffset; i++) { + for (let j = 0; j < i; j++) { + if (i % 2 == 1) { + if (i != (mid - 1)) { + if (i == 0 || (j == 0 || j == (i - 1))) { // Check if we're at the beginning or end of the line + outputString += "*"; + } else { + outputString += " "; + } + } + } + } + outputString += "\n"; + } + outputString += "* " + input + " *\n"; + for (let i = mid - subOffset; i > 0; i--) { + for (let j = 0; j < i; j++) { + if (i % 2 == 1) { + if (j != (mid - 1) || (j != mid)) { + if ((i == 0 || (j == 0 || j == (i - 1)))) { // Check if we're at the beginning or end of the line + outputString += "*"; + } else { + outputString += " "; + } + } + } + } + outputString += "\n"; + } + return outputString; +} \ No newline at end of file