Skip to content

Commit

Permalink
Merge pull request #1785 from fatiudeen/fatiudeen-#1
Browse files Browse the repository at this point in the history
Added Ceaser Cipher
  • Loading branch information
fineanmol authored Oct 1, 2022
2 parents a33db4e + 2727635 commit 0b1c810
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
109 changes: 109 additions & 0 deletions js/Bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const Discord = require("discord.js")
const fetch = require("node-fetch")
const keepAlive = require("./server")
const Database = require("@replit/database")

const db = new Database()
const client = new Discord.Client()

const sadWords = ["sad", "depressed", "unhappy", "angry"]

const starterEncouragements = [
"Cheer up!",
"Hang in there.",
"You are a great person / bot!"
]

db.get("encouragements").then(encouragements => {
if (!encouragements || encouragements.length < 1) {
db.set("encouragements", starterEncouragements)
}
})

db.get("responding").then(value => {
if (value == null) {
db.set("responding", true)
}
})

function updateEncouragements(encouragingMessage) {
db.get("encouragements").then(encouragements => {
encouragements.push([encouragingMessage])
db.set("encouragements", encouragements)
})
}

function deleteEncouragement(index) {
db.get("encouragements").then(encouragements => {
if (encouragements.length > index) {
encouragements.splice(index, 1)
db.set("encouragements", encouragements)
}
})
}

function getQuote() {
return fetch("https://zenquotes.io/api/random")
.then(res => {
return res.json()
})
.then(data => {
return data[0]["q"] + " -" + data[0]["a"]
})
}

client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})

client.on("message", msg => {
if (msg.author.bot) return

if (msg.content === "$inspire") {
getQuote().then(quote => msg.channel.send(quote))
}

db.get("responding").then(responding =>{
if (responding && sadWords.some(word => msg.content.includes(word))) {
db.get("encouragements").then(encouragements => {
const encouragement = encouragements[Math.floor(Math.random() * encouragements.length)]
msg.reply(encouragement)
})
}
})


if (msg.content.startsWith("$new")) {
encouragingMessage = msg.content.split("$new ")[1]
updateEncouragements(encouragingMessage)
msg.channel.send("New encouraging message added.")
}

if (msg.content.startsWith("$del")) {
index = parseInt(msg.content.split("$del ")[1])
deleteEncouragement(index)
msg.channel.send("Encouraging message deleted.")
}

if (msg.content.startsWith("$list")) {
db.get("encouragements").then(encouragements => {
msg.channel.send(encouragements)
})
}

if (msg.content.startsWith("$responding")) {
value = msg.content.split("$responding ")[1]

if (value.toLowerCase() == "true") {
db.set("responding", true)
msg.channel.send("Responding is on.")
} else {
db.set("responding", false)
msg.channel.send("Responding is off.")
}
}

})

keepAlive()
client.login(TOKEN)
44 changes: 44 additions & 0 deletions js/Caesar_Cipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// An algorithm to solve the Caesar Cipher problem.
// Example
// Input:
// text = ABCD , Key = 13
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
// 13 shift to A is N
// 13 shift to B is O
// 13 shift to C is P
// 13 shift to D is Q

// Output:
// NOPQ
let ceaserCipher = (str) => {
//Deciphered reference letters
let decoded = {
a: 'n', b: 'o', c: 'p',
d: 'q', e: 'r', f: 's',
g: 't', h: 'u', i: 'v',
j: 'w', k: 'x', l: 'y',
m: 'z', n: 'a', o: 'b',
p: 'c', q: 'd', r: 'e',
s: 'f', t: 'g', u: 'h',
v: 'i', w: 'j', x: 'k',
y: 'l', z: 'm'
}

//convert the string to lowercase
str = str.toLowerCase();

//decipher the code
let decipher = '';
for(let i = 0 ; i < str.length; i++){
decipher += decoded[str[i]];
}

//return the output
return decipher;
}
console.log(ceaserCipher('attackatonce'));
console.log(ceaserCipher('prashantyadav'));

// Expected output
// "nggnpxngbapr"
// "cenfunaglnqni"

0 comments on commit 0b1c810

Please sign in to comment.