-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateCommand.cs
36 lines (33 loc) · 1.52 KB
/
CalculateCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
namespace HyBot.Commands {
public class CalculateCommand : ApplicationCommandModule {
[SlashCommand("calculate", "Calculate a math expression.")]
public async Task CalculatorCommandAsync(InteractionContext ctx,
[Option("expression", "The math expression to calculate.")] string expression) {
// Calculate the result of the expression
double result = 0;
try
{
result = Convert.ToDouble(new System.Data.DataTable().Compute(expression, null));
}
catch (Exception)
{
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent("Invalid expression."));
return;
}
// Create the embed for the result
var embed = new DiscordEmbedBuilder()
.WithTitle("Calculator")
.WithDescription($"`{expression}` = **{result}**")
.WithFooter(
$"Calculated by {ctx.User.Username} at {DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")} in #{ctx.Channel.Name}")
.WithColor(DiscordColor.Green);
// Send the embed to the channel
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().AddEmbed(embed.Build()));
}
}
}