Fix Rafflesystem, take out kinks from other systems
This commit is contained in:
parent
1180aa2bb4
commit
707e0b1bc1
@ -125,11 +125,15 @@ namespace ChaosBot.Database
|
||||
public int Count()
|
||||
{
|
||||
QueryBuilderRaw query = new QueryBuilderRaw();
|
||||
long amount = (long)query
|
||||
query
|
||||
.Select()
|
||||
.Append("COUNT(*) as count")
|
||||
.FromTable(_table)
|
||||
.Run().Rows[0]["count"];
|
||||
.FromTable(_table);
|
||||
|
||||
if (_whereConditions.Count > 0)
|
||||
query.Where(_whereConditions);
|
||||
|
||||
long amount = (long)query.Run().Rows[0]["count"];
|
||||
return Convert.ToInt32(amount);
|
||||
}
|
||||
|
||||
@ -139,21 +143,29 @@ namespace ChaosBot.Database
|
||||
public bool Exists()
|
||||
{
|
||||
QueryBuilderRaw query = new QueryBuilderRaw();
|
||||
return (bool)query
|
||||
query
|
||||
.Select()
|
||||
.Append("COUNT(*)>0 as present")
|
||||
.FromTable(_table)
|
||||
.Run().Rows[0]["present"];
|
||||
.FromTable(_table);
|
||||
|
||||
if (_whereConditions.Count > 0)
|
||||
query.Where(_whereConditions);
|
||||
|
||||
return (bool)query.Run().Rows[0]["present"];
|
||||
}
|
||||
|
||||
public bool DoesNotExist()
|
||||
{
|
||||
QueryBuilderRaw query = new QueryBuilderRaw();
|
||||
return (bool)query
|
||||
query
|
||||
.Select()
|
||||
.Append("COUNT(*)=0 as notPresent")
|
||||
.FromTable(_table)
|
||||
.Run().Rows[0]["notPresent"];
|
||||
.FromTable(_table);
|
||||
|
||||
if (_whereConditions.Count > 0)
|
||||
query.Where(_whereConditions);
|
||||
|
||||
return (bool)query.Run().Rows[0]["notPresent"];
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@ -133,7 +133,7 @@ namespace ChaosBot.Database.Repository
|
||||
/// Delete a <c>Raffle</c> by id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public static void Delete(int id)
|
||||
public static void Delete(long id)
|
||||
{
|
||||
Raffle.Query().Where("id", id).Delete();
|
||||
}
|
||||
|
||||
@ -18,165 +18,105 @@ namespace ChaosBot.Discord.Modules
|
||||
|
||||
[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)
|
||||
public async Task RaffleInfo()
|
||||
{
|
||||
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}!");
|
||||
}
|
||||
}
|
||||
var eb = new EmbedBuilder();
|
||||
var sb = new StringBuilder();
|
||||
string prefix = ConfigurationRepository.GetValue<string>("Discord.Prefix");
|
||||
|
||||
[Command("raffle pick")]
|
||||
[RequireUserPermission(GuildPermission.ManageGuild)]
|
||||
public async Task RaffleCommandPick()
|
||||
{
|
||||
await RaffleCommandHelper("pick");
|
||||
}
|
||||
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");
|
||||
|
||||
[Command("raffle clear")]
|
||||
[RequireUserPermission(GuildPermission.ManageGuild)]
|
||||
public async Task RaffleCommandClear()
|
||||
{
|
||||
await RaffleCommandHelper("clear");
|
||||
eb.Title = "Raffle System";
|
||||
eb.Description = sb.ToString();
|
||||
|
||||
await ReplyAsync(null, false, eb.Build());
|
||||
}
|
||||
|
||||
[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) {
|
||||
try
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
var embed = new EmbedBuilder();
|
||||
string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
|
||||
[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)));
|
||||
|
||||
embed.WithColor(new Color(255, 255,0));
|
||||
embed.Title = $"Raffle system";
|
||||
long userAmount = RaffleRepository.Count(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id));
|
||||
|
||||
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;
|
||||
}
|
||||
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);
|
||||
|
||||
/*
|
||||
* Add the string to the Embed
|
||||
*/
|
||||
embed.Description = sb.ToString();
|
||||
var eb = new EmbedBuilder();
|
||||
var sb = new StringBuilder();
|
||||
|
||||
/*
|
||||
* Reply with the Embed created above
|
||||
*/
|
||||
await ReplyAsync(null, false, embed.Build());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error($"RaffleSystem.RaffleCommandHelper: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
List<Raffle> raffles = new List<Raffle>();
|
||||
var eb = new EmbedBuilder();
|
||||
var sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < amount; i++)
|
||||
raffles.Add(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
|
||||
sb.AppendLine($"<@{winningRaffle.userId}> has won the raffle!");
|
||||
|
||||
RaffleRepository.MassInsert(raffles);
|
||||
}
|
||||
else
|
||||
{
|
||||
RaffleRepository.Insert(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)));
|
||||
}
|
||||
eb.Title = "Raffle System";
|
||||
eb.Description = sb.ToString();
|
||||
|
||||
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!");
|
||||
await ReplyAsync(null, false, eb.Build());
|
||||
}
|
||||
|
||||
private void PickRaffle(StringBuilder sb)
|
||||
[Command("raffle clear")]
|
||||
public async Task RaffleClear()
|
||||
{
|
||||
Raffle winner = RaffleRepository.PickRandom(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));
|
||||
|
||||
long totalAmount = RaffleRepository.Count(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)
|
||||
{
|
||||
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4));
|
||||
sb.AppendLine($"{Context.User.Mention} has cleared all {totalAmount} rafflepoints.");
|
||||
|
||||
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.");
|
||||
eb.Title = "Raffle System";
|
||||
eb.Description = sb.ToString();
|
||||
|
||||
await ReplyAsync(null, false, eb.Build());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user