diff --git a/commands/crypto.ts b/commands/crypto.ts new file mode 100644 index 0000000..0328839 --- /dev/null +++ b/commands/crypto.ts @@ -0,0 +1,63 @@ +import { Message } from "discord.js"; +import { Command, CommandDefinition, Action } from "."; +import dotenv from "dotenv"; +import fetch from "node-fetch"; + +dotenv.config(); + +export const description: CommandDefinition = { + name: "Crypto Watcher", + description: + "Displays Crypto data (price, volume, change...) for the pair provided.", + usage: ["!crypto "], + keys: ["crypto"], +}; + +export const action: Action = async (message: Message): Promise => { + const symbol = message.content.replace("!crypto ", "").toUpperCase(); + if (symbol === "") { + message.channel.send( + "I don't see a symbol... For example, you can do `!crypto BASE-TARGET`, i.e. `!crypto btc-usd`" + ); + return; + } + + return fetch(`https://api.cryptonator.com/api/ticker/${symbol}`) + .then((response: any) => { + if (response.status !== 200) throw new Error("API Not OK"); + return response.json(); + }) + .then((res: any) => { + if (res === undefined || res.length === 0) + // Result of an invalid symbol provided + message.channel.send( + "Invalid Symbol provided, but you can try again! :chart_with_upwards_trend:" + ); + else { + return message.channel.send( + `**${symbol}** :chart_with_upwards_trend:\nCurrent: \`${ + res.ticker.price + } ${res.ticker.target}\`\n24h Volume:\`${res.ticker.volume} ${ + res.ticker.base + }\`\nPast Hour Change:\`${res.ticker.change} ${ + res.ticker.target + }\`\nPast Hour Change Percentage:\`${ + Math.round((res.ticker.change / res.ticker.price) * 100 * 1000) / + 100 + }%\`` + ); + } + }) + .catch((err: any) => { + console.error(err); + return message.channel.send( + "Whoops... Looks like the API hit an error. Make sure the format you're using is `!crypto BASE-TARGET`. For example, `!crypto btc-usd`." + ); + }); +}; + +export const command: Command = { + definition: description, + action: action, +}; +export default command; diff --git a/commands/index.ts b/commands/index.ts index 6f9ca84..da8f5eb 100644 --- a/commands/index.ts +++ b/commands/index.ts @@ -13,8 +13,9 @@ import Tone from "./tone"; import Remind from "./remind"; import MadLibs from "./madlibs"; import FSM from "./state-machine"; -import Music from './music'; +import Music from "./music"; import NewMember from "./newmember"; +import Crypto from "./crypto"; // To register a command, import it above and add it to this array. export const commands: Command[] = [ @@ -31,6 +32,7 @@ export const commands: Command[] = [ Roulette, Remind, NewMember, + Crypto, PingPong, Contribute, ]; diff --git a/commands/madlibs.ts b/commands/madlibs.ts index 5865e35..49fba9a 100644 --- a/commands/madlibs.ts +++ b/commands/madlibs.ts @@ -28,9 +28,7 @@ wordValues.push([ // Add duplicate "person" as "name" wordKeys.push("name"); -wordValues.push([ - ...words.person, -]); +wordValues.push([...words.person]); function replaceInsertions(word: string): string { // If the word doesn't start with an insertion operator, throw it to the wolves. diff --git a/commands/state-machine.ts b/commands/state-machine.ts index 88ec8e1..cc0655c 100644 --- a/commands/state-machine.ts +++ b/commands/state-machine.ts @@ -7,7 +7,8 @@ axiosRetry(axios, { retries: 3 }); export const description: CommandDefinition = { name: "Finite State Machine", - description: "Returns a pretty image of an FSA. Takes optional start, end, and engine parameters.", + description: + "Returns a pretty image of an FSA. Takes optional start, end, and engine parameters.", usage: [ "!fsm <[edge],[source],[target]>+ ", "!fsm a,x,y b,x,z a,y,x b,y,z b,y,z a,z,z b,z,z start=x end=z", @@ -52,7 +53,9 @@ export const action: Action = async (message: Message): Promise => { // Return early if input is malformed. if (points.length < 1) - return message.reply("Please provide at least two points and an edge, like 'a,x,y'"); + return message.reply( + "Please provide at least two points and an edge, like 'a,x,y'" + ); if (start.length > 1) return message.reply("Please provide just one 'start=source' definition."); if (end.length > 1) @@ -65,7 +68,7 @@ export const action: Action = async (message: Message): Promise => { if (end.length === 1) data.end = end[0].charAt(start[0].length); if (engine.length === 1) data.engine = engine[0].split("=")[1]; - console.log(`Calling fsa-svc with data: ${JSON.stringify(data)}`); + console.log(`Calling fsa-svc with data: ${JSON.stringify(data)}`); return axios .post(endpoint, data, config) diff --git a/commands/translate.ts b/commands/translate.ts index 9c127cc..890d1fd 100644 --- a/commands/translate.ts +++ b/commands/translate.ts @@ -84,7 +84,9 @@ export const translate = async ( } else { // Key can be !translate or !baguette return translateIBM(text, "", "en-fr-CA").then(async (translation) => { - return message.reply(`the French Canadian translation is '${translation}'`); + return message.reply( + `the French Canadian translation is '${translation}'` + ); }); }