From 16dec32abc2edb7bdc9d8bbef41a482619e64ae9 Mon Sep 17 00:00:00 2001 From: Sean Stoves Date: Wed, 5 Aug 2020 19:30:12 -0400 Subject: [PATCH] Adding Dice Commands --- ChaosBot/Discord/Modules/User/Roll.cs | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 ChaosBot/Discord/Modules/User/Roll.cs diff --git a/ChaosBot/Discord/Modules/User/Roll.cs b/ChaosBot/Discord/Modules/User/Roll.cs new file mode 100644 index 0000000..1a30f7e --- /dev/null +++ b/ChaosBot/Discord/Modules/User/Roll.cs @@ -0,0 +1,125 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using ChaosBot.Discord.PreConditions; +using Dice; +using Discord; +using Discord.Commands; +using Microsoft.VisualBasic; +using NLog; + +namespace ChaosBot.Discord.Modules.User +{ + public class Roll : ModuleBase + { + private static readonly ILogger _logger = Program.Logger; + + [Command("roll")] + [Alias("random", "dice")] + [CheckCommandPerm("User")] + public async Task DiceRoll(params string[] args) + { + try + { + string diceRecipe = Strings.Join(args, " "); + + var sb = new StringBuilder(); + var embed = new EmbedBuilder(); + + RollResult rollResult = Roller.Roll(diceRecipe); + + embed.WithColor(new Color(255, 255,0)); + // embed.Title = $"Dice Roll"; + sb.AppendLine($"{Context.User.Mention} :game_die:"); + sb.AppendLine($"Result: {diceRecipe} ({DiceOutput(rollResult)})"); + sb.AppendLine($"Total: {rollResult.Value}"); + + /* + * Add the string to the Embed + */ + embed.Description = sb.ToString(); + + /* + * Reply with the Embed created above + */ + await ReplyAsync(null, false, embed.Build()); + } + catch (Exception ex) + { + _logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + private string DiceOutput(RollResult rollResult) + { + if (rollResult.RollRoot == null) return ""; + StringBuilder sb = new StringBuilder(); + + foreach (DieResult dieResult in rollResult.RollRoot.Values) + { + string result = ParseDiceResult(dieResult); + + sb.Append(DieFormatting(dieResult.Flags)); + sb.Append(result); + sb.Append(DieFormatting(dieResult.Flags, true)); + } + + return sb.ToString(); + } + + private string ParseDiceResult(DieResult dieResult) + { + switch (dieResult.DieType) + { + case DieType.Normal: + case DieType.Literal: + return dieResult.Value.ToString(); + case DieType.Special: + switch (dieResult.SpecialDie) + { + case SpecialDie.Add: + return "+"; + case SpecialDie.Comma: + return ","; + case SpecialDie.Divide: + return "/"; + case SpecialDie.Multiply: + return "*"; + case SpecialDie.Negate: + return "~"; + case SpecialDie.Subtract: + return "-"; + case SpecialDie.Text: + return dieResult.Data; + case SpecialDie.CloseParen: + return ")"; + case SpecialDie.OpenParen: + return "("; + } + + break; + } + + return ""; + } + + private string DieFormatting(DieFlags flags, bool reverse=false) + { + StringBuilder sb = new StringBuilder(); + if ((flags & (DieFlags.Critical ^ DieFlags.Success ^ DieFlags.Failure ^ DieFlags.Fumble)) != 0) + sb.Append("**"); + + if ((flags & DieFlags.Dropped) != 0) + sb.Append("~~"); + + if ((flags & DieFlags.Extra) != 0) + sb.Append("*"); + + if (reverse) + return new string(sb.ToString().Reverse().ToArray()); + + return sb.ToString(); + } + } +} \ No newline at end of file