Sending Channel to parsers for Reply on level Up, Adding Leveling Formula

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-08 23:54:27 -04:00
parent e8c6fcce76
commit e7d10caf4e
2 changed files with 30 additions and 4 deletions

View File

@ -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<string>("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);
}

View File

@ -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
{
@ -35,6 +37,7 @@ namespace ChaosBot.Discord.Services
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();
@ -59,5 +62,28 @@ namespace ChaosBot.Discord.Services
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
public static async Task<ulong> 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;
}
}
}