2017-09-15 00:01:00 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Modules
|
|
|
|
|
{
|
|
|
|
|
public class Dice : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly Random rnd;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
public Dice(Random RandomClient)
|
|
|
|
|
{
|
|
|
|
|
rnd = RandomClient;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("dice", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Roll a dice.")]
|
|
|
|
|
public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d6")
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
var dice = diceType.Split("d");
|
2017-09-18 23:31:11 +02:00
|
|
|
|
var maxAndMod = dice[1].Split("+");
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
if (dice.Length != 2
|
|
|
|
|
|| !int.TryParse(dice[0], out int times)
|
2017-09-18 23:31:11 +02:00
|
|
|
|
|| !int.TryParse(maxAndMod[0], out int max))
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-18 23:31:11 +02:00
|
|
|
|
await ReplyAsync("That is not a valid dice, examples are: 1d20, 1d6, 2d10, 5d12, 1d20+5, 1d6+2, etc...");
|
2017-09-15 00:01:00 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-18 23:31:11 +02:00
|
|
|
|
|
|
|
|
|
int modifier = 0;
|
|
|
|
|
if (maxAndMod.Length == 2 && !int.TryParse(maxAndMod[1], out modifier) || maxAndMod.Length > 2)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("That is not a valid dice, examples are: 1d20, 1d6, 2d10, 5d12, 1d20+5, 1d6+2, etc...");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
if (times > 10 && !(times < 0))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("You can only roll between 1 and 10 dices");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-18 23:31:11 +02:00
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
if (max > 100 && !(max < 1))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("The dice must have between 1 and 100 sides");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-18 23:31:11 +02:00
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
var eb = new EmbedBuilder();
|
|
|
|
|
eb.WithAuthor(new EmbedAuthorBuilder()
|
|
|
|
|
.WithIconUrl(Context.User.GetAvatarUrl())
|
|
|
|
|
.WithName(Context.User.Username));
|
|
|
|
|
eb.WithColor(new Color(133, 189, 219));
|
|
|
|
|
eb.Title = $":game_die: Dice Roll - Type {diceType} :game_die:";
|
2017-09-18 23:31:11 +02:00
|
|
|
|
|
|
|
|
|
var total = 0;
|
2017-09-15 00:01:00 +02:00
|
|
|
|
for (var i = 0; i < times; i++)
|
2017-09-18 23:31:11 +02:00
|
|
|
|
{
|
|
|
|
|
var roll = rnd.Next(1, max);
|
|
|
|
|
eb.AddInlineField($"Dice {i + 1}", roll);
|
|
|
|
|
total = total + roll;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:53:59 +02:00
|
|
|
|
eb.AddField("Total", modifier == 0 ? $"{total}" : $"{total + modifier} ({total} +{modifier})");
|
2017-09-15 00:01:00 +02:00
|
|
|
|
await ReplyAsync("", false, eb.Build());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-19 18:53:59 +02:00
|
|
|
|
}
|