diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index d5b292b..0d0f52f 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, context.Channel); + ExperienceHandler.addXP(context); return; } if(Convert.ToBoolean(ConfigurationRepository.GetValue("Experience:Commands", context.Guild.Id, "false"))) - ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); + ExperienceHandler.addXP(context); await _commands.ExecuteAsync(context, argPos, _services); } diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index 941f53b..9efa6a1 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -1,9 +1,11 @@ using System; using System.Linq; using System.Reflection; +using System.Threading.Channels; using System.Threading.Tasks; using ChaosBot.Models; using ChaosBot.Repositories; +using Discord.Commands; using Discord.WebSocket; using Microsoft.EntityFrameworkCore; using NLog; @@ -14,7 +16,7 @@ namespace ChaosBot.Discord.Services { private static readonly ILogger _logger = Program.Logger; - public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId, ISocketMessageChannel Channel) + public static async void addXP(SocketCommandContext context) { try { @@ -22,8 +24,8 @@ namespace ChaosBot.Discord.Services { IQueryable ctxUser = dbContext.ExperiencePoints; IQueryable usrXp = ctxUser - .Where(p => p.DiscordGuildId.Equals(DiscordGuildId)) - .Where(p => p.DiscordUserId.Equals(DiscordUserId)); + .Where(p => p.DiscordGuildId.Equals(context.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(context.User.Id)); Experience usrNewXp; if (usrXp.Any()) @@ -34,10 +36,10 @@ namespace ChaosBot.Discord.Services { usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26)); - usrNewXp.DiscordGuildId = DiscordGuildId; - usrNewXp.DiscordUserId = DiscordUserId; + usrNewXp.DiscordGuildId = context.Guild.Id; + usrNewXp.DiscordUserId = context.User.Id; usrNewXp.LastUpdated = DateTime.Now; - usrNewXp.Level = await checkLevel(usrNewXp, Channel); + usrNewXp.Level = await checkLevel(usrNewXp, context); await dbContext.ExperiencePoints.Upsert(usrNewXp) .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); @@ -47,8 +49,8 @@ namespace ChaosBot.Discord.Services usrNewXp = new Experience(); usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26)); - usrNewXp.DiscordGuildId = DiscordGuildId; - usrNewXp.DiscordUserId = DiscordUserId; + usrNewXp.DiscordGuildId = context.Guild.Id; + usrNewXp.DiscordUserId = context.User.Id; usrNewXp.LastUpdated = DateTime.Now; await dbContext.ExperiencePoints.Upsert(usrNewXp) @@ -63,7 +65,7 @@ namespace ChaosBot.Discord.Services } } - public static async Task checkLevel(Experience usrExperience, ISocketMessageChannel Channel) + public static async Task checkLevel(Experience usrExperience, SocketCommandContext context) { ulong curLevel = 0; @@ -74,7 +76,14 @@ namespace ChaosBot.Discord.Services if (usrExperience.Amount >= nextLevelXP) { curLevel = usrExperience.Level + 1; - await Channel.SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}. Congratulations!"); + string ConfigSet = ConfigurationRepository.GetValue("LevelUp:Channel", usrExperience.DiscordGuildId, "false"); + if (ConfigSet != "false") + { + ulong channelId = Convert.ToUInt64(ConfigSet.Substring(2, ConfigSet.Length - 3)); + await context.Guild.GetTextChannel(channelId).SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}."); + } + else + await context.Channel.SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}. Congratulations!"); } } catch (Exception ex)