diff --git a/.gitignore b/.gitignore index c8266e8..545a8d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -token.txt +token.env diff --git a/.vs/Discord_Simple-Embed-Bot/v16/.suo b/.vs/Discord_Simple-Embed-Bot/v16/.suo index cb47f52..4c3c127 100644 Binary files a/.vs/Discord_Simple-Embed-Bot/v16/.suo and b/.vs/Discord_Simple-Embed-Bot/v16/.suo differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..731a264 Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/Discord_Simple-Embed-Bot/CommandHandler.cs b/Discord_Simple-Embed-Bot/CommandHandler.cs index 7de233a..ca67115 100644 --- a/Discord_Simple-Embed-Bot/CommandHandler.cs +++ b/Discord_Simple-Embed-Bot/CommandHandler.cs @@ -12,22 +12,21 @@ namespace Discord_Simple_Embed_Bot { public static class CommandHandler { - public static DiscordSocketClient Client; - private static readonly SqlManager sqlManager = new SqlManager(); + public static DiscordSocketClient Client { get; set; } - static readonly Dictionary _commands = new() + public static readonly Dictionary CommandList = new() { - { "help", new Command { Fun = Help, Desc = "Lists all commands", Usage = "" } }, - { "prefix", new Command { Fun = ChangePrefix, Desc = "Changes prefix", Usage = "" } }, + { "help", new Command { Fun = Commands.Help, Desc = "Lists all commands", Usage = "" } }, + { "prefix", new Command { Fun = Commands.ChangePrefix, Desc = "Changes prefix", Usage = "" } }, { "new", new Command { - Fun = CreateEmbed, + Fun = Commands.CreateEmbed, Desc = "Creats new embed", Usage = "\n<hex color>\n<description>\n<field>", Example = "Title" - + "\n#FFFFFF" + + "\n#EEEEEE" + "\nDescription" + "\n++Field name" + "\nField content" @@ -42,15 +41,17 @@ namespace Discord_Simple_Embed_Bot public static async Task HandleCommand(SocketMessage messageParam) { SocketUserMessage message = messageParam as SocketUserMessage; - if (message == null) return; + if (message is null) return; int argPos = 0; string prefix = PrefixFromMessage(message); if (message.MentionedUsers.Any(x => x.Discriminator == Client.CurrentUser.Discriminator)) { - EmbedBuilder eb = new EmbedBuilder(); - eb.Color = Color.Blue; - eb.Description = $"Use `{prefix} help` to list all commands."; + EmbedBuilder eb = new EmbedBuilder + { + Color = Color.Blue, + Description = $"Use `{prefix} help` to list all commands." + }; await message.Channel.SendMessageAsync("", false, eb.Build()); return; } @@ -65,12 +66,12 @@ namespace Discord_Simple_Embed_Bot if (command.Contains(" ")) command = command.Substring(0, command.IndexOf(" ")); - if (_commands.ContainsKey(command)) + if (CommandList.ContainsKey(command)) { SocketGuildUser socketGuildUser = message.Author as SocketGuildUser; if (socketGuildUser.GuildPermissions.Administrator) { - await _commands[command].Fun(message); + await CommandList[command].Fun(message); } } else @@ -79,17 +80,17 @@ namespace Discord_Simple_Embed_Bot } } - static string PrefixFromMessage(SocketUserMessage message) + public static string PrefixFromMessage(SocketUserMessage message) { - string prefix = sqlManager.GetData((message.Channel as SocketGuildChannel).Guild.Id, 'p').Result; + string prefix = SqlManager.GetData((message.Channel as SocketGuildChannel).Guild.Id, 'p').Result; GC.Collect(); GC.WaitForPendingFinalizers(); return prefix; } - static string[] CheckCommandArgs(string content, int min, int max, string prefix) + public static string[] CheckCommandArgs(string content, int min, int max, string prefix) { content = content.Trim(); content = content.Remove(0, prefix.Length).Trim(); @@ -107,130 +108,7 @@ namespace Discord_Simple_Embed_Bot return null; } - static public async Task Help(SocketUserMessage message) - { - EmbedBuilder eb = new EmbedBuilder(); - eb.Color = Color.Blue; - string[] args = CheckCommandArgs(message.Content, 0, 1, PrefixFromMessage(message)); - if (args == null) - { - eb.Title = "Commands:"; - - foreach (var item in _commands) - { - eb.AddField(item.Key, item.Value.Desc); - } - } - else if (args.Length > 0) - { - if (_commands.ContainsKey(args[0].ToLower())) - { - eb.Title = args[0].ToLower(); - eb.AddField("Parameters", _commands[args[0].ToLower()].Usage); - if (!string.IsNullOrWhiteSpace(_commands[args[0].ToLower()].Example)) - { - eb.AddField("Example", $"```{PrefixFromMessage(message)} {args[0].ToLower()} {_commands[args[0].ToLower()].Example}```"); - } - } - else - { - eb.WithDescription("Command not found!"); - } - } - await message.Channel.SendMessageAsync("", false, eb.Build()); - } - - static public async Task ChangePrefix(SocketUserMessage message) - { - string prefix = ""; - if (message.Content.Contains(" ")) - prefix = message.Content[message.Content.LastIndexOf(" ")..].Trim(); - if (prefix.Length <= 0) - { - await message.Channel.SendMessageAsync($"Prefix not valid!"); - return; - } - - await sqlManager.SetData((message.Channel as SocketGuildChannel).Guild.Id, prefix, 'p'); - await message.Channel.SendMessageAsync($"Prefix changed to: `{prefix}`"); - } - - static public async Task CreateEmbed(SocketUserMessage message) - { - string prefix = PrefixFromMessage(message); - string content = message.Content[prefix.Length..].Trim()[3..]; - string[] lines = content.Split("\n"); - - await message.DeleteAsync(); - - EmbedBuilder eb = new EmbedBuilder(); - - - eb.Title = lines[0]; - - if (lines.Length > 1) - { - try - { - if (!lines[1].Contains("#")) - lines[1] = "#" + lines[1]; - System.Drawing.Color col = (System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(lines[1]); - eb.Color = new Color(col.R, col.G, col.B); - } - catch { } - } - if (lines.Length > 2) - { - eb.Description = lines[2]; - } - - EmbedFieldBuilder efb = null; - foreach (string line in lines.Skip(3)) - { - if (line.StartsWith("++")) - { - if (efb is not null) - eb.AddField(efb); - efb = new EmbedFieldBuilder(); - efb.IsInline = false; - efb.Name = line.Remove(0, 2); - } - else if (line.StartsWith("--")) - { - if (efb is not null) - eb.AddField(efb); - efb = new EmbedFieldBuilder(); - efb.IsInline = true; - efb.Name = line.Remove(0, 2); - } - else if (efb is not null && !string.IsNullOrWhiteSpace(line)) - { - efb.Value += line + "\n"; - } - } - - if (eb is not null) - eb.AddField(efb); - - for (int i = 0; i < eb.Fields.Count; i++) - { - if (string.IsNullOrWhiteSpace(eb.Fields[i].Value as string)) - { - eb.Fields[i].Value = "-"; - } - } - - try - { - await message.Channel.SendMessageAsync("", false, eb.Build()); - } - catch (Exception ex) - { - await Logging.Log(new LogMessage(LogSeverity.Debug, "CMD Handler", "", ex)); - } - } - - class Command + public class Command { public string Desc { get; set; } public string Usage { get; set; } diff --git a/Discord_Simple-Embed-Bot/Commands.cs b/Discord_Simple-Embed-Bot/Commands.cs new file mode 100644 index 0000000..58f4194 --- /dev/null +++ b/Discord_Simple-Embed-Bot/Commands.cs @@ -0,0 +1,134 @@ +using Discord; +using Discord.WebSocket; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Discord_Simple_Embed_Bot +{ + static class Commands + { + static public async Task Help(SocketUserMessage message) + { + EmbedBuilder eb = new EmbedBuilder(); + eb.Color = Color.Blue; + string[] args = CommandHandler.CheckCommandArgs(message.Content, 0, 1, CommandHandler.PrefixFromMessage(message)); + if (args == null) + { + eb.Title = "Commands:"; + + foreach (var item in CommandHandler.CommandList) + { + eb.AddField(item.Key, item.Value.Desc); + } + } + else if (args.Length > 0) + { + if (CommandHandler.CommandList.ContainsKey(args[0].ToLower())) + { + eb.Title = args[0].ToLower(); + eb.AddField("Parameters", CommandHandler.CommandList[args[0].ToLower()].Usage); + if (!string.IsNullOrWhiteSpace(CommandHandler.CommandList[args[0].ToLower()].Example)) + { + eb.AddField("Example", $"```{CommandHandler.PrefixFromMessage(message)} {args[0].ToLower()} {CommandHandler.CommandList[args[0].ToLower()].Example}```"); + } + } + else + { + eb.WithDescription("Command not found!"); + } + } + await message.Channel.SendMessageAsync("", false, eb.Build()); + } + + static public async Task ChangePrefix(SocketUserMessage message) + { + string prefix = ""; + if (message.Content.Contains(" ")) + prefix = message.Content[message.Content.LastIndexOf(" ")..].Trim(); + if (prefix.Length <= 0) + { + await message.Channel.SendMessageAsync($"Prefix not valid!"); + return; + } + + await SqlManager.SetData((message.Channel as SocketGuildChannel).Guild.Id, prefix, 'p'); + await message.Channel.SendMessageAsync($"Prefix changed to: `{prefix}`"); + } + + static public async Task CreateEmbed(SocketUserMessage message) + { + string prefix = CommandHandler.PrefixFromMessage(message); + string content = message.Content[prefix.Length..].Trim()[3..]; + string[] lines = content.Split("\n"); + + await message.DeleteAsync(); + + EmbedBuilder eb = new EmbedBuilder(); + eb.Title = lines[0]; + + if (lines.Length > 1) + { + try + { + if (!lines[1].Contains("#")) + lines[1] = "#" + lines[1]; + System.Drawing.Color col = (System.Drawing.Color)new System.Drawing.ColorConverter().ConvertFromString(lines[1]); + eb.Color = new Color(col.R, col.G, col.B); + } + catch { } + } + if (lines.Length > 2) + { + eb.Description = lines[2]; + } + + EmbedFieldBuilder efb = null; + foreach (string line in lines.Skip(3)) + { + if (line.StartsWith("++")) + { + if (efb is not null) + eb.AddField(efb); + efb = new EmbedFieldBuilder(); + efb.IsInline = false; + efb.Name = line.Remove(0, 2); + } + else if (line.StartsWith("--")) + { + if (efb is not null) + eb.AddField(efb); + efb = new EmbedFieldBuilder(); + efb.IsInline = true; + efb.Name = line.Remove(0, 2); + } + else if (efb is not null && !string.IsNullOrWhiteSpace(line)) + { + efb.Value += line + "\n"; + } + } + + if (eb is not null) + eb.AddField(efb); + + for (int i = 0; i < eb.Fields.Count; i++) + { + if (string.IsNullOrWhiteSpace(eb.Fields[i].Value as string)) + { + eb.Fields[i].Value = "-"; + } + } + + try + { + await message.Channel.SendMessageAsync("", false, eb.Build()); + } + catch (Exception ex) + { + await Logging.Log(new LogMessage(LogSeverity.Debug, "CMD Handler", "", ex)); + } + } + } +} diff --git a/Discord_Simple-Embed-Bot/Program.cs b/Discord_Simple-Embed-Bot/Program.cs index 17d68b7..294f937 100644 --- a/Discord_Simple-Embed-Bot/Program.cs +++ b/Discord_Simple-Embed-Bot/Program.cs @@ -13,7 +13,7 @@ namespace Discord_Simple_Embed_Bot public async Task MainAsync() { - string tokenPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "token.txt"); + string tokenPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "token.env"); if (!File.Exists(tokenPath)) File.Create(tokenPath).Close(); @@ -65,14 +65,14 @@ namespace Discord_Simple_Embed_Bot private static ConsoleColor GetColor(LogSeverity logSeverity) { - switch (logSeverity) + return logSeverity switch { - case LogSeverity.Critical: return ConsoleColor.DarkRed; - case LogSeverity.Error: return ConsoleColor.Red; - case LogSeverity.Warning: return ConsoleColor.DarkYellow; - case LogSeverity.Info: return ConsoleColor.Green; - default: return ConsoleColor.White; - } + LogSeverity.Critical => ConsoleColor.DarkRed, + LogSeverity.Error => ConsoleColor.Red, + LogSeverity.Warning => ConsoleColor.DarkYellow, + LogSeverity.Info => ConsoleColor.Green, + _ => ConsoleColor.White, + }; } } } diff --git a/Discord_Simple-Embed-Bot/SqlManager.cs b/Discord_Simple-Embed-Bot/SqlManager.cs index 0ad4908..1de4462 100644 --- a/Discord_Simple-Embed-Bot/SqlManager.cs +++ b/Discord_Simple-Embed-Bot/SqlManager.cs @@ -7,82 +7,83 @@ using System.Threading.Tasks; namespace Discord_Simple_Embed_Bot { - class SqlManager { - readonly string sqlConnectionString; - readonly string databasePath; - public SqlManager() + static class SqlManager { - databasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "MainDatabase.sqlite"); - sqlConnectionString = "Data Source = " + databasePath + "; Version = 3"; - - //Create SQLite database file - if (!File.Exists(databasePath)) + static readonly string sqlConnectionString; + static readonly string databasePath; + static SqlManager() { - SQLiteConnection.CreateFile(databasePath); + databasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "MainDatabase.sqlite"); + sqlConnectionString = "Data Source = " + databasePath + "; Version = 3"; + + //Create SQLite database file + if (!File.Exists(databasePath)) + { + SQLiteConnection.CreateFile(databasePath); + } + + string sqlCommand1 = "CREATE TABLE IF NOT EXISTS Events (ServerId INTEGER,ChannelID INTEGER,EndDate TEXT)"; + string sqlCommand2 = "CREATE TABLE IF NOT EXISTS Settings (ServerId INTEGER PRIMARY KEY,Data TEXT)"; + + SQLiteConnection m_dbConnection = new SQLiteConnection(sqlConnectionString); + m_dbConnection.Open(); + + SQLiteCommand command = new SQLiteCommand(m_dbConnection); + command.CommandText = sqlCommand1; + command.ExecuteNonQuery(); + command.CommandText = sqlCommand2; + command.ExecuteNonQuery(); + + m_dbConnection.Close(); } - string sqlCommand1 = "CREATE TABLE IF NOT EXISTS Events (ServerId INTEGER,ChannelID INTEGER,EndDate TEXT)"; - string sqlCommand2 = "CREATE TABLE IF NOT EXISTS Settings (ServerId INTEGER PRIMARY KEY,Data TEXT)"; - - SQLiteConnection m_dbConnection = new SQLiteConnection(sqlConnectionString); - m_dbConnection.Open(); - - SQLiteCommand command = new SQLiteCommand(m_dbConnection); - command.CommandText = sqlCommand1; - command.ExecuteNonQuery(); - command.CommandText = sqlCommand2; - command.ExecuteNonQuery(); - - m_dbConnection.Close(); - } - - public async Task SetData(ulong serverId, string data, char type) - { - await Task.Run(() => + public static async Task SetData(ulong serverId, string data, char type) { - SQLiteConnection dbConnection = new SQLiteConnection(sqlConnectionString); - SQLiteCommand command = new SQLiteCommand(); - - command.CommandText = "INSERT OR REPLACE INTO Settings (ServerId,Data) values (@serverId, @prefix)"; - - command.Parameters.Add(new SQLiteParameter("@serverId", serverId + type)); - command.Parameters.Add(new SQLiteParameter("@prefix", data)); - - - dbConnection.Open(); - command.Connection = dbConnection; - command.ExecuteNonQuery(); - dbConnection.Close(); - }); - } - - public async Task<string> GetData(ulong serverId, char type) - { - return await Task<string>.Run(() => - { - string data = null; - using (SQLiteConnection dbConnection = new SQLiteConnection(sqlConnectionString)) + await Task.Run(() => { + SQLiteConnection dbConnection = new SQLiteConnection(sqlConnectionString); + SQLiteCommand command = new SQLiteCommand("INSERT OR REPLACE INTO Settings (ServerId,Data) values (@serverId, @prefix)", dbConnection); + + command.Parameters.Add(new SQLiteParameter("@serverId", serverId + type)); + command.Parameters.Add(new SQLiteParameter("@prefix", data)); + dbConnection.Open(); - SQLiteCommand command = new SQLiteCommand($"SELECT * FROM Settings WHERE ServerId=@type", dbConnection); - command.Parameters.Add(new SQLiteParameter("@type", serverId + type)); - SQLiteDataReader dr = command.ExecuteReader(); - if (dr.Read()) - { - data = dr.GetString(1); - } - + command.Connection = dbConnection; + command.ExecuteNonQuery(); dbConnection.Close(); - } - if (data == null) + }); + } + + public static async Task<string> GetData(ulong serverId, char type) + { + return await Task<string>.Run(() => { - switch (type) + string data = null; + using (SQLiteConnection dbConnection = new SQLiteConnection(sqlConnectionString)) { - case 'p': return "eb!"; + + SQLiteCommand command = new SQLiteCommand($"SELECT * FROM Settings WHERE ServerId=@type", dbConnection); + + command.Parameters.Add(new SQLiteParameter("@type", serverId + type)); + + dbConnection.Open(); + SQLiteDataReader dr = command.ExecuteReader(); + if (dr.Read()) + { + data = dr.GetString(1); + } + + dbConnection.Close(); } - } - return data; - }); + if (data == null) + { + switch (type) + { + case 'p': return "eb!"; + } + } + return data; + }); + } } } -} diff --git a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.dll b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.dll index e1b6fbc..ea62f05 100644 Binary files a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.dll and b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.dll differ diff --git a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.pdb b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.pdb index c7a9547..a9384a1 100644 Binary files a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.pdb and b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/Discord_Simple-Embed-Bot.pdb differ diff --git a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/MainDatabase.sqlite b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/MainDatabase.sqlite index f598fc7..d3e4e5d 100644 Binary files a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/MainDatabase.sqlite and b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/MainDatabase.sqlite differ diff --git a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll index e4596d6..4ac3dd5 100644 Binary files a/Discord_Simple-Embed-Bot/bin/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll and b/Discord_Simple-Embed-Bot/bin/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll differ diff --git a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csproj.CoreCompileInputs.cache b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csproj.CoreCompileInputs.cache index ea9074e..68c3d7d 100644 --- a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csproj.CoreCompileInputs.cache +++ b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -1eb6d2e13bbb6a533f4118a46962c1047e1cffb0 +fce56c50dd4c34795ae563c97b2bd30baf9ffcb4 diff --git a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache index ddc787d..ea680ca 100644 Binary files a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache and b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache differ diff --git a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.dll b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.dll index e1b6fbc..ea62f05 100644 Binary files a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.dll and b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.dll differ diff --git a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.pdb b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.pdb index c7a9547..a9384a1 100644 Binary files a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.pdb and b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/Discord_Simple-Embed-Bot.pdb differ diff --git a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll index e4596d6..4ac3dd5 100644 Binary files a/Discord_Simple-Embed-Bot/obj/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll and b/Discord_Simple-Embed-Bot/obj/Debug/net5.0/ref/Discord_Simple-Embed-Bot.dll differ diff --git a/Discord_Simple-Embed-Bot/obj/Release/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache b/Discord_Simple-Embed-Bot/obj/Release/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache index f2355c2..4ccae32 100644 Binary files a/Discord_Simple-Embed-Bot/obj/Release/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache and b/Discord_Simple-Embed-Bot/obj/Release/net5.0/Discord_Simple-Embed-Bot.csprojAssemblyReference.cache differ