Augmenting Point System
This commit is contained in:
parent
a672c3968f
commit
8aa467cef3
275
ChaosBot/Discord/Modules/User/Points.cs
Normal file
275
ChaosBot/Discord/Modules/User/Points.cs
Normal file
@ -0,0 +1,275 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using ChaosBot.Discord.PreConditions;
|
||||||
|
using ChaosBot.Migrations;
|
||||||
|
using ChaosBot.Models;
|
||||||
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NLog;
|
||||||
|
using ChaosBot.Repositories;
|
||||||
|
using ChaosBot.Services;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace ChaosBot.Discord.Modules.User
|
||||||
|
{
|
||||||
|
public class Points : ModuleBase
|
||||||
|
{
|
||||||
|
private static ILogger _logger = Program.Logger;
|
||||||
|
|
||||||
|
[Command("points")]
|
||||||
|
[CheckCommandPerm("User")]
|
||||||
|
public async Task PointsCommand(string cmd = null, string userMention = null, ulong Amount = 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if ((cmd.ToLower() == "add") || (cmd.ToLower() == "+") || (cmd.ToLower() == "give"))
|
||||||
|
{
|
||||||
|
if(Amount != 0)
|
||||||
|
{
|
||||||
|
Boolean adminAccess = await CheckPermissions.CheckPerms(Context, "points.give", "Admin");
|
||||||
|
await AddPoints(userMention, Amount, adminAccess);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await PointsHelp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((cmd.ToLower() == "remove") || (cmd.ToLower() == "rem") || (cmd.ToLower() == "take") || (cmd.ToLower() == "-"))
|
||||||
|
{
|
||||||
|
if((Amount != 0) || (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin")))
|
||||||
|
{
|
||||||
|
await RemPoints(userMention, Amount);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await PointsHelp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del"))
|
||||||
|
{
|
||||||
|
if((Amount == 0) || (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin")))
|
||||||
|
{
|
||||||
|
await DelPoints(userMention);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await PointsHelp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((cmd.ToLower() == "=") || (cmd.ToLower() == "info") || (cmd.ToLower() == "total") || (cmd == null))
|
||||||
|
{
|
||||||
|
await TotalPoints();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await PointsHelp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PointsHelp()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
var embed = new EmbedBuilder();
|
||||||
|
|
||||||
|
embed.WithColor(new Color(255, 255, 0));
|
||||||
|
embed.Title = $"Points system";
|
||||||
|
sb.AppendLine($"{Context.User.Mention} has requested points information.");
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.AppendLine($"Usage:");
|
||||||
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}points status");
|
||||||
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}points help");
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.AppendLine("Moderation commands:");
|
||||||
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}points add <discord mention> <amount>");
|
||||||
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}point remove <discord mention> <amount>");
|
||||||
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}point delete <discord mention>");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Add the string to the Embed
|
||||||
|
*/
|
||||||
|
embed.Description = sb.ToString();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reply with the Embed created above
|
||||||
|
*/
|
||||||
|
await ReplyAsync(null, false, embed.Build());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task TotalPoints()
|
||||||
|
{
|
||||||
|
ulong cur = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
||||||
|
{
|
||||||
|
IQueryable<Point> points = dbContext.Points;
|
||||||
|
cur = points
|
||||||
|
.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id))
|
||||||
|
.Where(p => p.DiscordUserId.Equals(Context.User.Id))
|
||||||
|
.First().Amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddPoints(string userMention = null, ulong Amount = 0, bool admin = false)
|
||||||
|
{
|
||||||
|
ulong cur = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (admin)
|
||||||
|
{
|
||||||
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
||||||
|
{
|
||||||
|
IQueryable<Point> ctxPoints = dbContext.Points;
|
||||||
|
|
||||||
|
Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Context.User.Id)).First();
|
||||||
|
|
||||||
|
cur = usrPoint.Amount + Amount;
|
||||||
|
usrPoint.Amount = cur;
|
||||||
|
usrPoint.DiscordGuildId = Context.Guild.Id;
|
||||||
|
usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
|
||||||
|
|
||||||
|
await dbContext.Points.Upsert(usrPoint)
|
||||||
|
.On(x => usrPoint).RunAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
await ReplyAsync(
|
||||||
|
$"{Context.User.Mention} has added {Amount} points to <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> for a total of {cur} points.",
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
||||||
|
{
|
||||||
|
IQueryable<Point> ctxPoints = dbContext.Points;
|
||||||
|
|
||||||
|
Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Context.User.Id)).First();
|
||||||
|
Point rctPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).First();
|
||||||
|
|
||||||
|
if (usrPoint.Amount >= Amount)
|
||||||
|
{
|
||||||
|
usrPoint.Amount -= Amount;
|
||||||
|
usrPoint.DiscordGuildId = Context.Guild.Id;
|
||||||
|
usrPoint.DiscordUserId = Context.User.Id;
|
||||||
|
|
||||||
|
await dbContext.Points.Upsert(usrPoint)
|
||||||
|
.On(x => usrPoint).RunAsync();
|
||||||
|
|
||||||
|
cur = rctPoint.Amount + Amount;
|
||||||
|
rctPoint.Amount = cur;
|
||||||
|
rctPoint.DiscordGuildId = Context.Guild.Id;
|
||||||
|
rctPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
|
||||||
|
|
||||||
|
await dbContext.Points.Upsert(rctPoint)
|
||||||
|
.On(x => rctPoint).RunAsync();
|
||||||
|
|
||||||
|
await ReplyAsync(
|
||||||
|
$"{Context.User.Mention} has given <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> {Amount} points for a total of {cur} points.",
|
||||||
|
false);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await ReplyAsync(
|
||||||
|
$"{Context.User.Mention}, you do not have enough points to give <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> {Amount} points.",
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemPoints(string userMention = null, ulong Amount = 0)
|
||||||
|
{
|
||||||
|
ulong cur = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
||||||
|
{
|
||||||
|
IQueryable<Point> ctxPoints = dbContext.Points;
|
||||||
|
|
||||||
|
Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).First();
|
||||||
|
|
||||||
|
cur = (usrPoint.Amount - Amount) >= 1 ? cur : 0;
|
||||||
|
usrPoint.Amount = cur;
|
||||||
|
usrPoint.DiscordGuildId = Context.Guild.Id;
|
||||||
|
usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
|
||||||
|
|
||||||
|
await dbContext.Points.Upsert(usrPoint)
|
||||||
|
.On(x => usrPoint).RunAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
|
||||||
|
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} points.",
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DelPoints(string userMention = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
||||||
|
{
|
||||||
|
Point usrPoint = new Point();
|
||||||
|
|
||||||
|
usrPoint.Amount = 0;
|
||||||
|
usrPoint.DiscordGuildId = Context.Guild.Id;
|
||||||
|
usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
|
||||||
|
|
||||||
|
await dbContext.Points.Upsert(usrPoint)
|
||||||
|
.On(x => usrPoint).RunAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(
|
||||||
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await ReplyAsync(
|
||||||
|
$"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.",
|
||||||
|
false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user