177 lines
4.4 KiB
TypeScript
177 lines
4.4 KiB
TypeScript
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-4)")
|
|
.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 = parseInt(interaction.options.getString('design'));
|
|
if (design < 1 || design > 4) {
|
|
await interaction.reply('Invalid design choice.');
|
|
return;
|
|
}
|
|
var output;
|
|
switch (design) {
|
|
case 1:
|
|
output = design1(inputString);
|
|
break;
|
|
case 2:
|
|
output = design2(inputString);
|
|
break;
|
|
case 3:
|
|
output = design3(inputString);
|
|
break;
|
|
case 4:
|
|
output = design4(inputString);
|
|
}
|
|
console.log(codeBlock("", output));
|
|
var testLength = "\n" + 'output length was ' + (output.length) + " characters.";
|
|
if (output.length + testLength.length > 2000) {
|
|
await interaction.reply('Error: String is too big. Resultant output would be too big to send to Discord. (the output length was ' + (output.length + testLength.length) + " characters.)");
|
|
return;
|
|
}
|
|
await interaction.reply(codeBlock("", output) + testLength);
|
|
console.log("User " + interaction.user.tag + " ran /drawtriangle");
|
|
},
|
|
};
|
|
function design1(input) {
|
|
console.log("Running design1...");
|
|
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) {
|
|
console.log("Running design2...");
|
|
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) {
|
|
console.log("Running design3...");
|
|
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;
|
|
}
|
|
|
|
function design4(input) {
|
|
var outputString = "";
|
|
var radius = input.length +1;
|
|
var mid = radius;
|
|
if (mid % 2 == 1) {
|
|
var spaceOffset = " ";
|
|
} else {
|
|
var spaceOffset = "";
|
|
}
|
|
|
|
// dist represents distance to the center
|
|
var dist = parseFloat(0);
|
|
var space = ""
|
|
for (let i = 0; i < (input.length / 2); ++i) {
|
|
space += " ";
|
|
}
|
|
// for horizontal movement
|
|
for (var i = 0; i <= 2 * radius; i++) {
|
|
if (i != mid && j != 0) {
|
|
// for vertical movement
|
|
for (var j = 0; j <= 2 * radius; j++) {
|
|
|
|
dist = Math.sqrt(
|
|
(i - radius) * (i - radius) +
|
|
(j - radius) * (j - radius)
|
|
);
|
|
|
|
// dist should be in the range (radius - 0.5)
|
|
// and (radius + 0.5) to print stars(*)
|
|
|
|
|
|
if (dist > radius - 0.5 && dist < radius + 0.5) {
|
|
outputString += "*";
|
|
} else {
|
|
outputString += " ";
|
|
}
|
|
|
|
}
|
|
} else {
|
|
outputString += "*" + space + input + space + spaceOffset + "*";
|
|
}
|
|
|
|
|
|
outputString += "\n";
|
|
}
|
|
return outputString;
|
|
} |