mirror of
https://github.com/RPCS3/discord-bot.git
synced 2024-11-30 13:40:30 +00:00
hook in math parser help
This commit is contained in:
parent
750e97cac4
commit
1519333f03
57
CompatBot/Commands/BotMath.cs
Normal file
57
CompatBot/Commands/BotMath.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CompatBot.Commands.Attributes;
|
||||
using CompatBot.Utils;
|
||||
using DSharpPlus.CommandsNext;
|
||||
using DSharpPlus.CommandsNext.Attributes;
|
||||
using org.mariuszgromada.math.mxparser;
|
||||
|
||||
namespace CompatBot.Commands
|
||||
{
|
||||
[Group("math"), TriggersTyping]
|
||||
[Description("Math, here you go Juhn. Use `math help` for syntax help")]
|
||||
internal sealed class BotMath : BaseCommandModuleCustom
|
||||
{
|
||||
[GroupCommand, Priority(9)]
|
||||
public async Task Expression(CommandContext ctx, [RemainingText, Description("Math expression")] string expression)
|
||||
{
|
||||
var result = @"Something went wrong ¯\\_(ツ)\_/¯" + "\nMath is hard, yo";
|
||||
try
|
||||
{
|
||||
var expr = new Expression(expression);
|
||||
result = expr.calculate().ToString();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Log.Warn(e, "Math failed");
|
||||
}
|
||||
await ctx.RespondAsync(result).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("help"), LimitedToSpamChannel, Cooldown(1, 5*60, CooldownBucketType.Global)]
|
||||
[Description("General math expression help, or description of specific math word")]
|
||||
public Task Help(CommandContext ctx, string word = null)
|
||||
{
|
||||
var help = string.IsNullOrEmpty(word) ? mXparser.getHelp() : mXparser.getHelp(word);
|
||||
var hasR = help.Contains('\r');
|
||||
var hasN = help.Contains('\n');
|
||||
if (Environment.NewLine == "\r\n")
|
||||
{
|
||||
if (hasR && !hasN)
|
||||
help = help.Replace("\r", Environment.NewLine);
|
||||
else if (hasN && !hasR)
|
||||
help = help.Replace("\n", Environment.NewLine);
|
||||
}
|
||||
else if (Environment.NewLine == "\r" || Environment.NewLine == "\n")
|
||||
{
|
||||
if (hasR && hasN)
|
||||
help = help.Replace("\r\n", Environment.NewLine);
|
||||
else if (Environment.NewLine == "\r" && hasN)
|
||||
help = help.Replace("\n", Environment.NewLine);
|
||||
else if (Environment.NewLine == "\n" && hasR)
|
||||
help = help.Replace("\r", Environment.NewLine);
|
||||
}
|
||||
return ctx.SendAutosplitMessageAsync($"```{help}```");
|
||||
}
|
||||
}
|
||||
}
|
@ -3,13 +3,11 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using CompatBot.Commands.Attributes;
|
||||
using CompatBot.Utils;
|
||||
using DSharpPlus.CommandsNext;
|
||||
using DSharpPlus.CommandsNext.Attributes;
|
||||
using DSharpPlus.CommandsNext.Converters;
|
||||
using DSharpPlus.Entities;
|
||||
using org.mariuszgromada.math.mxparser;
|
||||
|
||||
namespace CompatBot.Commands
|
||||
{
|
||||
@ -95,23 +93,6 @@ namespace CompatBot.Commands
|
||||
await ctx.RespondAsync(embed: embed.Build());
|
||||
}
|
||||
|
||||
[Command("math"), TriggersTyping]
|
||||
[Description("Math, here you go Juhn")]
|
||||
public async Task Math(CommandContext ctx, [RemainingText, Description("Math expression")] string expression)
|
||||
{
|
||||
var result = @"Something went wrong ¯\\_(ツ)\_/¯" + "\nMath is hard, yo";
|
||||
try
|
||||
{
|
||||
var expr = new Expression(expression);
|
||||
result = expr.calculate().ToString();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Config.Log.Warn(e, "Math failed");
|
||||
}
|
||||
await ctx.RespondAsync(result).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("roll")]
|
||||
[Description("Generates a random number between 1 and maxValue. Can also roll dices like `2d6`. Default is 1d6")]
|
||||
public async Task Roll(CommandContext ctx, [Description("Some positive natural number")] int maxValue = 6, [Description("Optional text"), RemainingText] string comment = null)
|
||||
|
@ -119,6 +119,7 @@ namespace CompatBot
|
||||
commands.RegisterCommands<Invites>();
|
||||
commands.RegisterCommands<Moderation>();
|
||||
commands.RegisterCommands<Ird>();
|
||||
commands.RegisterCommands<BotMath>();
|
||||
|
||||
client.Ready += async r =>
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user