Skip to content

Instantly share code, notes, and snippets.

@tyrelsouza
Last active January 15, 2019 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrelsouza/f6f6b631499c3cd3d3f75221a08ed229 to your computer and use it in GitHub Desktop.
Save tyrelsouza/f6f6b631499c3cd3d3f75221a08ed229 to your computer and use it in GitHub Desktop.
HamBot for Discord
const _ = require('lodash')
const discord = require('discord.js')
const express = require('express')
const morsify = require('morsify')
const client = new discord.Client()
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split('')
const GAME_ROOM = "cw-practice"
const OPTIONS = { dash: '-', dot: '.', space: ' '}
const HELP = `
\`\`\`
Commands:
Any Channel:
\`!help\`
Show this message
\`!me [message]\`
Encode a message into Morse
You: \`!me K3TAS\`
Bot: \`Encoded: -.- ...-- - .- ...\`
\`!md [morse]\`
Decode dashes and dots, letter space separated, into letters.
You: \`!md -.-. .-- .. ... -.-. --- --- .-..\`
Bot: \`Decoded : C W I S C O O L\`
#cw-practice
\`!new\`
Start a new guessing game. Generates a new random letter and allows 5 wrong guesses.
Guesses will be considered any single character message.
If a game is in session, please no extra talking in the game room.
\`\`\`
`
let game = {
running: false,
channel: undefined
}
function clearGameChannel () {
game.channel.fetchMessages().then(
oldMsgs => oldMsgs.deleteAll()
)
game.channel.send('Get a new letter with `!new`')
}
function win (msg) {
clearGameChannel()
msg.reply(`Correct \`${game.code}\` is \`${game.letter}\`!`)
resetGame()
}
function lose (msg, why){
let channel = msg.channel
clearGameChannel()
if (why === 'cheating') {
channel.send(`${msg.author} is cheating, letter was \`${game.letter}\`. Game Ended`)
} else if (why === 'toomany') {
channel.send(`Game over! Too many wrong guesses. \`${game.code}\` is \`${game.letter}\``)
} else {
channel.send(`Game over! \`${game.code}\` is \`${game.letter}\``)
}
if (channel.name === GAME_ROOM) channel.send("`!new` for new game")
resetGame()
}
function resetGame () {
game.running = false
game.code = undefined
game.letter = undefined
game.guesses = 0
}
function newGame (msg) {
clearGameChannel()
game.running = true
game.letter = _.sample(ALPHABET)
game.code = morsify.encode(game.letter)
game.guesses = 0
}
function cheating (channel, message) {
return game.running && channel.name === GAME_ROOM && message.includes(game.letter)
}
client.on('message', msg => {
if(msg.author.username === 'HamBot') return // Don't process on self messages, prevent loops for unguarded messages
// Show the help message
if (msg.content === "!help"){
msg.reply(HELP)
return
}
// Encode any letters into morse code
if (msg.content.startsWith("!me ")){
let message = msg.content.slice(4).toLowerCase()
if (cheating(msg.channel, message)) {
lose(msg, 'cheating')
} else {
let words = []
message.split(" ").forEach((word) => { // Todo change to map
words.push(morsify.encode(word, OPTIONS))
})
let encoded = words.join(' / ')
msg.reply(`Encoded: \`${encoded}\``)
}
return
}
if (msg.content.startsWith("!md ")){
let message = msg.content.slice(4).toLowerCase()
let words = []
message.split("/").forEach((word) => { // Todo change to map
let decoded = morsify.decode(word.trim(), OPTIONS).toLowerCase().replace(/ /g, '')
words.push(decoded)
})
let decoded = words.join(' ')
if (cheating(msg.channel, decoded)) {
lose(msg, 'cheating')
} else {
msg.reply(`Decoded: \`${decoded}\``)
}
return
}
if (msg.channel.name === GAME_ROOM) {
if (!game.channel) game.channel = msg.channel // set game channel if not set
if (msg.content === '!new'){
newGame(msg)
game.channel.send('One letter responses are considered a guess.')
game.channel.send(`New letter, you have 60 seconds: \`${game.code}\``).then(
(sent) => sent.delete(60*1000).then(() => clearGameChannel(msg.channel))
)
} else if(game.running && msg.content.length === 1 ){
let guess = msg.content
if (guess.toLowerCase() === game.letter) {
win(msg)
} else {
msg.reply(`Sorry, not \`${guess}\``)
game.guesses += 1
if (game.guesses > 5){
lose(msg, 'toomany')
}
}
} else if(game.running) {
game.channel.send('Game is running, please do send messages of more than one letter.')
game.channel.send('One letter responses are considered a guess.')
}
return
}
})
client.login(process.env.BOT_TOKEN)
{
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "4.16.3",
"discord.js": "^11.4.2",
"morsify": "^1.0.0",
"lodash": "^4.17.11"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment