From 0fdcbd0968135ece889ea94171a689ffc4ffbd9d Mon Sep 17 00:00:00 2001 From: Sean Stoves Date: Sun, 9 Aug 2020 11:01:19 -0400 Subject: [PATCH] Adding Welcome and Goodbye --- ChaosBot/Discord/Services/CommandHandler.cs | 122 +++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index 0d0f52f..b940d98 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -1,12 +1,15 @@ using System; +using System.Linq; using System.Reflection; using System.Threading.Tasks; +using ChaosBot.Discord.Modules.User; +using ChaosBot.Models; using ChaosBot.Repositories; using Microsoft.Extensions.DependencyInjection; using Discord; using Discord.Commands; using Discord.WebSocket; -using Microsoft.Extensions.Configuration; +using Microsoft.EntityFrameworkCore; using NLog; namespace ChaosBot.Discord.Services @@ -29,6 +32,10 @@ namespace ChaosBot.Discord.Services _commands.CommandExecuted += CommandExecutedAsync; _client.MessageReceived += MessageReceivedAsync; + + _client.UserJoined += AnnounceJoinedUser; + + _client.UserLeft += AnnounceLeftUser; } catch (Exception ex) { @@ -74,6 +81,119 @@ namespace ChaosBot.Discord.Services } } + public async Task AnnounceJoinedUser(SocketGuildUser user) + { + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + Experience newUser = new Experience(); + newUser.Amount = 0; + newUser.DiscordGuildId = user.Guild.Id; + newUser.DiscordUserId = user.Id; + newUser.LastUpdated = DateTime.Now.Subtract(TimeSpan.FromMinutes(1)); + newUser.Level = 1; + + await dbContext.ExperiencePoints.Upsert(newUser) + .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); + } + + string ConfigSet = ConfigurationRepository.GetValue("LevelUp:Channel", user.Guild.Id, "false"); + if (ConfigSet != "false") + { + ulong channelId = Convert.ToUInt64(ConfigSet.Substring(2, ConfigSet.Length - 3)); + + await user.Guild.GetTextChannel(channelId).SendMessageAsync( + $"Welcome {user.Mention}!"); + } + else + { + await user.Guild.SystemChannel.SendMessageAsync($"Welcome {user.Mention}!"); + } + + + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task AnnounceLeftUser(SocketGuildUser user) + { + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxUser = dbContext.ExperiencePoints; + IQueryable usrXp = ctxUser + .Where(p => p.DiscordGuildId.Equals(user.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(user.Id)); + + if (usrXp.Any()) + { + dbContext.ExperiencePoints.Remove(usrXp.First()); + await dbContext.SaveChangesAsync(); + } + + IQueryable ctxUserLS = dbContext.LodestoneCharacter; + IQueryable userChar = ctxUserLS + .Where(p => p.DiscordGuildId.Equals(user.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(user.Id)); + + if (userChar.Any()) + { + dbContext.LodestoneCharacter.Remove(userChar.First()); + await dbContext.SaveChangesAsync(); + } + + IQueryable ctxRaffles = dbContext.Raffles; + + IQueryable ctxRaffleDetail = ctxRaffles + .Where(p => p.DiscordGuildId.Equals(user.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(user.Id)); + + int cur = ctxRaffleDetail.Count(); + + if (cur != 0) + { + dbContext.Raffles.RemoveRange(ctxRaffleDetail); + await dbContext.SaveChangesAsync(); + } + + Point usrPoint = new Point(); + + usrPoint.Amount = 0; + usrPoint.DiscordGuildId = user.Guild.Id; + usrPoint.DiscordUserId = user.Id; + + await dbContext.Points.Upsert(usrPoint) + .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); + } + + string ConfigSet = ConfigurationRepository.GetValue("LevelUp:Channel", user.Guild.Id, "false"); + if (ConfigSet != "false") + { + ulong channelId = Convert.ToUInt64(ConfigSet.Substring(2, ConfigSet.Length - 3)); + + await user.Guild.GetTextChannel(channelId).SendMessageAsync( + $"Goodbye {user.Mention}!"); + } + else + { + await user.Guild.SystemChannel.SendMessageAsync($"Goodbye {user.Mention}!"); + } + + + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + public async Task CommandExecutedAsync(Optional command, ICommandContext context, IResult result) { try