Merge branch 'develop' into 'master'

Updating Experience Levelup Messages to send to specified channel.

See merge request discord-bots/chaosbot!7
This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-09 00:46:59 -04:00
commit a492ecd2ee
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) ||
message.HasStringPrefix(prefix, ref argPos)))
{
ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel);
ExperienceHandler.addXP(context);
return;
}
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);
}

View File

@ -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<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> 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<ulong> checkLevel(Experience usrExperience, ISocketMessageChannel Channel)
public static async Task<ulong> 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<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)