diff --git a/ChaosBot/Discord/Modules/User/RaffleCmd.cs b/ChaosBot/Discord/Modules/User/RaffleCmd.cs index 0662647..f4d9dc1 100644 --- a/ChaosBot/Discord/Modules/User/RaffleCmd.cs +++ b/ChaosBot/Discord/Modules/User/RaffleCmd.cs @@ -58,7 +58,7 @@ namespace ChaosBot.Discord.Modules.User } else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del")) { - if((Amount == 0) && (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) + if((Amount == 0) && (await CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin"))) { await DelRaffle(userMention); } @@ -71,6 +71,10 @@ namespace ChaosBot.Discord.Modules.User { await TotalRaffle(); } + else if ((cmd.ToLower() == "clear") && (await CheckPermissions.CheckPerms(Context, "raffle.clear", "Admin"))) + { + await ClearRaffle(userMention); + } else { await RaffleHelp(); @@ -146,7 +150,7 @@ namespace ChaosBot.Discord.Modules.User } - await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false); + await ReplyAsync($"{Context.User.Mention}, you have {cur} raffle tickets.", false); } @@ -227,7 +231,7 @@ namespace ChaosBot.Discord.Modules.User } 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.", + $"{Context.User.Mention} has taken {Amount} raffle tickets from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur-Amount} raffle tickets.", false); } @@ -260,10 +264,50 @@ namespace ChaosBot.Discord.Modules.User } await ReplyAsync( - $"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.", + $"{Context.User.Mention} has removed all raffle tickets 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 ctxRaffles = dbContext.Raffles; + + IQueryable 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("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 diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index d00c5a3..d5b292b 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -59,12 +59,12 @@ namespace ChaosBot.Discord.Services if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(prefix, ref argPos))) { - ExperienceHandler.addXP(context.Guild.Id, context.User.Id); + ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); return; } - + if(Convert.ToBoolean(ConfigurationRepository.GetValue("Experience:Commands", context.Guild.Id, "false"))) - ExperienceHandler.addXP(context.Guild.Id, context.User.Id); + ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); await _commands.ExecuteAsync(context, argPos, _services); } diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index 9b74856..941f53b 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -1,8 +1,10 @@ using System; using System.Linq; using System.Reflection; +using System.Threading.Tasks; using ChaosBot.Models; using ChaosBot.Repositories; +using Discord.WebSocket; using Microsoft.EntityFrameworkCore; using NLog; @@ -12,7 +14,7 @@ namespace ChaosBot.Discord.Services { private static readonly ILogger _logger = Program.Logger; - public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId) + public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId, ISocketMessageChannel Channel) { try { @@ -27,18 +29,31 @@ namespace ChaosBot.Discord.Services if (usrXp.Any()) { usrNewXp = usrXp.First(); - usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(ConfigurationRepository.GetValue("Experience:PerMsg", DiscordGuildId, "0")); - } + + if(DateTime.Now >= usrNewXp.LastUpdated.AddMinutes(1)) + { + usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26)); + + usrNewXp.DiscordGuildId = DiscordGuildId; + usrNewXp.DiscordUserId = DiscordUserId; + usrNewXp.LastUpdated = DateTime.Now; + usrNewXp.Level = await checkLevel(usrNewXp, Channel); + + await dbContext.ExperiencePoints.Upsert(usrNewXp) + .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); + } } else { usrNewXp = new Experience(); - usrNewXp.Amount = Convert.ToUInt64(ConfigurationRepository.GetValue("Experience:PerMsg", DiscordGuildId, "0")); + usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26)); + + usrNewXp.DiscordGuildId = DiscordGuildId; + usrNewXp.DiscordUserId = DiscordUserId; + usrNewXp.LastUpdated = DateTime.Now; + + await dbContext.ExperiencePoints.Upsert(usrNewXp) + .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); } - usrNewXp.DiscordGuildId = DiscordGuildId; - usrNewXp.DiscordUserId = DiscordUserId; - - await dbContext.ExperiencePoints.Upsert(usrNewXp) - .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); } } catch (Exception ex) @@ -47,5 +62,28 @@ namespace ChaosBot.Discord.Services $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); } } + + public static async Task checkLevel(Experience usrExperience, ISocketMessageChannel Channel) + { + ulong curLevel = 0; + + try + { + ulong nextLevelXP = 6 * (usrExperience.Level / 2) + 48 * usrExperience.Level + 123; + + if (usrExperience.Amount >= nextLevelXP) + { + curLevel = usrExperience.Level + 1; + await Channel.SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}. Congratulations!"); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + return curLevel; + } } } \ No newline at end of file diff --git a/ChaosBot/Migrations/20200804213001_ExperiencePoints.cs b/ChaosBot/Migrations/20200804213001_ExperiencePoints.cs index 6abf7c6..1ef77de 100644 --- a/ChaosBot/Migrations/20200804213001_ExperiencePoints.cs +++ b/ChaosBot/Migrations/20200804213001_ExperiencePoints.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore.Metadata; +using System; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; namespace ChaosBot.Migrations @@ -15,7 +16,9 @@ namespace ChaosBot.Migrations { DiscordGuildId = table.Column(nullable: false), DiscordUserId = table.Column(nullable: false), - Amount = table.Column(nullable: false, defaultValue: 0) + Amount = table.Column(nullable: false, defaultValue: 0), + Level = table.Column(nullable: false, defaultValue: 0), + lastUpdated = table.Column(nullable:false, defaultValue: "0000-00-00 00:00:00") }, constraints: table => { diff --git a/ChaosBot/Models/Experience.cs b/ChaosBot/Models/Experience.cs index f46148e..933de2d 100644 --- a/ChaosBot/Models/Experience.cs +++ b/ChaosBot/Models/Experience.cs @@ -1,3 +1,4 @@ +using System; using System.ComponentModel.DataAnnotations; namespace ChaosBot.Models @@ -10,6 +11,8 @@ namespace ChaosBot.Models [Required] public ulong DiscordGuildId { get; set; } public ulong Amount { get; set; } + public DateTime LastUpdated { get; set; } + public ulong Level { get; set; } } #endregion } \ No newline at end of file