Adding Dice Commands
This commit is contained in:
parent
955ff55dd8
commit
16dec32abc
125
ChaosBot/Discord/Modules/User/Roll.cs
Normal file
125
ChaosBot/Discord/Modules/User/Roll.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user