Skip to content

Commit

Permalink
added new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardMcSorley committed Nov 19, 2018
1 parent 9b42ae0 commit 1e44974
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 166 deletions.
9 changes: 5 additions & 4 deletions commands/admin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const util = require("util");
const handle = ({ message, options, prefix }) => {
const handle = ({ message, options, prefix, bot }) => {
const usedPrefix = prefix.prefix[prefix.name];
const prefixIndex = message.content.indexOf(usedPrefix.value);
const msg = message.content.slice(prefixIndex + usedPrefix.value.length); // slice of the prefix on the message
Expand All @@ -17,11 +17,11 @@ const handle = ({ message, options, prefix }) => {
) {
// < checks the message author's id to owners
const code = args.join(" ");
return evalCmd(message, code);
return evalCmd({ message, code, bot, options, prefix });
}
};

function evalCmd(message, code) {
function evalCmd({ message, code, bot, options, prefix }) {
if (message.author.id !== process.env.KOREAN_DICT_BOT_DISCORD_OWNER) return;
try {
let evaled = eval(code);
Expand Down Expand Up @@ -52,7 +52,8 @@ module.exports = {
"!admin": {
match: "admin",
value: "!admin ",
lang: "en"
lang: "en",
display: false
}
}
};
6 changes: 4 additions & 2 deletions commands/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ module.exports = {
"!define": {
match: "define", // used to match type, useful if we have multiple for same function
value: "!define ", // what the user typed
lang: "en" // maybe use to respond in another language
lang: "en", // maybe use to respond in another language,
display: "lang"
},
"!정의": {
match: "define",
value: "!정의 ",
lang: "ko"
lang: "ko",
display: "lang"
}
}
};
6 changes: 4 additions & 2 deletions commands/ggami.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ module.exports = {
match: "ggami",
description: "What are you doing, Ggami?",
value: "!ggami ",
lang: "en"
lang: "en",
display: "lang"
},
"!까미": {
match: "ggami",
value: "!까미 ",
description: "까미~ 뭐해?",
lang: "ko"
lang: "ko",
display: "lang"
}
}
};
51 changes: 51 additions & 0 deletions commands/hangulify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const kpop = require("kpop");
const { hasKoreanTXT } = require("../utils/language");
const numberHandle = require("./number");
const handle = ({ message, options, bot, prefix }) => {
const usedPrefix = prefix.prefix[prefix.name];
const prefixIndex = message.content.indexOf(usedPrefix.value);
const msg = message.content.slice(prefixIndex + usedPrefix.value.length); // slice of the prefix on the message
const hasKOTXT = hasKoreanTXT(msg);
let string;
if (hasNumber) {
message.lang = hasKOTXT ? "ko" : "en";
return numberHandle.handle({ message, options, bot, prefix });
}
if (hasKOTXT) {
string = `Romanized that's ***${kpop.romanize(msg)}***`;
} else {
string = `Hungulified that's ***${kpop.hangulify(msg)}***`;
}
if (message.channel.type === "youtube") {
return message.channel.send(string);
}
options.setDescription(string);
return message.channel.send(options);
};
function hasNumber(myString) {
return /\d/.test(myString);
}

module.exports = {
handle,
prefix: {
"!hangul": {
match: "hangul",
value: "!hangul ",
lang: "en",
display: "lang"
},
"!hungulify": {
match: "hungulify",
value: "!hungulify ",
lang: "en",
display: "lang"
},
"!한굴": {
match: "한굴",
value: "!한굴 ",
lang: "ko",
display: "lang"
}
}
};
19 changes: 12 additions & 7 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ const handle = ({ message, options, bot }) => {
let commandString = "";
bot.commands
.array()
.filter(({ name }) => name !== "!admin")
.filter(
({ display, lang }) =>
display === true || (display === "lang" && message.lang === lang)
)
.forEach(({ name }) => {
commandString += ` ${name} `;
});
//bot.commands.tap(command => console.log(command));
if (message.channel.type === "youtube") {
return message.channel.send("Available commands:" + commandString);
}
options.setTitle("Here are all the my commands:");
options.setDescription(constants.HELPER_TEXT);
return message.channel.send("Available commands:" + commandString);
options.setDescription("Available commands:" + commandString);
return message.channel.send(options);
};

module.exports = {
Expand All @@ -22,17 +24,20 @@ module.exports = {
"!help": {
match: "help",
value: "!help ",
lang: "en"
lang: "en",
display: false
},
"!command": {
match: "help",
value: "!command ",
lang: "en"
lang: "en",
display: false
},
"!commands": {
match: "help",
value: "!commands ",
lang: "en"
lang: "en",
display: false
}
}
};
41 changes: 41 additions & 0 deletions commands/keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const keyboard = require("gksdud");
const errorTxt = "\nEX: ```!keyboard dkssud```";
const handle = ({ message, options, bot, prefix }) => {
const usedPrefix = prefix.prefix[prefix.name];
const prefixIndex = message.content.indexOf(usedPrefix.value);
const msg = message.content.slice(prefixIndex + usedPrefix.value.length); // slice of the prefix on the message
let string = "";
if (msg === "") {
string = "Sorry you have to type something." + errorTxt;
if (message.channel.type === "youtube") {
return message.channel.send(string);
}
options.setDescription(string);
return message.channel.send(options);
}
if (message.lang === "en") {
string = "Result: " + keyboard(msg);
} else {
string =
"Looks like you are typing in korean already. No need for me ^^" +
errorTxt;
}

if (message.channel.type === "youtube") {
return message.channel.send(string);
}
options.setDescription(string);
return message.channel.send(options);
};

module.exports = {
handle,
prefix: {
"!keyboard": {
match: "keyboard",
value: "!keyboard ",
lang: "en",
display: true
}
}
};
66 changes: 66 additions & 0 deletions commands/ko_name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const namer = require("korean-name-generator");
const preString = "How about ";
const sufString = " :smiley:";
const kpop = require("kpop");
const { titleCase } = require("../utils/format-text");
const handle = ({ message, options, bot, prefix }) => {
const msg = message.content.toLowerCase(); // dont slice just search whole text
let string = "";
if (!containsMale(msg) && !containsFemale(msg)) {
return message.channel.send(
"Please let me know your gender so I can give you a Korean name :wink: \nEX:```!name male|female```"
);
}
if (containsMale(msg)) {
const name = namer.generate(false);
const lastname = name.substring(0, 1);
const firstname = name.substring(-2);
const romanized = titleCase(kpop.romanize(firstname + " " + lastname));
string = `${preString} ${name} - ${romanized}? ${sufString}`;
} else if (containsFemale(msg)) {
const name = namer.generate(false);
const lastname = name.substring(0, 1);
const firstname = name.substring(-2);
const romanized = titleCase(kpop.romanize(firstname + " " + lastname));
string = `${preString} ${name} - ${romanized}? ${sufString}`;
}

if (message.channel.type === "youtube") {
return message.channel.send(string);
}
return message.channel.send(string);
};
const containsMale = msg => {
return (
msg.includes("male") ||
msg.includes("boy") ||
msg.includes("guy") ||
msg.includes("man")
);
};
const containsFemale = msg => {
return msg.includes("female") || msg.includes("girl") || msg.includes("gal");
};
module.exports = {
handle,
prefix: {
"!name": {
match: "name",
value: "!name ",
lang: "en",
display: true
},
name_me: {
match: "name",
value: "name me ",
lang: "en",
display: false
},
korean_name: {
match: "name",
value: "name me ",
lang: "en",
display: false
}
}
};
48 changes: 48 additions & 0 deletions commands/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { numberToKorean } = require("number-to-korean");
const kpop = require("kpop");
const koreanToNumber = require("korean-numbers");
var numeral = require("numeral");
const errorTxt = "\nEX: ```!number 3000```";
const handle = ({ message, options, bot, prefix }) => {
const usedPrefix = prefix.prefix[prefix.name];
const prefixIndex = message.content.indexOf(usedPrefix.value);
const msg = message.content.slice(prefixIndex + usedPrefix.value.length); // slice of the prefix on the message
let string = "";
if (msg === "") {
string = "Sorry you have to type something." + errorTxt;
if (message.channel.type === "youtube") {
return message.channel.send(string);
}
options.setDescription(string);
return message.channel.send(options);
}
if (message.lang === "ko") {
const englishNbr = koreanToNumber.parse(msg);
string = `In English **${msg}** - ${kpop.romanize(
msg.replace(/\d/, "")
)} is **${numeral(englishNbr).format("0,0")}**`;
} else {
const koreanNbr = numberToKorean(msg);
string = `In Korean **${msg}** is **${koreanNbr}** - ${kpop.romanize(
koreanNbr.replace(/\d/, "")
)}`;
}

if (message.channel.type === "youtube") {
return message.channel.send(string);
}
options.setDescription(string);
return message.channel.send(options);
};

module.exports = {
handle,
prefix: {
"!number": {
match: "number",
value: "!number ",
lang: "en",
display: true
}
}
};
12 changes: 8 additions & 4 deletions discord-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ broadcastState.broadcast.on("unsubscribe", dispatcher => {
console.log("Channel unsubscribed from broadcast :(");
});

broadcastState.broadcast.on("end", () => playNextSong());
broadcastState.broadcast.on("end", () => bot.playNextSong());
broadcastState.broadcast.on("error", () => {
broadcastState.broadcast.destroy();
broadcastState.broadcast = bot.createVoiceBroadcast();
Expand All @@ -52,7 +52,7 @@ bot.fire = action => {
stdio: ["ignore"]
});
subprocess.unref();
fire("shutdown"); // close this process
bot.fire("shutdown"); // close this process
return "restarting";
case "shutdown":
process.exit();
Expand All @@ -72,6 +72,7 @@ bot.fire = action => {
return "please provide an action";
}
};
bot.run = bot.fire; // sometimes I forget which one ;)

bot.prepareKLPChannel = () => {
bot.playNextSong();
Expand Down Expand Up @@ -150,7 +151,11 @@ loadFiles({ path: "./commands/" }, ({ library }) => {
const possibleCommands = Object.keys(library.prefix);
// add all prefixes of each command
possibleCommands.forEach(prefix => {
bot.commands.set(prefix, { ...library, name: prefix });
bot.commands.set(prefix, {
...library,
name: prefix,
...library.prefix[prefix]
});
});
});
loadFiles({ path: "./events/discord.socket.io/" }, ({ library }) => {
Expand All @@ -161,6 +166,5 @@ loadFiles({ path: "./events/discord.bot/" }, ({ library }) => {
bot.on(library.name, message => library.handle({ message, bot, db }));
});
loadFiles({ path: "./events/firebase/" }, ({ library }) => {
// bot.selfEVENTS.set(library.name, library);
library.ref.on(library.name, message => library.handle({ message, bot, db }));
});
Loading

0 comments on commit 1e44974

Please sign in to comment.