Finishing Raffle Code, Raffle Pick
This commit is contained in:
parent
44f04f9555
commit
ee74aba169
302
ChaosBot/Discord/Modules/User/RaffleCmd.cs
Normal file
302
ChaosBot/Discord/Modules/User/RaffleCmd.cs
Normal file
@ -0,0 +1,302 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
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;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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 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}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,156 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Discord;
|
|
||||||
using Discord.Commands;
|
|
||||||
using NLog;
|
|
||||||
using ChaosBot.Models;
|
|
||||||
using ChaosBot.Repositories;
|
|
||||||
|
|
||||||
namespace ChaosBot.Discord.Modules
|
|
||||||
{
|
|
||||||
|
|
||||||
public class RaffleSystem : ModuleBase
|
|
||||||
{
|
|
||||||
private static ILogger _logger = Program.Logger;
|
|
||||||
|
|
||||||
[Command("raffle")]
|
|
||||||
public async Task RaffleStatus()
|
|
||||||
{
|
|
||||||
long userAmount = RaffleRepository.Count(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id));
|
|
||||||
long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id));
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine($"{Context.User.Mention}, you have {userAmount} rafflepoints.");
|
|
||||||
sb.AppendLine($"There is a total of {totalAmount} rafflepoints.");
|
|
||||||
|
|
||||||
eb.Title = "Raffle System";
|
|
||||||
eb.Description = sb.ToString();
|
|
||||||
|
|
||||||
await ReplyAsync(null, false, eb.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("raffle help")]
|
|
||||||
[Alias("raffle ?")]
|
|
||||||
public async Task RaffleInfo()
|
|
||||||
{
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
string prefix = ConfigurationRepository.GetValue("Discord.Prefix", Context.Guild.Id, "!");
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
eb.Title = "Raffle System";
|
|
||||||
eb.Description = sb.ToString();
|
|
||||||
|
|
||||||
await ReplyAsync(null, false, eb.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("raffle add")]
|
|
||||||
public async Task RaffleAdd(string userMention, int amount)
|
|
||||||
{
|
|
||||||
IUser user = await Context.Channel.GetUserAsync(Convert.ToUInt64(userMention.Substring(3, userMention.Length-4)));
|
|
||||||
|
|
||||||
long userAmount = RaffleRepository.Count(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id));
|
|
||||||
|
|
||||||
List<Raffle> raffleList = new List<Raffle>();
|
|
||||||
for (int i = 0; i < amount; i++)
|
|
||||||
raffleList.Add(new Raffle(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id)));
|
|
||||||
RaffleRepository.MassInsert(raffleList);
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to {user.Mention}");
|
|
||||||
sb.AppendLine($"{user.Mention} now has {userAmount + amount} rafflepoints!");
|
|
||||||
|
|
||||||
eb.Title = "Raffle System";
|
|
||||||
eb.Description = sb.ToString();
|
|
||||||
|
|
||||||
await ReplyAsync(null, false, eb.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("raffle pick")]
|
|
||||||
public async Task RafflePick()
|
|
||||||
{
|
|
||||||
Raffle winningRaffle = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id));
|
|
||||||
if (winningRaffle.id == null) return; // Shouldn't be needed, but it suppresses the warning
|
|
||||||
RaffleRepository.Delete((long)winningRaffle.id);
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine($"<@{winningRaffle.userId}> has won the raffle!");
|
|
||||||
|
|
||||||
eb.Title = "Raffle System";
|
|
||||||
eb.Description = sb.ToString();
|
|
||||||
|
|
||||||
await ReplyAsync(null, false, eb.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("raffle clear")]
|
|
||||||
public async Task RaffleClear()
|
|
||||||
{
|
|
||||||
long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id));
|
|
||||||
RaffleRepository.ClearRaffle(Convert.ToInt64(Context.Guild.Id));
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine($"{Context.User.Mention} has cleared all {totalAmount} rafflepoints.");
|
|
||||||
|
|
||||||
eb.Title = "Raffle System";
|
|
||||||
eb.Description = sb.ToString();
|
|
||||||
|
|
||||||
await ReplyAsync(null, false, eb.Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Command("raffle buy")]
|
|
||||||
public async Task RaffleCommandBuy(int amount = 1)
|
|
||||||
{
|
|
||||||
int cost = Convert.ToInt32(ConfigurationRepository.GetValue<string>($"Raffle:Cost", Context.Guild.Id)) * amount;
|
|
||||||
long curPoints = PointsRepository.Total(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id));
|
|
||||||
int curRaffle = RaffleRepository.Count(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id));
|
|
||||||
int maxRaffle = Convert.ToInt32(ConfigurationRepository.GetValue<string>($"Raffle:Max", Context.Guild.Id));
|
|
||||||
int newRaffle = curRaffle + amount;
|
|
||||||
|
|
||||||
Console.WriteLine($"curPoints: {curPoints}, curRaffle: {curRaffle}, maxRaffle: {maxRaffle}, newRaffle: {newRaffle}");
|
|
||||||
|
|
||||||
if (amount <= maxRaffle && newRaffle <= maxRaffle)
|
|
||||||
{
|
|
||||||
if (curPoints > cost)
|
|
||||||
{
|
|
||||||
long newPoints = PointsRepository.Remove(Convert.ToInt64(Context.User.Id), cost, Convert.ToInt64(Context.Guild.Id));
|
|
||||||
|
|
||||||
List<Raffle> raffleList = new List<Raffle>();
|
|
||||||
for (int i = 0; i < amount; i++)
|
|
||||||
raffleList.Add(new Raffle(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id)));
|
|
||||||
RaffleRepository.MassInsert(raffleList);
|
|
||||||
|
|
||||||
await ReplyAsync($"You have spent {cost} points and bought {amount} tickets. You have {newPoints} left.", false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
await ReplyAsync($"That will cost {cost} points, you only have {curPoints}.", false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await ReplyAsync(
|
|
||||||
$"You cannot buy more then {ConfigurationRepository.GetValue<string>($"Raffle:Max", Context.Guild.Id)} tickets.", false);
|
|
||||||
_logger.Warn($"{Context.User.Username} has bought {amount} tickets!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user