chaosbot/ChaosBot/Discord/Modules/PointsCommands.cs

124 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Database.Entity;
using Discord;
using Discord.Commands;
using Microsoft.Extensions.Configuration;
using NLog;
using ChaosBot.Database.Repository;
using ChaosBot.Database;
using System.Data;
namespace ChaosBot.Discord.Modules
{
public class PointsCommands : ModuleBase
{
private static Logger _logger = Program._logger;
[Command("points help")]
public async Task PointsCommandInfo()
{
try
{
var sb = new StringBuilder();
var embed = new EmbedBuilder();
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix");
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($"{prefix}points status");
sb.AppendLine($"{prefix}points help");
sb.AppendLine();
sb.AppendLine("Moderation commands:");
sb.AppendLine($"{prefix}points add <discord mention> <amount>");
sb.AppendLine($"{prefix}point remove <discord mention> <amount>");
sb.AppendLine($"{prefix}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($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("points")]
[Alias("points info")]
public async Task PointsCommandTotal()
{
int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString());
await ReplyAsync($"You have {cur} points.", false);
}
[Command("points add")]
[RequireUserPermission(ChannelPermission.ManageMessages)]
public async Task RaffleCommandAdd(string user, int amount = 1)
{
if (ChannelPermissions.Text.ManageMessages)
{
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4));
await ReplyAsync($"{Context.User.Mention} has given <@{userId}> {amount} points for a total of {PointsRepository.Add(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false);
}
else
await ReplyAsync($"NO ACCESS");
}
[Command("points remove")]
[RequireUserPermission(ChannelPermission.ManageMessages)]
public async Task RaffleCommandRemove(string user, int amount = 1)
{
ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4));
int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString());
if (cur > amount)
await ReplyAsync($"{Context.User.Mention} has removed {amount} points from <@{userId}> for a total of {PointsRepository.Remove(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false);
else
await ReplyAsync($"{Context.User.Mention} has tried to remove {amount} points from <@{userId}> they only had {cur} points. None were taken...", false);
}
[Command("points delete")]
[RequireUserPermission(ChannelPermission.ManageMessages)]
public async Task DeletePoints(string userMention)
{
ulong userId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4));
Dictionary<string, object> filterColumns = new Dictionary<string, object>
{
{ "userId", userId },
{ "guildId", Context.Guild.Id }
};
int matches = PointsRepository.Count(userId.ToString(), Context.Guild.Id.ToString());
if (matches > 0)
{
Controller.DeleteQuery("PointsTable", filterColumns);
string message = $"{Context.User.Mention} has removed <@{userId}> from the database.";
await ReplyAsync(message, false);
_logger.Info($"PointsCommands.DeletePoints: {message}");
}
else
{
string message = $"{Context.User.Mention} has failed to remove <@{userId}> from the database, <@{userId}> does not exist.";
await ReplyAsync(message, false);
_logger.Warn($"PointsCommands.DeletePoints: {message}");
}
}
}
}