2023-04-05 08:30:58 +07:00
const { SlashCommandBuilder , Discord , codeBlock , EmbedBuilder } = require ( 'discord.js' ) ;
const fs = require ( 'fs' ) ;
var path = require ( 'node:path' ) ;
2023-04-05 00:49:38 +07:00
//const { writeFile } = require('../main.cjs');
2023-04-04 20:50:02 +07:00
module . exports = {
data : new SlashCommandBuilder ( )
. setName ( 'drawdesign' )
. setDescription ( 'Draws a design in a codeblock' )
. addStringOption ( option =>
option . setName ( 'design' )
2023-04-08 23:13:51 +07:00
. setDescription ( "Pick a design. (number 1-5)" )
2023-04-04 20:50:02 +07:00
. setRequired ( true )
)
. addStringOption ( option =>
option . setName ( 'string' )
. setDescription ( "String to feed into input" )
. setRequired ( true )
) ,
async execute ( interaction , client ) {
2023-04-05 00:49:38 +07:00
let inputString = interaction . options . getString ( 'string' ) ;
let design = parseInt ( interaction . options . getString ( 'design' ) ) ;
2023-04-08 23:15:03 +07:00
if ( design < 1 || design > 5 ) {
2023-04-04 20:50:02 +07:00
await interaction . reply ( 'Invalid design choice.' ) ;
2023-04-04 21:23:21 +07:00
return ;
2023-04-04 20:50:02 +07:00
}
2023-04-05 00:49:38 +07:00
let output ;
2023-04-04 20:50:02 +07:00
switch ( design ) {
case 1 :
2023-04-04 21:08:24 +07:00
output = design1 ( inputString ) ;
2023-04-04 20:50:02 +07:00
break ;
case 2 :
2023-04-04 21:08:24 +07:00
output = design2 ( inputString ) ;
2023-04-04 20:50:02 +07:00
break ;
case 3 :
2023-04-04 21:08:24 +07:00
output = design3 ( inputString ) ;
2023-04-04 20:50:02 +07:00
break ;
2023-04-04 23:40:36 +07:00
case 4 :
output = design4 ( inputString ) ;
2023-04-08 23:13:51 +07:00
break ;
case 5 :
output = design5 ( inputString ) ;
break ;
2023-04-04 20:50:02 +07:00
}
2023-04-08 22:32:44 +07:00
//console.log(codeBlock("", output));
2023-04-05 00:49:38 +07:00
let testLength = "\n" + 'output length was ' + ( output . length ) + " characters." ;
2023-04-04 21:26:00 +07:00
if ( output . length + testLength . length > 2000 ) {
2023-04-05 08:30:58 +07:00
//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.)");
let replyString = "output length was " + ( output . length ) + " characters." ;
let filePath = writeFile ( output ) ;
await interaction . reply ( { content : replyString , files : [ filePath ] } ) ;
fs . unlinkSync ( filePath ) ; // Delete file once we're done with it
} else {
await interaction . reply ( codeBlock ( "" , output ) + testLength ) ;
2023-04-04 21:18:51 +07:00
}
2023-04-04 20:50:02 +07:00
console . log ( "User " + interaction . user . tag + " ran /drawtriangle" ) ;
} ,
} ;
2023-04-05 00:49:38 +07:00
2023-04-05 08:30:58 +07:00
function writeFile ( content ) {
//console.log(content);
console . log ( "Attempting to write string to file..." ) ;
let filename = Date . now ( ) + '.txt' ;
console . log ( filename ) ;
var filePath = path . join ( _ _dirname , '..' , filename ) ;
console . log ( filePath ) ;
fs . writeFile ( filePath , content , err => {
if ( err ) {
console . error ( err ) ;
}
} ) ;
return filePath ;
2023-04-05 00:49:38 +07:00
}
2023-04-04 20:50:02 +07:00
function design1 ( input ) {
2023-04-04 21:10:00 +07:00
console . log ( "Running design1..." ) ;
2023-04-05 00:49:38 +07:00
let outputString ;
let startEnd = "" ;
2023-04-04 20:50:02 +07:00
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 ) {
2023-04-04 21:10:00 +07:00
console . log ( "Running design2..." ) ;
2023-04-05 00:49:38 +07:00
let outputString ;
let height = ( input . length * 2 ) + 6 ;
let mid = Math . trunc ( height / 2 ) + 1 ;
2023-04-04 20:50:02 +07:00
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 ) {
2023-04-04 21:10:00 +07:00
console . log ( "Running design3..." ) ;
2023-04-05 00:49:38 +07:00
let outputString ;
let height = ( input . length * 2 ) + 6 ;
let mid = Math . trunc ( height / 2 ) + 1 ;
2023-04-04 20:50:02 +07:00
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 {
2023-04-04 21:13:42 +07:00
outputString += " " ;
2023-04-04 20:50:02 +07:00
}
}
}
}
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 {
2023-04-04 21:13:42 +07:00
outputString += " " ;
2023-04-04 20:50:02 +07:00
}
}
}
}
outputString += "\n" ;
}
return outputString ;
2023-04-04 23:40:36 +07:00
}
function design4 ( input ) {
2023-04-05 00:49:38 +07:00
let outputString = "" ;
2023-04-08 22:25:20 +07:00
let outputLine = "" ;
let radius = ( input . length + 1 ) ;
2023-04-05 00:49:38 +07:00
let mid = radius ;
2023-04-04 23:40:36 +07:00
if ( mid % 2 == 1 ) {
var spaceOffset = " " ;
} else {
var spaceOffset = "" ;
}
2023-04-08 22:25:20 +07:00
console . log ( radius , mid ) ;
2023-04-04 23:40:36 +07:00
// dist represents distance to the center
2023-04-05 00:49:38 +07:00
let dist = parseFloat ( 0 ) ;
let space = ""
2023-04-04 23:40:36 +07:00
for ( let i = 0 ; i < ( input . length / 2 ) ; ++ i ) {
space += " " ;
}
// for horizontal movement
2023-04-08 22:25:20 +07:00
var i ;
var j ;
let lineHasInput = false ;
for ( i = 0 ; i <= 2 * radius ; i ++ ) {
lineHasInput = false ;
if ( Math . trunc ( i ) != mid ) {
2023-04-04 23:40:36 +07:00
// for vertical movement
2023-04-08 22:25:20 +07:00
for ( j = 0 ; j <= 2 * radius ; j ++ ) {
2023-04-04 23:40:36 +07:00
dist = Math . sqrt (
2023-04-08 22:30:40 +07:00
( ( i - radius ) * ( i - radius ) * 4 ) + // (* 4) accounts for the offset between lines
2023-04-08 22:25:20 +07:00
( j - radius ) * ( j - radius )
2023-04-04 23:40:36 +07:00
) ;
2023-04-08 22:25:20 +07:00
//console.log(i, j, radius, dist);
2023-04-04 23:40:36 +07:00
// dist should be in the range (radius - 0.5)
// and (radius + 0.5) to print stars(*)
2023-04-08 22:25:20 +07:00
if ( dist > radius - 1 && dist < radius + 1 ) { //&& dist > radius - 1
lineHasInput = true ;
2023-04-09 09:02:13 +07:00
outputLine += "·" ; //zero-width space
2023-04-04 23:40:36 +07:00
} else {
2023-04-08 22:25:20 +07:00
outputLine += " " ;
2023-04-04 23:40:36 +07:00
}
}
} else {
2023-04-08 22:25:20 +07:00
lineHasInput = true ;
2023-04-09 09:02:13 +07:00
outputLine += "·" + space + input + space + spaceOffset + "·" ; //zero-width space
2023-04-04 23:40:36 +07:00
}
2023-04-08 22:25:20 +07:00
//console.log(i, j, outputLine);
if ( lineHasInput ) {
outputString += outputLine + "\n" ;
}
outputLine = "" ;
2023-04-04 23:40:36 +07:00
}
return outputString ;
2023-04-08 22:25:20 +07:00
}
2023-04-08 23:12:49 +07:00
var charIterateState ;
function charIterate ( input ) {
2023-04-08 23:16:33 +07:00
if ( charIterateState == undefined ) {
charIterateState = 0 ;
}
2023-04-08 23:19:43 +07:00
if ( charIterateState < input . length ) {
2023-04-08 23:12:49 +07:00
charIterateState ++ ;
} else {
charIterateState = 0 ;
}
2023-04-09 09:15:21 +07:00
console . log ( charIterateState , input . charAt ( charIterate ) ) ;
2023-04-08 23:12:49 +07:00
return charIterateState ;
}
function design5 ( input ) {
let outputString = "" ;
let outputLine = "" ;
let radius = ( input . length ) ;
let mid = radius ;
if ( mid % 2 == 1 ) {
var spaceOffset = " " ;
} else {
var spaceOffset = "" ;
}
console . log ( radius , mid ) ;
// dist represents distance to the center
let dist = parseFloat ( 0 ) ;
let space = ""
for ( let i = 0 ; i < ( input . length / 2 ) ; ++ i ) {
space += " " ;
}
// for horizontal movement
var i ;
var j ;
let lineHasInput = false ;
for ( i = 0 ; i <= 2 * radius ; i ++ ) {
lineHasInput = false ;
if ( Math . trunc ( i ) != mid ) {
// for vertical movement
for ( j = 0 ; j <= 2 * radius ; j ++ ) {
dist = Math . sqrt (
( ( i - radius ) * ( i - radius ) * 4 ) + // (* 4) accounts for the offset between lines
( j - radius ) * ( j - radius )
) ;
//console.log(i, j, radius, dist);
// dist should be in the range (radius - 0.5)
// and (radius + 0.5) to print stars(*)
if ( dist > radius - 1 && dist < radius + 1 ) { //&& dist > radius - 1
lineHasInput = true ;
2023-04-09 09:09:34 +07:00
outputLine += " " + input . charAt ( charIterate ( input ) ) ; //zero-width space
2023-04-08 23:12:49 +07:00
} else {
outputLine += " " ;
}
}
} else {
lineHasInput = true ;
2023-04-08 23:18:52 +07:00
outputLine += input . charAt ( charIterate ( input ) ) + space + input + space + spaceOffset + input . charAt ( charIterate ( input ) ) ;
2023-04-08 23:12:49 +07:00
}
//console.log(i, j, outputLine);
if ( lineHasInput ) {
outputString += outputLine + "\n" ;
}
outputLine = "" ;
}
return outputString ;
}
2023-04-08 22:25:39 +07:00
//console.log(design4("super long test string super long test string"));
2023-04-08 22:25:20 +07:00
//"super long test string super long test string"