-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar_cipher.js
46 lines (39 loc) · 1.43 KB
/
caesar_cipher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const { SlashCommandBuilder } = require('discord.js')
module.exports = {
data: new SlashCommandBuilder()
.setName('caesar_cipher')
.setDescription('Use the caesar cipher on a piece of text')
.addStringOption((option) => option
.setName('text')
.setDescription('The text you want to apply the cipher to ')
.setRequired(true)
)
.addIntegerOption((option) => option
.setName('shift')
.setDescription('The shift you want to apply to the text')
.setRequired(true)
),
async execute(interaction) {
let text = interaction.options.getString('text')
let shift = interaction.options.getInteger('shift')
if (shift < 1) {
await interaction.reply({ content: '# Error\nShift must be a natural number', ephemeral: true })
return
}
text = text.toLowerCase()
shift = shift % 26
let alphabet = 'abcdefghijklmnopqrstuvwxyz'
let shifted = alphabet.slice(shift) + alphabet.slice(0, shift)
let encrypted = ''
for (let i = 0; i < text.length; i++) {
let char = text[i];
let index = alphabet.indexOf(char)
if (index !== -1) {
encrypted += shifted[index]
} else {
encrypted += char
}
}
await interaction.reply(encrypted)
}
}