Add in-memory raffle system
This commit is contained in:
parent
9cceace905
commit
bda79ba38a
149
ChaosBot/Discord/Modules/RaffleSystem.cs
Normal file
149
ChaosBot/Discord/Modules/RaffleSystem.cs
Normal file
@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NLog;
|
||||
|
||||
namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
|
||||
public class RaffleSystem : ModuleBase
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
|
||||
private static Dictionary<ulong, int> _currentPot = new Dictionary<ulong, int>();
|
||||
|
||||
[Command("raffle")]
|
||||
[RequireUserPermission(GuildPermission.ManageGuild)]
|
||||
public async Task RaffleInfo() {
|
||||
try
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
var embed = new EmbedBuilder();
|
||||
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix");
|
||||
|
||||
embed.WithColor(new Color(255, 255,0));
|
||||
embed.Title = $"Raffle system";
|
||||
sb.AppendLine($"{Context.User.Mention} has requested raffle information.");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"Usage:");
|
||||
sb.AppendLine($"{prefix}raffle add <discord mention> <amount>");
|
||||
sb.AppendLine($"{prefix}raffle pick");
|
||||
sb.AppendLine($"{prefix}raffle clear");
|
||||
|
||||
/*
|
||||
* 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($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
}
|
||||
|
||||
[Command("raffle")]
|
||||
[RequireUserPermission(GuildPermission.ManageGuild)]
|
||||
public async Task RaffleCommand(string action, string user = null, int amount = 0) {
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
var embed = new EmbedBuilder();
|
||||
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix");
|
||||
|
||||
embed.WithColor(new Color(255, 255,0));
|
||||
embed.Title = $"Raffle system";
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case "add":
|
||||
AddRaffle(sb, user, amount);
|
||||
break;
|
||||
case "pick":
|
||||
PickRaffle(sb);
|
||||
break;
|
||||
case "clear":
|
||||
ClearRaffle(sb);
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
}
|
||||
|
||||
private void AddRaffle(StringBuilder sb, string user, int amount)
|
||||
{
|
||||
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4));
|
||||
int currentAmount;
|
||||
if (_currentPot.TryGetValue(userId, out currentAmount))
|
||||
{
|
||||
_currentPot.Remove(userId);
|
||||
_currentPot.Add(userId, currentAmount + amount);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentPot.Add(userId, amount);
|
||||
}
|
||||
|
||||
sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>.");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"<@{userId}> now has {_currentPot.GetValueOrDefault(userId)} rafflepoints!");
|
||||
}
|
||||
|
||||
private void PickRaffle(StringBuilder sb)
|
||||
{
|
||||
int totalRaffle = _currentPot.Values.Sum();
|
||||
|
||||
if (totalRaffle == 0)
|
||||
{
|
||||
sb.Append("No one has any rafflepoints.");
|
||||
return;
|
||||
}
|
||||
|
||||
int winner = new Random().Next(totalRaffle);
|
||||
foreach (ulong userId in _currentPot.Keys)
|
||||
{
|
||||
int userValue = _currentPot.GetValueOrDefault(userId);
|
||||
if (winner <= userValue)
|
||||
{
|
||||
sb.Append($"<@{userId}> has won the raffle!");
|
||||
ClearRaffle(sb, true);
|
||||
return;
|
||||
}
|
||||
|
||||
winner -= userValue;
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearRaffle(StringBuilder sb, bool noOutput = false)
|
||||
{
|
||||
_currentPot = new Dictionary<ulong, int>();
|
||||
|
||||
if (noOutput) return;
|
||||
|
||||
sb.AppendLine($"{Context.User.Mention} has cleared all rafflepoints");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user