Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
Crypto Command (#47)
Browse files Browse the repository at this point in the history
* Crypto Command

Other files were changed automatically with `npm run format`

* Specify 24h Volume (!crypto)

Co-authored-by: Vlad <[email protected]>
  • Loading branch information
Vlad J and vladjdk authored Mar 17, 2021
1 parent f442154 commit 2227979
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
63 changes: 63 additions & 0 deletions commands/crypto.ts
Original file line number Diff line number Diff line change
@@ -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 <base-target>"],
keys: ["crypto"],
};

export const action: Action = async (message: Message): Promise<any> => {
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;
4 changes: 3 additions & 1 deletion commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand All @@ -31,6 +32,7 @@ export const commands: Command[] = [
Roulette,
Remind,
NewMember,
Crypto,
PingPong,
Contribute,
];
Expand Down
4 changes: 1 addition & 3 deletions commands/madlibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions commands/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]>+ <start=[source]> <end=[target]> <engine=[engine]>",
"!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",
Expand Down Expand Up @@ -52,7 +53,9 @@ export const action: Action = async (message: Message): Promise<any> => {

// 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)
Expand All @@ -65,7 +68,7 @@ export const action: Action = async (message: Message): Promise<any> => {
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)
Expand Down
4 changes: 3 additions & 1 deletion commands/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`
);
});
}

Expand Down

0 comments on commit 2227979

Please sign in to comment.