344 lines
14 KiB
C#
344 lines
14 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ChaosBot.Discord.PreConditions;
|
|
using ChaosBot.Models;
|
|
using Discord;
|
|
using Discord.Commands;
|
|
using NLog;
|
|
using ChaosBot.Repositories;
|
|
using ChaosBot.Services;
|
|
|
|
namespace ChaosBot.Discord.Modules.User
|
|
{
|
|
public class RaffleCmd : ModuleBase
|
|
{
|
|
private static ILogger _logger = Program.Logger;
|
|
|
|
[Command("raffle")]
|
|
[CheckCommandPerm("User")]
|
|
public async Task RaffleCommand(string cmd = "total", string userMention = null, int Amount = 0)
|
|
{
|
|
try
|
|
{
|
|
if ((cmd.ToLower() == "add") || (cmd.ToLower() == "+") || (cmd.ToLower() == "give"))
|
|
{
|
|
if((Amount != 0) && (await CheckPermissions.CheckPerms(Context, "raffle.add", "Admin")))
|
|
{
|
|
await AddRaffle(userMention, Amount, true);
|
|
}
|
|
else
|
|
{
|
|
await RaffleHelp();
|
|
}
|
|
}
|
|
else if (cmd.ToLower() == "pick")
|
|
{
|
|
if(await CheckPermissions.CheckPerms(Context, "raffle.pick", "Admin"))
|
|
{
|
|
await PickRaffle();
|
|
}
|
|
else
|
|
{
|
|
await RaffleHelp();
|
|
}
|
|
}
|
|
else if ((cmd.ToLower() == "remove") || (cmd.ToLower() == "rem") || (cmd.ToLower() == "take") || (cmd.ToLower() == "-"))
|
|
{
|
|
if((Amount != 0) && (await CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin")))
|
|
{
|
|
await RemRaffle(userMention, Amount);
|
|
}
|
|
else
|
|
{
|
|
await RaffleHelp();
|
|
}
|
|
}
|
|
else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del"))
|
|
{
|
|
if((Amount == 0) && (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin")))
|
|
{
|
|
await DelRaffle(userMention);
|
|
}
|
|
else
|
|
{
|
|
await RaffleHelp();
|
|
}
|
|
}
|
|
else if ((cmd.ToLower() == "=") || (cmd.ToLower() == "info") || (cmd.ToLower() == "total") || (cmd == null))
|
|
{
|
|
await TotalRaffle();
|
|
}
|
|
else if ((cmd.ToLower() == "clear") && (await CheckPermissions.CheckPerms(Context, "points.clear", "Admin")))
|
|
{
|
|
await ClearRaffle(userMention);
|
|
}
|
|
else
|
|
{
|
|
await RaffleHelp();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
public async Task RaffleHelp()
|
|
{
|
|
try
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
|
|
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($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle info");
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle help");
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle buy <amount>");
|
|
sb.AppendLine();
|
|
sb.AppendLine("Moderation commands:");
|
|
if(await CheckPermissions.CheckPerms(Context, "raffle.add", "Admin"))
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle add <discord mention> <amount>");
|
|
if(await CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin"))
|
|
{
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle remove <discord mention> <amount>");
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle delete <discord mention>");
|
|
}
|
|
/*
|
|
* 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}]>.");
|
|
}
|
|
}
|
|
|
|
public async Task TotalRaffle()
|
|
{
|
|
int cur = 0;
|
|
|
|
try
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
cur = ctxRaffles
|
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id))
|
|
.Where(p => p.DiscordUserId.Equals(Context.User.Id)).Count();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
|
|
|
|
await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false);
|
|
|
|
}
|
|
|
|
public async Task AddRaffle(string userMention = null, int Amount = 0, bool admin = false)
|
|
{
|
|
int cur = 0;
|
|
try
|
|
{
|
|
if (admin)
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
for (int i = 0; i < Amount; i++)
|
|
{
|
|
Raffle usrRaff = new Raffle();
|
|
|
|
usrRaff.DiscordGuildId = Context.Guild.Id;
|
|
usrRaff.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
|
|
|
|
dbContext.Raffles.Add(usrRaff);
|
|
}
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
cur = ctxRaffles
|
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id))
|
|
.Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).Count();
|
|
}
|
|
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention} has added {Amount} tickets to <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> for a total of {cur} tickets.",
|
|
false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
public async Task RemRaffle(string userMention = null, int Amount = 0)
|
|
{
|
|
int cur = 0;
|
|
try
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
IQueryable<Raffle> ctxRaffleDetail = ctxRaffles
|
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id))
|
|
.Where(p => p.DiscordUserId.Equals(
|
|
Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))));
|
|
|
|
cur = ctxRaffleDetail.Count();
|
|
|
|
if (cur < Amount)
|
|
Amount = cur;
|
|
|
|
if (cur != 0)
|
|
{
|
|
for (int i = 0; i < Amount; i++)
|
|
{
|
|
Raffle usrRaff = ctxRaffleDetail.First();
|
|
dbContext.Raffles.Remove(usrRaff);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention} has taken {Amount} points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur-Amount} points.",
|
|
false);
|
|
}
|
|
|
|
public async Task DelRaffle(string userMention = null)
|
|
{
|
|
try
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
IQueryable<Raffle> ctxRaffleDetail = ctxRaffles
|
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id))
|
|
.Where(p => p.DiscordUserId.Equals(
|
|
Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))));
|
|
|
|
int cur = ctxRaffleDetail.Count();
|
|
|
|
if (cur != 0)
|
|
{
|
|
dbContext.Raffles.RemoveRange(ctxRaffleDetail);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.",
|
|
false);
|
|
}
|
|
|
|
public async Task ClearRaffle(string confirm = null)
|
|
{
|
|
try
|
|
{
|
|
if(confirm == "confirm")
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
IQueryable<Raffle> ctxRaffleDetail = ctxRaffles
|
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id));
|
|
|
|
int cur = ctxRaffleDetail.Count();
|
|
|
|
if (cur != 0)
|
|
{
|
|
dbContext.Raffles.RemoveRange(ctxRaffleDetail);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention} has removed all tickets.",
|
|
false);
|
|
}
|
|
else
|
|
{
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention}, if you wish to clear ALL tickets, please send the below command.```{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle clear confirm```",
|
|
false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
public async Task PickRaffle()
|
|
{
|
|
try
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
|
|
|
|
IQueryable<Raffle> ctxRaffleDetail = ctxRaffles.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id));
|
|
|
|
var ctxRaffleDetails = ctxRaffles.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).ToList();
|
|
|
|
if (ctxRaffleDetails.Any())
|
|
{
|
|
var count = new Random().Next(0, ctxRaffleDetails.Count - 1);
|
|
Raffle WinnerTicket = ctxRaffleDetail.Where(r => r.Id == ctxRaffleDetails[count].Id).First();
|
|
|
|
dbContext.Raffles.Remove(WinnerTicket);
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
await ReplyAsync(
|
|
$"{Context.User.Mention} drawn a ticket, <@{WinnerTicket.DiscordUserId}> has won the raffle!",
|
|
false);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
}
|
|
} |