From 4fdff99e70bc5284991d9fd1f1178a6dd73f50ed Mon Sep 17 00:00:00 2001 From: Creeperman007 Date: Mon, 19 Mar 2018 21:15:54 +0100 Subject: [PATCH 1/4] First half of commands --- scheme.sql | 2 +- source/WarnBot/Commands.cs | 113 +++++++ source/WarnBot/DBConnector.cs | 14 +- source/WarnBot/Program.cs | 378 ++-------------------- source/WarnBot/Properties/AssemblyInfo.cs | 2 +- source/WarnBot/WarnBot.csproj | 8 + source/WarnBot/packages.config | 2 + 7 files changed, 166 insertions(+), 353 deletions(-) create mode 100644 source/WarnBot/Commands.cs diff --git a/scheme.sql b/scheme.sql index 492b5d8..e2487c8 100644 --- a/scheme.sql +++ b/scheme.sql @@ -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', diff --git a/source/WarnBot/Commands.cs b/source/WarnBot/Commands.cs new file mode 100644 index 0000000..dee1f0e --- /dev/null +++ b/source/WarnBot/Commands.cs @@ -0,0 +1,113 @@ +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")] + public async Task Warn(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + { + try + { + if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && 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"); + } + } + catch (Exception e) + { + ErrorCatch(ctx, e); + } + } + + [Command("kick")] + public async Task Kick(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + { + try + { + if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && 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!"); + } + } + catch (Exception e) + { + ErrorCatch(ctx, e); + } + } + + [Command("ban")] + public async Task Ban(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + { + try + { + if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + { + DBConnector.Prepare(usr.Id, ctx.Guild.Id); + int kick = DBConnector.Info(usr.Id, ctx.Guild.Id)[1]; + if (kick != 3 && (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!"); + } + } + catch (Exception e) + { + ErrorCatch(ctx, e); + } + } + + [Command("clear")] + public async Task Clear(CommandContext ctx, DiscordUser usr) + { + try + { + if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + { + DBConnector.Clear(usr.Id, ctx.Guild.Id); + await ctx.RespondAsync("Cleared record for " + usr.Mention); + } + } + catch (Exception e) + { + ErrorCatch(ctx, e); + } + } + + private async void ErrorCatch(CommandContext ctx, Exception e) + { + await ctx.Member.SendMessageAsync("Internal error occured!"); + await ctx.Member.SendMessageAsync("Go to open new issue and paste text below and how you got this error"); + await ctx.Member.SendMessageAsync("```" + e + "```"); + } + } +} diff --git a/source/WarnBot/DBConnector.cs b/source/WarnBot/DBConnector.cs index 9c101f3..484e359 100644 --- a/source/WarnBot/DBConnector.cs +++ b/source/WarnBot/DBConnector.cs @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/source/WarnBot/Program.cs b/source/WarnBot/Program.cs index af99bd3..582ea5a 100644 --- a/source/WarnBot/Program.cs +++ b/source/WarnBot/Program.cs @@ -1,366 +1,56 @@ -using Discord; -using Discord.WebSocket; -using Discord.Commands; -using System; +using System; using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.CommandsNext; +using DSharpPlus.EventArgs; namespace WarnBot { - public class Program + class Program { public static void Main(string[] args) - => new Program().MainAsync().GetAwaiter().GetResult(); - - public async Task MainAsync() - { - var client = new DiscordSocketClient(); - - client.Log += Log; - client.MessageReceived += MessageReceived; - - string token = File.ReadAllText(@"token.txt"); // Remember to keep this private! - await client.LoginAsync(TokenType.Bot, token); - await client.StartAsync(); - await client.SetGameAsync("/help"); - // Block this task until the program is closed. - await Task.Delay(-1); - } - - private async Task MessageReceived(SocketMessage msg) { try { - if (msg.Content.Contains("/") && !msg.Content.Contains("`")) - { - string[] msgSplit = msg.ToString().Split(' '); - string cmd = msgSplit[0]; - string user = ""; - string context = ""; - ulong usr2ulong = 0; - string[] replace = { "<", ">", "@", "!" }; - try - { - if (msgSplit[1] != "@everyone" && msgSplit[1] != "@here") - user = msgSplit[1]; - string tmp = user; - for (int i = 2; i < msgSplit.Length; i++) - context += msgSplit[i] + " "; - foreach (string e in replace) - tmp = tmp.Replace(e, ""); - usr2ulong = ulong.Parse(tmp); - - } - catch { } - var chnl = msg.Channel as SocketGuildChannel; - if (usr2ulong != 355763643964719106 && usr2ulong != chnl.Guild.Owner.Id) - { - switch (cmd) - { - case "/warn": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - if (context != "") - { - DBConnector.Prepare(user, chnl.Guild.Id); - int count = DBConnector.WarnCount(user, chnl.Guild.Id) + 1; - if (count > 3) - count = 1; - DBConnector.Warn(user, chnl.Guild.Id, count); - await msg.Channel.SendMessageAsync("Warned: " + user + "\nReason: " + context + "\nWarning " + count + "/3"); - if (count == 3) - await msg.Channel.SendMessageAsync("User now can be kicked!"); - } - else - await msg.Channel.SendMessageAsync("You can't warn without a reason!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot warn user!"); - ErrorCatch(msg, e); - } - break; - case "/kick": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - if (info[0] == 3) - { - if (context != "") - { - await chnl.GetUser(usr2ulong).KickAsync(context); - DBConnector.Kick(user, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Kicked " + user + " for \"" + context + "\""); - } - else - await msg.Channel.SendMessageAsync("You can't kick without a reason!"); - } - else - await msg.Channel.SendMessageAsync("User does not have 3 warnings yet!"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot kick user!"); - ErrorCatch(msg, e); - } - break; - case "/ban": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - if (context != "") - { - int kick = DBConnector.Info(user, chnl.Guild.Id)[1]; - if (kick != 0 && (kick % 5) == 0) - { - DBConnector.Ban(user, chnl.Guild.Id, context); - await chnl.Guild.AddBanAsync(usr2ulong, 0, context); - await msg.Channel.SendMessageAsync("Banned " + user + " for \"" + context + "\""); - } - else - await msg.Channel.SendMessageAsync("User does not have number of kicks divisible by 5!"); - } - else - await msg.Channel.SendMessageAsync("You can't ban without a reason!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot ban user!"); - ErrorCatch(msg, e); - } - break; - case "/clear": - try - { - if (user != "") - { - if (DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || (msg.Author.Id == chnl.Guild.Owner.Id || usr2ulong != msg.Author.Id)) - { - DBConnector.Clear(user, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Cleared record for " + user); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot clear record for user!"); - ErrorCatch(msg, e); - } - break; - case "/info": - try - { - if (user != "") - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Warnings: " + info[0] + "\nKicks: " + info[1]); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot retreive info about user!"); - ErrorCatch(msg, e); - } - break; - case "/help": - await msg.Channel.SendMessageAsync("```Everyone:\n/about............................About this bot\n/example ...............Shows example of specified command\n/info ......................Shows warnings and kicks\n\nAdmins:\n/ban ..............Bans person\n/clear .....................Clears warning count\n/check .....................Total and current warning count + kicks\n/kick .............Kicks person\n/warn .............Give person warning\n\nOwner:\n/addusr .............Adds user to Admins\n/rmusr ..............Remove user from Admins\n/updateusr .........Updates permissions for user\n..................................K=Kick, KB=Kick and Ban```"); - break; - case "/addusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.AddUsr(usr2ulong, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Added user to Admins."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot add user to Admins!"); - ErrorCatch(msg, e); - } - break; - case "/rmusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.RmUsr(usr2ulong, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Removed user from Admins."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot remove user from Admins!"); - ErrorCatch(msg, e); - } - break; - case "/updateusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.UpdateUsr(usr2ulong, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Updated permissions for user."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot update permissions!"); - ErrorCatch(msg, e); - } - break; - case "/about": - var eb = new EmbedBuilder(); - eb.WithColor(Color.Red); - eb.WithTitle("About"); - eb.WithDescription("Programmed by Creeperman007\nUsing Discord.Net library\nGitHub repository: "); - await msg.Channel.SendMessageAsync("", false, eb); - break; - case "/example": - switch (user) - { - case "/ban": - await msg.Channel.SendMessageAsync("```/ban @" + msg.Author.ToString() + " Spamming```"); - break; - case "/kick": - await msg.Channel.SendMessageAsync("```/kick @" + msg.Author.ToString() + " Spamming```"); - break; - case "/warn": - await msg.Channel.SendMessageAsync("```/warn @" + msg.Author.ToString() + " Spamming```"); - break; - case "/clear": - await msg.Channel.SendMessageAsync("```/clear @" + msg.Author.ToString() + "```"); - break; - case "/info": - await msg.Channel.SendMessageAsync("```/info @" + msg.Author.ToString() + "```"); - break; - case "/addusr": - await msg.Channel.SendMessageAsync("```/addusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/addusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); - break; - case "/rmusr": - await msg.Channel.SendMessageAsync("```/rmusr @" + msg.Author.ToString() + "```"); - break; - case "/updateusr": - await msg.Channel.SendMessageAsync("```/updateusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/updateusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); - break; - case "/example": - await msg.Channel.SendMessageAsync("```/example /kick```"); - break; - case "/check": - await msg.Channel.SendMessageAsync("```/check @" + msg.Author.ToString() + "```"); - break; - default: - await msg.Channel.SendMessageAsync("This is not existing command, or it does not need any arguments :confused:"); - break; - } - break; - case "/check": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id)) - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - await msg.Author.SendMessageAsync("Admin check for " + user + " from **" + chnl.Guild.Name + "**\nCurrent warnings: " + info[0] + "\nTotal warnings: " + info[2] + "\nKicks: " + info[1]); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot check user!"); - ErrorCatch(msg, e); - } - break; - } - } - else if (cmd.Contains("/") && !cmd.Contains("://")) - { - await msg.Channel.SendMessageAsync(msg.Author.Mention + " is it good idea to do this?"); - } - } + MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception e) { Console.WriteLine(e); + Console.ReadKey(); } } - private Task Log(LogMessage msg) + static async Task MainAsync(string[] args) { - Console.WriteLine(msg); - return Task.CompletedTask; + DiscordClient discord = new DiscordClient(new DiscordConfiguration + { + Token = File.ReadAllText("token.txt"), + TokenType = TokenType.Bot, + UseInternalLogHandler = true, + LogLevel = LogLevel.Info + }); + + CommandsNextModule commands = discord.UseCommandsNext(new CommandsNextConfiguration + { + StringPrefix = "/" + }); + + commands.RegisterCommands(); + + discord.Ready += Client_Ready; + await discord.ConnectAsync(); + await Task.Delay(-1); } - private async void ErrorCatch(SocketMessage msg, Exception e) + + private static Task Client_Ready(ReadyEventArgs e) { - Console.WriteLine(e); - await msg.Channel.SendMessageAsync("Internal error occured!"); - await msg.Author.SendMessageAsync("Go to open new issue and paste text below and how you got this error"); - await msg.Author.SendMessageAsync("```" + e + "```"); + e.Client.DebugLogger.LogMessage(LogLevel.Info, "WarnBot", "Client is ready to operate.", DateTime.Now); + return Task.CompletedTask; } } -} \ No newline at end of file +} diff --git a/source/WarnBot/Properties/AssemblyInfo.cs b/source/WarnBot/Properties/AssemblyInfo.cs index f2c647e..5a43c5f 100644 --- a/source/WarnBot/Properties/AssemblyInfo.cs +++ b/source/WarnBot/Properties/AssemblyInfo.cs @@ -8,7 +8,7 @@ [assembly: AssemblyTitle("WarnBot")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] +[assembly: AssemblyCompany("Creeperman007")] [assembly: AssemblyProduct("WarnBot")] [assembly: AssemblyCopyright("Copyright © 2017")] [assembly: AssemblyTrademark("")] diff --git a/source/WarnBot/WarnBot.csproj b/source/WarnBot/WarnBot.csproj index 18d5b03..3e02824 100644 --- a/source/WarnBot/WarnBot.csproj +++ b/source/WarnBot/WarnBot.csproj @@ -52,6 +52,12 @@ ..\packages\Discord.Net.WebSocket.1.0.2\lib\net45\Discord.Net.WebSocket.dll + + ..\packages\DSharpPlus.3.2.3\lib\net46\DSharpPlus.dll + + + ..\packages\DSharpPlus.CommandsNext.3.2.3\lib\net46\DSharpPlus.CommandsNext.dll + ..\packages\Microsoft.Extensions.DependencyInjection.1.1.1\lib\netstandard1.1\Microsoft.Extensions.DependencyInjection.dll @@ -152,8 +158,10 @@ + + diff --git a/source/WarnBot/packages.config b/source/WarnBot/packages.config index 5e20722..17769d0 100644 --- a/source/WarnBot/packages.config +++ b/source/WarnBot/packages.config @@ -7,6 +7,8 @@ + + From 58a4b67b6968b405241d01602859986bf28436d0 Mon Sep 17 00:00:00 2001 From: Creeperman007 Date: Mon, 19 Mar 2018 21:21:44 +0100 Subject: [PATCH 2/4] ProgramOld everything is commented and not working, but I won't delete it until it's done ;) --- source/WarnBot/ProgramOld.cs | 366 +++++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+) create mode 100644 source/WarnBot/ProgramOld.cs diff --git a/source/WarnBot/ProgramOld.cs b/source/WarnBot/ProgramOld.cs new file mode 100644 index 0000000..1085e12 --- /dev/null +++ b/source/WarnBot/ProgramOld.cs @@ -0,0 +1,366 @@ +/*using Discord; +using Discord.WebSocket; +using Discord.Commands; +using System; +using System.IO; +using System.Threading.Tasks; + +namespace WarnBot +{ + public class ProgramOld + { + public static void Main(string[] args) + => new ProgramOld().MainAsync().GetAwaiter().GetResult(); + + public async Task MainAsync() + { + var client = new DiscordSocketClient(); + + client.Log += Log; + client.MessageReceived += MessageReceived; + + string token = File.ReadAllText(@"token.txt"); // Remember to keep this private! + await client.LoginAsync(TokenType.Bot, token); + await client.StartAsync(); + await client.SetGameAsync("/help"); + // Block this task until the program is closed. + await Task.Delay(-1); + } + + private async Task MessageReceived(SocketMessage msg) + { + try + { + if (msg.Content.Contains("/") && !msg.Content.Contains("`")) + { + string[] msgSplit = msg.ToString().Split(' '); + string cmd = msgSplit[0]; + string user = ""; + string context = ""; + ulong usr2ulong = 0; + string[] replace = { "<", ">", "@", "!" }; + try + { + if (msgSplit[1] != "@everyone" && msgSplit[1] != "@here") + user = msgSplit[1]; + string tmp = user; + for (int i = 2; i < msgSplit.Length; i++) + context += msgSplit[i] + " "; + foreach (string e in replace) + tmp = tmp.Replace(e, ""); + usr2ulong = ulong.Parse(tmp); + + } + catch { } + var chnl = msg.Channel as SocketGuildChannel; + if (usr2ulong != 355763643964719106 && usr2ulong != chnl.Guild.Owner.Id) + { + switch (cmd) + { + case "/warn": + try + { + if (user != "") + { + if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) + { + if (context != "") + { + DBConnector.Prepare(user, chnl.Guild.Id); + int count = DBConnector.WarnCount(user, chnl.Guild.Id) + 1; + if (count > 3) + count = 1; + DBConnector.Warn(user, chnl.Guild.Id, count); + await msg.Channel.SendMessageAsync("Warned: " + user + "\nReason: " + context + "\nWarning " + count + "/3"); + if (count == 3) + await msg.Channel.SendMessageAsync("User now can be kicked!"); + } + else + await msg.Channel.SendMessageAsync("You can't warn without a reason!"); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot warn user!"); + ErrorCatch(msg, e); + } + break; + case "/kick": + try + { + if (user != "") + { + if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) + { + DBConnector.Prepare(user, chnl.Guild.Id); + int[] info = DBConnector.Info(user, chnl.Guild.Id); + if (info[0] == 3) + { + if (context != "") + { + await chnl.GetUser(usr2ulong).KickAsync(context); + DBConnector.Kick(user, chnl.Guild.Id, context); + await msg.Channel.SendMessageAsync("Kicked " + user + " for \"" + context + "\""); + } + else + await msg.Channel.SendMessageAsync("You can't kick without a reason!"); + } + else + await msg.Channel.SendMessageAsync("User does not have 3 warnings yet!"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot kick user!"); + ErrorCatch(msg, e); + } + break; + case "/ban": + try + { + if (user != "") + { + if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) + { + if (context != "") + { + int kick = DBConnector.Info(user, chnl.Guild.Id)[1]; + if (kick != 0 && (kick % 5) == 0) + { + DBConnector.Ban(user, chnl.Guild.Id, context); + await chnl.Guild.AddBanAsync(usr2ulong, 0, context); + await msg.Channel.SendMessageAsync("Banned " + user + " for \"" + context + "\""); + } + else + await msg.Channel.SendMessageAsync("User does not have number of kicks divisible by 5!"); + } + else + await msg.Channel.SendMessageAsync("You can't ban without a reason!"); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot ban user!"); + ErrorCatch(msg, e); + } + break; + case "/clear": + try + { + if (user != "") + { + if (DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || (msg.Author.Id == chnl.Guild.Owner.Id || usr2ulong != msg.Author.Id)) + { + DBConnector.Clear(user, chnl.Guild.Id); + await msg.Channel.SendMessageAsync("Cleared record for " + user); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot clear record for user!"); + ErrorCatch(msg, e); + } + break; + case "/info": + try + { + if (user != "") + { + DBConnector.Prepare(user, chnl.Guild.Id); + int[] info = DBConnector.Info(user, chnl.Guild.Id); + await msg.Channel.SendMessageAsync("Warnings: " + info[0] + "\nKicks: " + info[1]); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot retreive info about user!"); + ErrorCatch(msg, e); + } + break; + case "/help": + await msg.Channel.SendMessageAsync("```Everyone:\n/about............................About this bot\n/example ...............Shows example of specified command\n/info ......................Shows warnings and kicks\n\nAdmins:\n/ban ..............Bans person\n/clear .....................Clears warning count\n/check .....................Total and current warning count + kicks\n/kick .............Kicks person\n/warn .............Give person warning\n\nOwner:\n/addusr .............Adds user to Admins\n/rmusr ..............Remove user from Admins\n/updateusr .........Updates permissions for user\n..................................K=Kick, KB=Kick and Ban```"); + break; + case "/addusr": + try + { + if (user != "") + { + if (msg.Author.Id == chnl.Guild.Owner.Id) + { + DBConnector.AddUsr(usr2ulong, chnl.Guild.Id, context); + await msg.Channel.SendMessageAsync("Added user to Admins."); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot add user to Admins!"); + ErrorCatch(msg, e); + } + break; + case "/rmusr": + try + { + if (user != "") + { + if (msg.Author.Id == chnl.Guild.Owner.Id) + { + DBConnector.RmUsr(usr2ulong, chnl.Guild.Id); + await msg.Channel.SendMessageAsync("Removed user from Admins."); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot remove user from Admins!"); + ErrorCatch(msg, e); + } + break; + case "/updateusr": + try + { + if (user != "") + { + if (msg.Author.Id == chnl.Guild.Owner.Id) + { + DBConnector.UpdateUsr(usr2ulong, chnl.Guild.Id, context); + await msg.Channel.SendMessageAsync("Updated permissions for user."); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot update permissions!"); + ErrorCatch(msg, e); + } + break; + case "/about": + var eb = new EmbedBuilder(); + eb.WithColor(Color.Red); + eb.WithTitle("About"); + eb.WithDescription("Programmed by Creeperman007\nUsing Discord.Net library\nGitHub repository: "); + await msg.Channel.SendMessageAsync("", false, eb); + break; + case "/example": + switch (user) + { + case "/ban": + await msg.Channel.SendMessageAsync("```/ban @" + msg.Author.ToString() + " Spamming```"); + break; + case "/kick": + await msg.Channel.SendMessageAsync("```/kick @" + msg.Author.ToString() + " Spamming```"); + break; + case "/warn": + await msg.Channel.SendMessageAsync("```/warn @" + msg.Author.ToString() + " Spamming```"); + break; + case "/clear": + await msg.Channel.SendMessageAsync("```/clear @" + msg.Author.ToString() + "```"); + break; + case "/info": + await msg.Channel.SendMessageAsync("```/info @" + msg.Author.ToString() + "```"); + break; + case "/addusr": + await msg.Channel.SendMessageAsync("```/addusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/addusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); + break; + case "/rmusr": + await msg.Channel.SendMessageAsync("```/rmusr @" + msg.Author.ToString() + "```"); + break; + case "/updateusr": + await msg.Channel.SendMessageAsync("```/updateusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/updateusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); + break; + case "/example": + await msg.Channel.SendMessageAsync("```/example /kick```"); + break; + case "/check": + await msg.Channel.SendMessageAsync("```/check @" + msg.Author.ToString() + "```"); + break; + default: + await msg.Channel.SendMessageAsync("This is not existing command, or it does not need any arguments :confused:"); + break; + } + break; + case "/check": + try + { + if (user != "") + { + if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id)) + { + DBConnector.Prepare(user, chnl.Guild.Id); + int[] info = DBConnector.Info(user, chnl.Guild.Id); + await msg.Author.SendMessageAsync("Admin check for " + user + " from **" + chnl.Guild.Name + "**\nCurrent warnings: " + info[0] + "\nTotal warnings: " + info[2] + "\nKicks: " + info[1]); + } + else + await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); + } + else + await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); + } + catch (Exception e) + { + await msg.Channel.SendMessageAsync("Cannot check user!"); + ErrorCatch(msg, e); + } + break; + } + } + else if (cmd.Contains("/") && !cmd.Contains("://")) + { + await msg.Channel.SendMessageAsync(msg.Author.Mention + " is it good idea to do this?"); + } + } + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + + private Task Log(LogMessage msg) + { + Console.WriteLine(msg); + return Task.CompletedTask; + } + private async void ErrorCatch(SocketMessage msg, Exception e) + { + Console.WriteLine(e); + await msg.Channel.SendMessageAsync("Internal error occured!"); + await msg.Author.SendMessageAsync("Go to open new issue and paste text below and how you got this error"); + await msg.Author.SendMessageAsync("```" + e + "```"); + } + } +}*/ \ No newline at end of file From 92c7486f89a37cf92d8fb8100c9b112f72bda406 Mon Sep 17 00:00:00 2001 From: Creeperman007 Date: Thu, 22 Mar 2018 21:38:14 +0100 Subject: [PATCH 3/4] Second part Another commands are here! --- source/WarnBot/Commands.cs | 194 ++++++++++++++---- source/WarnBot/Program.cs | 9 + source/WarnBot/ProgramOld.cs | 366 ---------------------------------- source/WarnBot/WarnBot.csproj | 1 - 4 files changed, 165 insertions(+), 405 deletions(-) delete mode 100644 source/WarnBot/ProgramOld.cs diff --git a/source/WarnBot/Commands.cs b/source/WarnBot/Commands.cs index dee1f0e..2d9dcbd 100644 --- a/source/WarnBot/Commands.cs +++ b/source/WarnBot/Commands.cs @@ -13,22 +13,27 @@ namespace WarnBot { class Commands { - [Command("warn")] - public async Task Warn(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + [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 ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.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"); + 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) { @@ -36,24 +41,81 @@ public async Task Warn(CommandContext ctx, DiscordUser usr, [RemainingText] stri } } - [Command("kick")] - public async Task Kick(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + [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 ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + 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)[0]; - if (info == 3) + 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) { - 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)); + DBConnector.Clear(usr.Id, ctx.Guild.Id); + await ctx.RespondAsync("Cleared record for " + usr.Mention); } - 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) { @@ -61,24 +123,41 @@ public async Task Kick(CommandContext ctx, DiscordUser usr, [RemainingText] stri } } - [Command("ban")] - public async Task Ban(CommandContext ctx, DiscordUser usr, [RemainingText] string reason) + [Command("info"), Description("Shows warnings and kicks.")] + public async Task Info(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr) { try { - if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id)) { DBConnector.Prepare(usr.Id, ctx.Guild.Id); - int kick = DBConnector.Info(usr.Id, ctx.Guild.Id)[1]; - if (kick != 3 && (kick % 5) == 0) + 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) { - 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)); + DBConnector.AddUsr(usr.Id, ctx.Guild.Id, perms); + await ctx.RespondAsync("Added user to Admins."); } - 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) { @@ -86,16 +165,21 @@ public async Task Ban(CommandContext ctx, DiscordUser usr, [RemainingText] strin } } - [Command("clear")] - public async Task Clear(CommandContext ctx, DiscordUser usr) + [Command("rmusr"), Description("Removes user from Admins.")] + public async Task RmUsr(CommandContext ctx, [Description("Mention of user.")] DiscordUser usr) { try { - if ((DBConnector.PermCheck(ctx.Member.Id, ctx.Guild.Id)[0] > 1 || ctx.Member.Id == ctx.Guild.Owner.Id) && usr.Id != ctx.Member.Id) + if (!usr.IsCurrent || (usr.Id != ctx.Guild.Owner.Id)) { - DBConnector.Clear(usr.Id, ctx.Guild.Id); - await ctx.RespondAsync("Cleared record for " + usr.Mention); + 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) { @@ -103,7 +187,41 @@ public async Task Clear(CommandContext ctx, DiscordUser usr) } } - private async void ErrorCatch(CommandContext ctx, Exception 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: " + }; + 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 open new issue and paste text below and how you got this error"); diff --git a/source/WarnBot/Program.cs b/source/WarnBot/Program.cs index 582ea5a..795c2b8 100644 --- a/source/WarnBot/Program.cs +++ b/source/WarnBot/Program.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Exceptions; using DSharpPlus.EventArgs; namespace WarnBot @@ -40,6 +41,8 @@ static async Task MainAsync(string[] args) StringPrefix = "/" }); + commands.CommandExecuted += Commands_CommandExecuted; + commands.RegisterCommands(); discord.Ready += Client_Ready; @@ -52,5 +55,11 @@ private static Task Client_Ready(ReadyEventArgs e) e.Client.DebugLogger.LogMessage(LogLevel.Info, "WarnBot", "Client is ready to operate.", DateTime.Now); return Task.CompletedTask; } + + private static Task Commands_CommandExecuted(CommandExecutionEventArgs e) + { + e.Context.Client.DebugLogger.LogMessage(LogLevel.Info, "WarnBot", $"{e.Context.User.Username} successfully executed '{e.Command.QualifiedName}'", DateTime.Now); + return Task.CompletedTask; + } } } diff --git a/source/WarnBot/ProgramOld.cs b/source/WarnBot/ProgramOld.cs deleted file mode 100644 index 1085e12..0000000 --- a/source/WarnBot/ProgramOld.cs +++ /dev/null @@ -1,366 +0,0 @@ -/*using Discord; -using Discord.WebSocket; -using Discord.Commands; -using System; -using System.IO; -using System.Threading.Tasks; - -namespace WarnBot -{ - public class ProgramOld - { - public static void Main(string[] args) - => new ProgramOld().MainAsync().GetAwaiter().GetResult(); - - public async Task MainAsync() - { - var client = new DiscordSocketClient(); - - client.Log += Log; - client.MessageReceived += MessageReceived; - - string token = File.ReadAllText(@"token.txt"); // Remember to keep this private! - await client.LoginAsync(TokenType.Bot, token); - await client.StartAsync(); - await client.SetGameAsync("/help"); - // Block this task until the program is closed. - await Task.Delay(-1); - } - - private async Task MessageReceived(SocketMessage msg) - { - try - { - if (msg.Content.Contains("/") && !msg.Content.Contains("`")) - { - string[] msgSplit = msg.ToString().Split(' '); - string cmd = msgSplit[0]; - string user = ""; - string context = ""; - ulong usr2ulong = 0; - string[] replace = { "<", ">", "@", "!" }; - try - { - if (msgSplit[1] != "@everyone" && msgSplit[1] != "@here") - user = msgSplit[1]; - string tmp = user; - for (int i = 2; i < msgSplit.Length; i++) - context += msgSplit[i] + " "; - foreach (string e in replace) - tmp = tmp.Replace(e, ""); - usr2ulong = ulong.Parse(tmp); - - } - catch { } - var chnl = msg.Channel as SocketGuildChannel; - if (usr2ulong != 355763643964719106 && usr2ulong != chnl.Guild.Owner.Id) - { - switch (cmd) - { - case "/warn": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - if (context != "") - { - DBConnector.Prepare(user, chnl.Guild.Id); - int count = DBConnector.WarnCount(user, chnl.Guild.Id) + 1; - if (count > 3) - count = 1; - DBConnector.Warn(user, chnl.Guild.Id, count); - await msg.Channel.SendMessageAsync("Warned: " + user + "\nReason: " + context + "\nWarning " + count + "/3"); - if (count == 3) - await msg.Channel.SendMessageAsync("User now can be kicked!"); - } - else - await msg.Channel.SendMessageAsync("You can't warn without a reason!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot warn user!"); - ErrorCatch(msg, e); - } - break; - case "/kick": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - if (info[0] == 3) - { - if (context != "") - { - await chnl.GetUser(usr2ulong).KickAsync(context); - DBConnector.Kick(user, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Kicked " + user + " for \"" + context + "\""); - } - else - await msg.Channel.SendMessageAsync("You can't kick without a reason!"); - } - else - await msg.Channel.SendMessageAsync("User does not have 3 warnings yet!"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot kick user!"); - ErrorCatch(msg, e); - } - break; - case "/ban": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id) && usr2ulong != msg.Author.Id) - { - if (context != "") - { - int kick = DBConnector.Info(user, chnl.Guild.Id)[1]; - if (kick != 0 && (kick % 5) == 0) - { - DBConnector.Ban(user, chnl.Guild.Id, context); - await chnl.Guild.AddBanAsync(usr2ulong, 0, context); - await msg.Channel.SendMessageAsync("Banned " + user + " for \"" + context + "\""); - } - else - await msg.Channel.SendMessageAsync("User does not have number of kicks divisible by 5!"); - } - else - await msg.Channel.SendMessageAsync("You can't ban without a reason!"); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot ban user!"); - ErrorCatch(msg, e); - } - break; - case "/clear": - try - { - if (user != "") - { - if (DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[1] >= 1 || (msg.Author.Id == chnl.Guild.Owner.Id || usr2ulong != msg.Author.Id)) - { - DBConnector.Clear(user, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Cleared record for " + user); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot clear record for user!"); - ErrorCatch(msg, e); - } - break; - case "/info": - try - { - if (user != "") - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Warnings: " + info[0] + "\nKicks: " + info[1]); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot retreive info about user!"); - ErrorCatch(msg, e); - } - break; - case "/help": - await msg.Channel.SendMessageAsync("```Everyone:\n/about............................About this bot\n/example ...............Shows example of specified command\n/info ......................Shows warnings and kicks\n\nAdmins:\n/ban ..............Bans person\n/clear .....................Clears warning count\n/check .....................Total and current warning count + kicks\n/kick .............Kicks person\n/warn .............Give person warning\n\nOwner:\n/addusr .............Adds user to Admins\n/rmusr ..............Remove user from Admins\n/updateusr .........Updates permissions for user\n..................................K=Kick, KB=Kick and Ban```"); - break; - case "/addusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.AddUsr(usr2ulong, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Added user to Admins."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot add user to Admins!"); - ErrorCatch(msg, e); - } - break; - case "/rmusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.RmUsr(usr2ulong, chnl.Guild.Id); - await msg.Channel.SendMessageAsync("Removed user from Admins."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot remove user from Admins!"); - ErrorCatch(msg, e); - } - break; - case "/updateusr": - try - { - if (user != "") - { - if (msg.Author.Id == chnl.Guild.Owner.Id) - { - DBConnector.UpdateUsr(usr2ulong, chnl.Guild.Id, context); - await msg.Channel.SendMessageAsync("Updated permissions for user."); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot update permissions!"); - ErrorCatch(msg, e); - } - break; - case "/about": - var eb = new EmbedBuilder(); - eb.WithColor(Color.Red); - eb.WithTitle("About"); - eb.WithDescription("Programmed by Creeperman007\nUsing Discord.Net library\nGitHub repository: "); - await msg.Channel.SendMessageAsync("", false, eb); - break; - case "/example": - switch (user) - { - case "/ban": - await msg.Channel.SendMessageAsync("```/ban @" + msg.Author.ToString() + " Spamming```"); - break; - case "/kick": - await msg.Channel.SendMessageAsync("```/kick @" + msg.Author.ToString() + " Spamming```"); - break; - case "/warn": - await msg.Channel.SendMessageAsync("```/warn @" + msg.Author.ToString() + " Spamming```"); - break; - case "/clear": - await msg.Channel.SendMessageAsync("```/clear @" + msg.Author.ToString() + "```"); - break; - case "/info": - await msg.Channel.SendMessageAsync("```/info @" + msg.Author.ToString() + "```"); - break; - case "/addusr": - await msg.Channel.SendMessageAsync("```/addusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/addusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); - break; - case "/rmusr": - await msg.Channel.SendMessageAsync("```/rmusr @" + msg.Author.ToString() + "```"); - break; - case "/updateusr": - await msg.Channel.SendMessageAsync("```/updateusr @" + msg.Author.ToString() + " K``` /\\ for kick only\nOR\n```/updateusr @" + msg.Author.ToString() + " KB``` /\\ for kick and ban"); - break; - case "/example": - await msg.Channel.SendMessageAsync("```/example /kick```"); - break; - case "/check": - await msg.Channel.SendMessageAsync("```/check @" + msg.Author.ToString() + "```"); - break; - default: - await msg.Channel.SendMessageAsync("This is not existing command, or it does not need any arguments :confused:"); - break; - } - break; - case "/check": - try - { - if (user != "") - { - if ((DBConnector.PermCheck(msg.Author.Id, chnl.Guild.Id)[0] >= 1 || msg.Author.Id == chnl.Guild.Owner.Id)) - { - DBConnector.Prepare(user, chnl.Guild.Id); - int[] info = DBConnector.Info(user, chnl.Guild.Id); - await msg.Author.SendMessageAsync("Admin check for " + user + " from **" + chnl.Guild.Name + "**\nCurrent warnings: " + info[0] + "\nTotal warnings: " + info[2] + "\nKicks: " + info[1]); - } - else - await msg.Channel.SendMessageAsync(msg.Author.Mention + " you don't have permission for this action"); - } - else - await msg.Channel.SendMessageAsync("You need to specify user when using this command!"); - } - catch (Exception e) - { - await msg.Channel.SendMessageAsync("Cannot check user!"); - ErrorCatch(msg, e); - } - break; - } - } - else if (cmd.Contains("/") && !cmd.Contains("://")) - { - await msg.Channel.SendMessageAsync(msg.Author.Mention + " is it good idea to do this?"); - } - } - } - catch (Exception e) - { - Console.WriteLine(e); - } - } - - private Task Log(LogMessage msg) - { - Console.WriteLine(msg); - return Task.CompletedTask; - } - private async void ErrorCatch(SocketMessage msg, Exception e) - { - Console.WriteLine(e); - await msg.Channel.SendMessageAsync("Internal error occured!"); - await msg.Author.SendMessageAsync("Go to open new issue and paste text below and how you got this error"); - await msg.Author.SendMessageAsync("```" + e + "```"); - } - } -}*/ \ No newline at end of file diff --git a/source/WarnBot/WarnBot.csproj b/source/WarnBot/WarnBot.csproj index 3e02824..6d8973d 100644 --- a/source/WarnBot/WarnBot.csproj +++ b/source/WarnBot/WarnBot.csproj @@ -161,7 +161,6 @@ - From 30b524c7bf67ac3aab95518169eaa3906a6cc95d Mon Sep 17 00:00:00 2001 From: Creeperman007 Date: Thu, 22 Mar 2018 21:43:11 +0100 Subject: [PATCH 4/4] Removed packages --- source/WarnBot/WarnBot.csproj | 18 ------------------ source/WarnBot/packages.config | 7 ------- 2 files changed, 25 deletions(-) diff --git a/source/WarnBot/WarnBot.csproj b/source/WarnBot/WarnBot.csproj index 6d8973d..886a4a0 100644 --- a/source/WarnBot/WarnBot.csproj +++ b/source/WarnBot/WarnBot.csproj @@ -34,24 +34,6 @@ 4 - - ..\packages\Discord.Net.Commands.1.0.2\lib\netstandard1.1\Discord.Net.Commands.dll - - - ..\packages\Discord.Net.Core.1.0.2\lib\net45\Discord.Net.Core.dll - - - ..\packages\Discord.Net.Rest.1.0.2\lib\net45\Discord.Net.Rest.dll - - - ..\packages\Discord.Net.Rpc.1.0.2\lib\net45\Discord.Net.Rpc.dll - - - ..\packages\Discord.Net.Webhook.1.0.2\lib\netstandard1.1\Discord.Net.Webhook.dll - - - ..\packages\Discord.Net.WebSocket.1.0.2\lib\net45\Discord.Net.WebSocket.dll - ..\packages\DSharpPlus.3.2.3\lib\net46\DSharpPlus.dll diff --git a/source/WarnBot/packages.config b/source/WarnBot/packages.config index 17769d0..a9b28f5 100644 --- a/source/WarnBot/packages.config +++ b/source/WarnBot/packages.config @@ -1,12 +1,5 @@  - - - - - - -