Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Models/CryptoData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace CouchBot.UserCommands.Models
{
public class CryptoData
{
[JsonProperty("bitcoin")]
public BitcoinData Bitcoin { get; set; }

public class BitcoinData
{
[JsonProperty("usd")]
public double Usd { get; set; }

[JsonProperty("usd_24h_change")]
public double Usd24hChange { get; set; }
}
}
}
39 changes: 27 additions & 12 deletions Modules/UserCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class UserCommands : ModuleBase
public async Task GetCovidDataAsync()
{
string output;

try
{
using var client = new HttpClient { BaseAddress = new Uri("https://corona.lmao.ninja/") };
Expand Down Expand Up @@ -68,33 +67,49 @@ public async Task GetCovidDataAsync()
await ReplyAsync(output);
}

[Command("Advice", RunMode = RunMode.Async)]
[Alias("advice", "quote", "tip")]
[Summary("returns a random life advice")]
[Command("bitcoin", RunMode = RunMode.Async)]
[Alias("bitcoin price", "bitcoin status", "bitcoin updates", "bitcoin update", "bitcoin")]
[Summary("returns the current price of bitcoin vs usd")]
public async Task GetAdviceAsync()
{
string errorMessage = "An error occured while fetching advice";

string errorMessage = "Unable to fetch current price of bitcoin. Try later!";
string output;
try
{
using var client = new HttpClient { BaseAddress = new Uri("https://api.adviceslip.com/") };
var response = await client.GetAsync("advice");
using var client = new HttpClient { BaseAddress = new Uri("https://api.coingecko.com/") };
var response = await client.GetAsync("api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=true&include_last_updated_at=false");

if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var advice = JsonConvert.DeserializeObject<AdviceObject>(data);
await ReplyAsync(advice.AdviceSlip.AdviceText);
var btcData = (JsonConvert.DeserializeObject<CryptoData>(data));
var number = (int)btcData.Bitcoin.Usd24hChange;

output = number switch
{
0 => $"Just flat and constant! Bitcoin is {btcData.Bitcoin.Usd:n0} per USD with almost {btcData.Bitcoin.Usd24hChange:n0} percent change reported in last 24 hours.",
1 => $"Nothing much interesting in crypto market today! Currently bitcoin is {btcData.Bitcoin.Usd:n0} per USD, with just {btcData.Bitcoin.Usd24hChange:n0} percent change reported in last 24 hours.",
_ => $"Breaking News! There is a {btcData.Bitcoin.Usd24hChange:n0} percent change in price of bitcoin, currently standing at {btcData.Bitcoin.Usd:n0} per USD, "
};
}
else
{
await ReplyAsync(errorMessage);
output = errorMessage;
}
}
catch (Exception)
{
await ReplyAsync(errorMessage);
output = errorMessage;
}
await ReplyAsync(output);
}

[Command("commandName", RunMode = RunMode.Async)]
[Alias("alias1", "alias2", "etc")]
[Summary("Description of the command")]
public Task CommandAsync()
{
return ReplyAsync("Your Return Text!");
}
}
}