This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Crypto Command Other files were changed automatically with `npm run format` * Specify 24h Volume (!crypto) Co-authored-by: Vlad <[email protected]>
- Loading branch information
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters