Fix Rafflesystem, take out kinks from other systems

This commit is contained in:
Daniel_I_Am 2020-06-09 16:54:34 +02:00
parent 1180aa2bb4
commit 707e0b1bc1
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 105 additions and 153 deletions

View File

@ -125,11 +125,15 @@ namespace ChaosBot.Database
public int Count() public int Count()
{ {
QueryBuilderRaw query = new QueryBuilderRaw(); QueryBuilderRaw query = new QueryBuilderRaw();
long amount = (long)query query
.Select() .Select()
.Append("COUNT(*) as count") .Append("COUNT(*) as count")
.FromTable(_table) .FromTable(_table);
.Run().Rows[0]["count"];
if (_whereConditions.Count > 0)
query.Where(_whereConditions);
long amount = (long)query.Run().Rows[0]["count"];
return Convert.ToInt32(amount); return Convert.ToInt32(amount);
} }
@ -139,21 +143,29 @@ namespace ChaosBot.Database
public bool Exists() public bool Exists()
{ {
QueryBuilderRaw query = new QueryBuilderRaw(); QueryBuilderRaw query = new QueryBuilderRaw();
return (bool)query query
.Select() .Select()
.Append("COUNT(*)>0 as present") .Append("COUNT(*)>0 as present")
.FromTable(_table) .FromTable(_table);
.Run().Rows[0]["present"];
if (_whereConditions.Count > 0)
query.Where(_whereConditions);
return (bool)query.Run().Rows[0]["present"];
} }
public bool DoesNotExist() public bool DoesNotExist()
{ {
QueryBuilderRaw query = new QueryBuilderRaw(); QueryBuilderRaw query = new QueryBuilderRaw();
return (bool)query query
.Select() .Select()
.Append("COUNT(*)=0 as notPresent") .Append("COUNT(*)=0 as notPresent")
.FromTable(_table) .FromTable(_table);
.Run().Rows[0]["notPresent"];
if (_whereConditions.Count > 0)
query.Where(_whereConditions);
return (bool)query.Run().Rows[0]["notPresent"];
} }
/* /*

View File

@ -133,7 +133,7 @@ namespace ChaosBot.Database.Repository
/// Delete a <c>Raffle</c> by id /// Delete a <c>Raffle</c> by id
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
public static void Delete(int id) public static void Delete(long id)
{ {
Raffle.Query().Where("id", id).Delete(); Raffle.Query().Where("id", id).Delete();
} }

View File

@ -18,165 +18,105 @@ namespace ChaosBot.Discord.Modules
[Command("raffle")] [Command("raffle")]
[Alias("raffle info")] [Alias("raffle info")]
[RequireUserPermission(GuildPermission.ManageGuild)] public async Task RaffleInfo()
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) var eb = new EmbedBuilder();
await RaffleCommandHelper("add", user, amount); var sb = new StringBuilder();
else string prefix = ConfigurationRepository.GetValue<string>("Discord.Prefix");
{
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")] sb.AppendLine($"{Context.User.Mention} has requested raffle information.");
[RequireUserPermission(GuildPermission.ManageGuild)] sb.AppendLine();
public async Task RaffleCommandPick() sb.AppendLine("Usage:");
{ sb.AppendLine($"{prefix}raffle status");
await RaffleCommandHelper("pick"); 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");
[Command("raffle clear")] eb.Title = "Raffle System";
[RequireUserPermission(GuildPermission.ManageGuild)] eb.Description = sb.ToString();
public async Task RaffleCommandClear()
{ await ReplyAsync(null, false, eb.Build());
await RaffleCommandHelper("clear");
} }
[Command("raffle status")] [Command("raffle status")]
public async Task RaffleCommandStatus() public async Task RaffleStatus()
{ {
await RaffleCommandHelper("status", $"<@!{Context.User.Id}>"); long userAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id), Convert.ToInt64(Context.User.Id));
long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id));
var eb = new EmbedBuilder();
var sb = new StringBuilder();
string prefix = ConfigurationRepository.GetValue<string>("Discord.Prefix");
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());
} }
private async Task RaffleCommandHelper(string action, string user = null, int amount = 0) { [Command("raffle add")]
try public async Task RaffleAdd(string userMention, int amount)
{ {
StringBuilder sb = new StringBuilder(); IUser user = await Context.Channel.GetUserAsync(Convert.ToUInt64(userMention.Substring(3, userMention.Length-4)));
var embed = new EmbedBuilder();
string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
embed.WithColor(new Color(255, 255,0)); long userAmount = RaffleRepository.Count(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id));
embed.Title = $"Raffle system";
switch (action) List<Raffle> raffleList = new List<Raffle>();
{ for (int i = 0; i < amount; i++)
case "add": raffleList.Add(new Raffle(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id)));
AddRaffle(sb, user, amount); RaffleRepository.MassInsert(raffleList);
break;
case "pick":
PickRaffle(sb);
break;
case "clear":
ClearRaffle(sb);
break;
case "status":
StatusRaffle(sb, user);
break;
}
/* var eb = new EmbedBuilder();
* Add the string to the Embed var sb = new StringBuilder();
*/
embed.Description = sb.ToString();
/* sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to {user.Mention}");
* Reply with the Embed created above sb.AppendLine($"{user.Mention} now has {userAmount + amount} rafflepoints!");
*/
await ReplyAsync(null, false, embed.Build()); eb.Title = "Raffle System";
} eb.Description = sb.ToString();
catch (Exception ex)
{ await ReplyAsync(null, false, eb.Build());
_logger.Error($"RaffleSystem.RaffleCommandHelper: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
} }
private void AddRaffle(StringBuilder sb, string user, int amount = 1) [Command("raffle pick")]
public async Task RafflePick()
{ {
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4)); 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);
if (amount > 1) var eb = new EmbedBuilder();
{ var sb = new StringBuilder();
List<Raffle> raffles = new List<Raffle>();
for (int i = 0; i < amount; i++) sb.AppendLine($"<@{winningRaffle.userId}> has won the raffle!");
raffles.Add(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
RaffleRepository.MassInsert(raffles); eb.Title = "Raffle System";
} eb.Description = sb.ToString();
else
{
RaffleRepository.Insert(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
}
sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>."); await ReplyAsync(null, false, eb.Build());
sb.AppendLine();
sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))} rafflepoints!");
} }
private void PickRaffle(StringBuilder sb) [Command("raffle clear")]
public async Task RaffleClear()
{ {
Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id)); long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id));
if (winner.id != null)
RaffleRepository.Delete((int)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)); RaffleRepository.ClearRaffle(Convert.ToInt64(Context.Guild.Id));
sb.AppendLine($"{Context.User.Mention} has cleared all {removed} rafflepoints"); var eb = new EmbedBuilder();
} var sb = new StringBuilder();
private void StatusRaffle(StringBuilder sb, string user) sb.AppendLine($"{Context.User.Mention} has cleared all {totalAmount} rafflepoints.");
{
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."); eb.Title = "Raffle System";
sb.AppendLine($"There is a total of {RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id))} rafflepoints."); eb.Description = sb.ToString();
await ReplyAsync(null, false, eb.Build());
} }
} }
} }