Adding Welcome and Goodbye

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-09 11:01:19 -04:00
parent 60845644bb
commit 0fdcbd0968

View File

@ -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<string>("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<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> 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<LodestoneCharacter> ctxUserLS = dbContext.LodestoneCharacter;
IQueryable<LodestoneCharacter> 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<Raffle> ctxRaffles = dbContext.Raffles;
IQueryable<Raffle> 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<string>("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<CommandInfo> command, ICommandContext context, IResult result)
{
try