181 lines
6.7 KiB
C#
181 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ChaosBot.Database.Entity;
|
|
using Discord;
|
|
using Discord.Commands;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using ChaosBot.Database.Repository;
|
|
|
|
namespace ChaosBot.Discord.Modules
|
|
{
|
|
|
|
public class RaffleSystem : ModuleBase
|
|
{
|
|
private static Logger _logger = Program._logger;
|
|
|
|
[Command("raffle")]
|
|
[Alias("raffle info")]
|
|
[RequireUserPermission(GuildPermission.ManageGuild)]
|
|
public async Task RaffleCommandInfo() {
|
|
try
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
string prefix = ConfigurationRepository.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 status");
|
|
sb.AppendLine($"{prefix}raffle info");
|
|
sb.AppendLine();
|
|
sb.AppendLine("Moderation commands:");
|
|
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($"RaffleSystem.RaffleCommandInfo: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
[Command("raffle add")]
|
|
[RequireUserPermission(GuildPermission.ManageGuild)]
|
|
public async Task RaffleCommandAdd(string user, int amount = 1)
|
|
{
|
|
if (ConfigurationRepository.GetValue<int>($"Raffle:Max", Context.Guild.Id) >= amount)
|
|
await RaffleCommandHelper("add", user, amount);
|
|
else
|
|
{
|
|
await ReplyAsync(
|
|
$"You cannot give more then {ConfigurationRepository.GetValue<int>($"Raffle:Max", Context.Guild.Id).ToString()} tickets at a time", false);
|
|
_logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!");
|
|
}
|
|
}
|
|
|
|
[Command("raffle pick")]
|
|
[RequireUserPermission(GuildPermission.ManageGuild)]
|
|
public async Task RaffleCommandPick()
|
|
{
|
|
await RaffleCommandHelper("pick");
|
|
}
|
|
|
|
[Command("raffle clear")]
|
|
[RequireUserPermission(GuildPermission.ManageGuild)]
|
|
public async Task RaffleCommandClear()
|
|
{
|
|
await RaffleCommandHelper("clear");
|
|
}
|
|
|
|
[Command("raffle status")]
|
|
public async Task RaffleCommandStatus()
|
|
{
|
|
await RaffleCommandHelper("status", $"<@!{Context.User.Id}>");
|
|
}
|
|
|
|
private async Task RaffleCommandHelper(string action, string user = null, int amount = 0) {
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
string prefix = ConfigurationRepository.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;
|
|
case "status":
|
|
StatusRaffle(sb, user);
|
|
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($"RaffleSystem.RaffleCommandHelper: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
private void AddRaffle(StringBuilder sb, string user, int amount = 1)
|
|
{
|
|
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4));
|
|
|
|
if (amount > 1)
|
|
{
|
|
List<Raffle> raffles = new List<Raffle>();
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
raffles.Add(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
|
|
|
|
RaffleRepository.MassInsert(raffles);
|
|
}
|
|
else
|
|
{
|
|
RaffleRepository.Insert(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
|
|
}
|
|
|
|
sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>.");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))} rafflepoints!");
|
|
}
|
|
|
|
private void PickRaffle(StringBuilder sb)
|
|
{
|
|
Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id));
|
|
RaffleRepository.Delete(winner.id);
|
|
sb.Append($"<@{winner.userId}> has won the raffle!");
|
|
}
|
|
|
|
private void ClearRaffle(StringBuilder sb, bool noOutput = false)
|
|
{
|
|
int removed = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id));
|
|
|
|
RaffleRepository.ClearRaffle(Convert.ToInt64(Context.Guild.Id));
|
|
|
|
sb.AppendLine($"{Context.User.Mention} has cleared all {removed} rafflepoints");
|
|
}
|
|
|
|
private void StatusRaffle(StringBuilder sb, string user)
|
|
{
|
|
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4));
|
|
|
|
sb.AppendLine($"<@{userId}>, you have {RaffleRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))} rafflepoints.");
|
|
sb.AppendLine($"There is a total of {RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id))} rafflepoints.");
|
|
}
|
|
}
|
|
} |