Merge branch 'develop' into 'master'

Develop

See merge request discord-bots/chaosbot!6
This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-08-08 23:54:59 -04:00
commit b062bd4669
5 changed files with 106 additions and 18 deletions

View File

@ -58,7 +58,7 @@ namespace ChaosBot.Discord.Modules.User
} }
else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del")) else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del"))
{ {
if((Amount == 0) && (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) if((Amount == 0) && (await CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin")))
{ {
await DelRaffle(userMention); await DelRaffle(userMention);
} }
@ -71,6 +71,10 @@ namespace ChaosBot.Discord.Modules.User
{ {
await TotalRaffle(); await TotalRaffle();
} }
else if ((cmd.ToLower() == "clear") && (await CheckPermissions.CheckPerms(Context, "raffle.clear", "Admin")))
{
await ClearRaffle(userMention);
}
else else
{ {
await RaffleHelp(); await RaffleHelp();
@ -146,7 +150,7 @@ namespace ChaosBot.Discord.Modules.User
} }
await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false); await ReplyAsync($"{Context.User.Mention}, you have {cur} raffle tickets.", false);
} }
@ -227,7 +231,7 @@ namespace ChaosBot.Discord.Modules.User
} }
await ReplyAsync( await ReplyAsync(
$"{Context.User.Mention} has taken {Amount} points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur-Amount} points.", $"{Context.User.Mention} has taken {Amount} raffle tickets from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur-Amount} raffle tickets.",
false); false);
} }
@ -260,10 +264,50 @@ namespace ChaosBot.Discord.Modules.User
} }
await ReplyAsync( await ReplyAsync(
$"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.", $"{Context.User.Mention} has removed all raffle tickets from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.",
false); false);
} }
public async Task ClearRaffle(string confirm = null)
{
try
{
if(confirm == "confirm")
{
using (ChaosbotContext dbContext = new ChaosbotContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
IQueryable<Raffle> ctxRaffleDetail = ctxRaffles
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id));
int cur = ctxRaffleDetail.Count();
if (cur != 0)
{
dbContext.Raffles.RemoveRange(ctxRaffleDetail);
await dbContext.SaveChangesAsync();
}
}
await ReplyAsync(
$"{Context.User.Mention} has removed all tickets.",
false);
}
else
{
await ReplyAsync(
$"{Context.User.Mention}, if you wish to clear ALL tickets, please send the below command.```{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}raffle clear confirm```",
false);
}
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
public async Task PickRaffle() public async Task PickRaffle()
{ {
try try

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); ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel);
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); ExperienceHandler.addXP(context.Guild.Id, context.User.Id, context.Channel);
await _commands.ExecuteAsync(context, argPos, _services); await _commands.ExecuteAsync(context, argPos, _services);
} }

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using ChaosBot.Models; using ChaosBot.Models;
using ChaosBot.Repositories; using ChaosBot.Repositories;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NLog; using NLog;
@ -12,7 +14,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) public static async void addXP(ulong DiscordGuildId, ulong DiscordUserId, ISocketMessageChannel Channel)
{ {
try try
{ {
@ -27,18 +29,31 @@ namespace ChaosBot.Discord.Services
if (usrXp.Any()) if (usrXp.Any())
{ {
usrNewXp = usrXp.First(); usrNewXp = usrXp.First();
usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(ConfigurationRepository.GetValue<string>("Experience:PerMsg", DiscordGuildId, "0"));
} if(DateTime.Now >= usrNewXp.LastUpdated.AddMinutes(1))
else
{ {
usrNewXp = new Experience(); usrNewXp.Amount = usrNewXp.Amount + Convert.ToUInt64(new Random().Next(15, 26));
usrNewXp.Amount = Convert.ToUInt64(ConfigurationRepository.GetValue<string>("Experience:PerMsg", DiscordGuildId, "0"));
}
usrNewXp.DiscordGuildId = DiscordGuildId; usrNewXp.DiscordGuildId = DiscordGuildId;
usrNewXp.DiscordUserId = DiscordUserId; usrNewXp.DiscordUserId = DiscordUserId;
usrNewXp.LastUpdated = DateTime.Now;
usrNewXp.Level = await checkLevel(usrNewXp, Channel);
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();
} }
else
{
usrNewXp = new Experience();
usrNewXp.Amount = Convert.ToUInt64(new Random().Next(15, 26));
usrNewXp.DiscordGuildId = DiscordGuildId;
usrNewXp.DiscordUserId = DiscordUserId;
usrNewXp.LastUpdated = DateTime.Now;
await dbContext.ExperiencePoints.Upsert(usrNewXp)
.On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync();
}
} }
} }
catch (Exception ex) catch (Exception ex)
@ -47,5 +62,28 @@ namespace ChaosBot.Discord.Services
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); $"{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;
}
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore.Metadata; using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
namespace ChaosBot.Migrations namespace ChaosBot.Migrations
@ -15,7 +16,9 @@ namespace ChaosBot.Migrations
{ {
DiscordGuildId = table.Column<ulong>(nullable: false), DiscordGuildId = table.Column<ulong>(nullable: false),
DiscordUserId = table.Column<ulong>(nullable: false), DiscordUserId = table.Column<ulong>(nullable: false),
Amount = table.Column<ulong>(nullable: false, defaultValue: 0) Amount = table.Column<ulong>(nullable: false, defaultValue: 0),
Level = table.Column<ulong>(nullable: false, defaultValue: 0),
lastUpdated = table.Column<DateTime>(nullable:false, defaultValue: "0000-00-00 00:00:00")
}, },
constraints: table => constraints: table =>
{ {

View File

@ -1,3 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace ChaosBot.Models namespace ChaosBot.Models
@ -10,6 +11,8 @@ namespace ChaosBot.Models
[Required] [Required]
public ulong DiscordGuildId { get; set; } public ulong DiscordGuildId { get; set; }
public ulong Amount { get; set; } public ulong Amount { get; set; }
public DateTime LastUpdated { get; set; }
public ulong Level { get; set; }
} }
#endregion #endregion
} }