Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from Creeperman007/in-dev
Browse files Browse the repository at this point in the history
New framework
  • Loading branch information
fajnyCreeper authored Mar 22, 2018
2 parents 8a3090b + 30b524c commit dc7b805
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 375 deletions.
2 changes: 1 addition & 1 deletion scheme.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE `permissions` (
CREATE TABLE `warnings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild` bigint(20) NOT NULL,
`user` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`user` bigint(20) NOT NULL,
`warnsCurrent` int(11) NOT NULL DEFAULT '0',
`warnsTotal` int(11) NOT NULL DEFAULT '0',
`kicks` int(11) NOT NULL DEFAULT '0',
Expand Down
231 changes: 231 additions & 0 deletions source/WarnBot/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.EventArgs;
using DSharpPlus.Entities;

namespace WarnBot
{
class Commands
{
[Command("warn"), Description("Warns a user.")]
public async Task Warn(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr, [RemainingText, Description("Reason")] string reason)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.IsOwner) && usr.Id != ctx.Member.Id)
{
DBConnector.Prepare(usr.Id, ctx.Guild.Id);
int count = DBConnector.WarnCount(usr.Id, ctx.Guild.Id) + 1;
if (count > 3)
count = 1;
DBConnector.Warn(usr.Id, ctx.Guild.Id, count);
await ctx.RespondAsync(String.Format("Warned: {0}\nReason: {1}\nWarning {2}/3", usr.Mention, reason, count));
if (count == 3)
await ctx.RespondAsync("User can now be kicked!");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("kick"), Description("Kicks a person.")]
public async Task Kick(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr, [RemainingText, Description("Reason")] string reason)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.IsOwner) && usr.Id != ctx.Member.Id)
{
DBConnector.Prepare(usr.Id, ctx.Guild.Id);
int info = DBConnector.Info(usr.Id, ctx.Guild.Id)[0];
if (info == 3)
{
await ctx.Guild.GetMemberAsync(usr.Id).Result.RemoveAsync(reason);
DBConnector.Kick(usr.Id, ctx.Guild.Id, reason);
await ctx.RespondAsync(String.Format("Kicked {0} for {1}", usr.Username, reason));
}
else
await ctx.RespondAsync("User does not have three warnings yet!");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("ban"), Description("Bans a person.")]
public async Task Ban(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr, [RemainingText, Description("Reason")] string reason)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.IsOwner) && usr.Id != ctx.Member.Id)
{
DBConnector.Prepare(usr.Id, ctx.Guild.Id);
int kick = DBConnector.Info(usr.Id, ctx.Guild.Id)[1];
if (kick != 0 && (kick % 5) == 0)
{
await ctx.Guild.GetMemberAsync(usr.Id).Result.BanAsync(0, reason);
DBConnector.Ban(usr.Id, ctx.Guild.Id, reason);
await ctx.RespondAsync(String.Format("Banned {0} for {1}", usr.Username, reason));
}
else
await ctx.RespondAsync("User does not have number of kicks divisible by 5!");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("clear"), Description("Clears warnings count.")]
public async Task Clear(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.IsOwner) && usr.Id != ctx.Member.Id)
{
DBConnector.Clear(usr.Id, ctx.Guild.Id);
await ctx.RespondAsync("Cleared record for " + usr.Mention);
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("info"), Description("Shows warnings and kicks.")]
public async Task Info(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
DBConnector.Prepare(usr.Id, ctx.Guild.Id);
int[] info = DBConnector.Info(usr.Id, ctx.Guild.Id);
await ctx.RespondAsync(String.Format("Warnings: {0}\nKicks: {1}", info[0], info[1]));
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("addusr"), Description("Adds user to Admins.")]
public async Task AddUsr(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr, [Description("K - Kick or KB - Kick and Ban.")] string perms)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if (ctx.Member.IsOwner)
{
DBConnector.AddUsr(usr.Id, ctx.Guild.Id, perms);
await ctx.RespondAsync("Added user to Admins.");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("rmusr"), Description("Removes user from Admins.")]
public async Task RmUsr(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if (ctx.Member.IsOwner)
{
DBConnector.RmUsr(usr.Id, ctx.Guild.Id);
await ctx.RespondAsync("Removed user from Admins.");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("updateusr"), Description("Updates permissions.")]
public async Task UpdateUsr(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr, [Description("K - Kick or KB - Kick and Ban.")] string perms)
{
try
{
if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id))
{
if (ctx.Member.IsOwner)
{
DBConnector.UpdateUsr(usr.Id, ctx.Guild.Id, perms);
await ctx.RespondAsync("Updated permissions for user.");
}
}
else
await ctx.RespondAsync("Whoah, you can't use the command on this person!");
}
catch (Exception e)
{
ErrorCatch(ctx, e);
}
}

[Command("about"), Description("About this bot")]
public async Task About(CommandContext ctx)
{
var eb = new DiscordEmbedBuilder
{
Color = new DiscordColor("#FF0000"),
Title = "About",
Description = "Programmed by Creeperman007\nUsing DSharpPlus library\nGitHub repository: <http://github.com/Creeperman007/WarnBot>"
};
await ctx.RespondAsync(embed: eb);
}

private async void ErrorCatch(CommandContext ctx, Exception e)
{
await ctx.Member.SendMessageAsync("Internal error occured!");
await ctx.Member.SendMessageAsync("Go to <https://github.com/Creeperman007/WarnBot/issues> open new issue and paste text below and how you got this error");
await ctx.Member.SendMessageAsync("```" + e + "```");
}
}
}
14 changes: 7 additions & 7 deletions source/WarnBot/DBConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DBConnector
private static string pass = "warnbot";
private static string name = "warnbotdb";
private static string cs = @"server=" + host + ";userid=" + username + ";password=" + pass + ";database=" + name;
public static void Prepare(string user, UInt64 guild)
public static void Prepare(UInt64 user, UInt64 guild)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -38,7 +38,7 @@ public static void Prepare(string user, UInt64 guild)
conn.Close();

}
public static int WarnCount(string user, UInt64 guild)
public static int WarnCount(UInt64 user, UInt64 guild)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -57,7 +57,7 @@ public static int WarnCount(string user, UInt64 guild)
conn.Close();
return warns;
}
public static void Warn(string user, UInt64 guild, int warns)
public static void Warn(UInt64 user, UInt64 guild, int warns)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -78,7 +78,7 @@ public static void Warn(string user, UInt64 guild, int warns)
conn.Close();
}
}
public static void Kick(string user, UInt64 guild, string reason)
public static void Kick(UInt64 user, UInt64 guild, string reason)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -94,7 +94,7 @@ public static void Kick(string user, UInt64 guild, string reason)
conn.Close();
}
}
public static void Ban(string user, UInt64 guild, string reason)
public static void Ban(UInt64 user, UInt64 guild, string reason)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -110,7 +110,7 @@ public static void Ban(string user, UInt64 guild, string reason)
conn.Close();
}
}
public static int[] Info(string user, UInt64 guild)
public static int[] Info(UInt64 user, UInt64 guild)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand All @@ -131,7 +131,7 @@ public static int[] Info(string user, UInt64 guild)
conn.Close();
return info;
}
public static void Clear(string user, UInt64 guild)
public static void Clear(UInt64 user, UInt64 guild)
{
MySqlConnection conn = null;
conn = new MySqlConnection(cs);
Expand Down
Loading

0 comments on commit dc7b805

Please sign in to comment.