Updating Experience Levelup Messages to send to specified channel.

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-09 00:46:10 -04:00
parent e7d10caf4e
commit 7169bafb1a
2 changed files with 21 additions and 12 deletions

View File

@ -59,12 +59,12 @@ namespace ChaosBot.Discord.Services
if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) ||
message.HasStringPrefix(prefix, ref argPos))) message.HasStringPrefix(prefix, ref argPos)))
{ {
ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel); ExperienceHandler.addXP(context);
return; return;
} }
if(Convert.ToBoolean(ConfigurationRepository.GetValue<string>("Experience:Commands", context.Guild.Id, "false"))) if(Convert.ToBoolean(ConfigurationRepository.GetValue<string>("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); await _commands.ExecuteAsync(context, argPos, _services);
} }

View File

@ -1,9 +1,11 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Channels;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Models; using ChaosBot.Models;
using ChaosBot.Repositories; using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NLog; using NLog;
@ -14,7 +16,7 @@ namespace ChaosBot.Discord.Services
{ {
private static readonly ILogger _logger = Program.Logger; 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 try
{ {
@ -22,8 +24,8 @@ namespace ChaosBot.Discord.Services
{ {
IQueryable<Experience> ctxUser = dbContext.ExperiencePoints; IQueryable<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> usrXp = ctxUser IQueryable<Experience> usrXp = ctxUser
.Where(p => p.DiscordGuildId.Equals(DiscordGuildId)) .Where(p => p.DiscordGuildId.Equals(context.Guild.Id))
.Where(p => p.DiscordUserId.Equals(DiscordUserId)); .Where(p => p.DiscordUserId.Equals(context.User.Id));
Experience usrNewXp; Experience usrNewXp;
if (usrXp.Any()) if (usrXp.Any())
@ -34,10 +36,10 @@ namespace ChaosBot.Discord.Services
{ {
usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26)); usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26));
usrNewXp.DiscordGuildId = DiscordGuildId; usrNewXp.DiscordGuildId = context.Guild.Id;
usrNewXp.DiscordUserId = DiscordUserId; usrNewXp.DiscordUserId = context.User.Id;
usrNewXp.LastUpdated = DateTime.Now; usrNewXp.LastUpdated = DateTime.Now;
usrNewXp.Level = await checkLevel(usrNewXp, Channel); usrNewXp.Level = await checkLevel(usrNewXp, context);
await dbContext.ExperiencePoints.Upsert(usrNewXp) await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync(); .On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
@ -47,8 +49,8 @@ namespace ChaosBot.Discord.Services
usrNewXp = new Experience(); usrNewXp = new Experience();
usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26)); usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26));
usrNewXp.DiscordGuildId = DiscordGuildId; usrNewXp.DiscordGuildId = context.Guild.Id;
usrNewXp.DiscordUserId = DiscordUserId; usrNewXp.DiscordUserId = context.User.Id;
usrNewXp.LastUpdated = DateTime.Now; usrNewXp.LastUpdated = DateTime.Now;
await dbContext.ExperiencePoints.Upsert(usrNewXp) await dbContext.ExperiencePoints.Upsert(usrNewXp)
@ -63,7 +65,7 @@ namespace ChaosBot.Discord.Services
} }
} }
public static async Task<ulong> checkLevel(Experience usrExperience, ISocketMessageChannel Channel) public static async Task<ulong> checkLevel(Experience usrExperience, SocketCommandContext context)
{ {
ulong curLevel = 0; ulong curLevel = 0;
@ -74,7 +76,14 @@ namespace ChaosBot.Discord.Services
if (usrExperience.Amount >= nextLevelXP) if (usrExperience.Amount >= nextLevelXP)
{ {
curLevel = usrExperience.Level + 1; curLevel = usrExperience.Level + 1;
await Channel.SendMessageAsync($"Congratulations <@{usrExperience.DiscordUserId}>, You have reached {curLevel}. Congratulations!"); string ConfigSet = ConfigurationRepository.GetValue<string>("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) catch (Exception ex)