diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2fc441b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/DiscordBot/bin/Debug/net5.0/DiscordBot.dll", + "args": [], + "cwd": "${workspaceFolder}/DiscordBot", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/solution-explorer/class.cs-template b/.vscode/solution-explorer/class.cs-template new file mode 100644 index 0000000..20c7109 --- /dev/null +++ b/.vscode/solution-explorer/class.cs-template @@ -0,0 +1,5 @@ +namespace {{namespace}}; + +public class {{name}} +{ +} diff --git a/.vscode/solution-explorer/class.ts-template b/.vscode/solution-explorer/class.ts-template new file mode 100644 index 0000000..ff2edef --- /dev/null +++ b/.vscode/solution-explorer/class.ts-template @@ -0,0 +1,3 @@ +export class {{name}} { + +} \ No newline at end of file diff --git a/.vscode/solution-explorer/class.vb-template b/.vscode/solution-explorer/class.vb-template new file mode 100644 index 0000000..38ef67f --- /dev/null +++ b/.vscode/solution-explorer/class.vb-template @@ -0,0 +1,9 @@ +Imports System + +Namespace {{namespace}} + + Public Class {{name}} + + End Class + +End Namespace diff --git a/.vscode/solution-explorer/default.ts-template b/.vscode/solution-explorer/default.ts-template new file mode 100644 index 0000000..04af870 --- /dev/null +++ b/.vscode/solution-explorer/default.ts-template @@ -0,0 +1,3 @@ +export default {{name}} { + +} \ No newline at end of file diff --git a/.vscode/solution-explorer/enum.cs-template b/.vscode/solution-explorer/enum.cs-template new file mode 100644 index 0000000..1ded8c0 --- /dev/null +++ b/.vscode/solution-explorer/enum.cs-template @@ -0,0 +1,5 @@ +namespace {{namespace}}; + +public enum {{name}} +{ +} diff --git a/.vscode/solution-explorer/interface.cs-template b/.vscode/solution-explorer/interface.cs-template new file mode 100644 index 0000000..bf25cbb --- /dev/null +++ b/.vscode/solution-explorer/interface.cs-template @@ -0,0 +1,5 @@ +namespace {{namespace}}; + +public interface {{name}} +{ +} diff --git a/.vscode/solution-explorer/interface.ts-template b/.vscode/solution-explorer/interface.ts-template new file mode 100644 index 0000000..3ea404b --- /dev/null +++ b/.vscode/solution-explorer/interface.ts-template @@ -0,0 +1,3 @@ +export interface {{name}} { + +} \ No newline at end of file diff --git a/.vscode/solution-explorer/template-list.json b/.vscode/solution-explorer/template-list.json new file mode 100644 index 0000000..2849622 --- /dev/null +++ b/.vscode/solution-explorer/template-list.json @@ -0,0 +1,46 @@ +{ + "templates": [ + { + "name": "Class", + "extension": "cs", + "file": "./class.cs-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Interface", + "extension": "cs", + "file": "./interface.cs-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Enum", + "extension": "cs", + "file": "./enum.cs-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Class", + "extension": "ts", + "file": "./class.ts-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Interface", + "extension": "ts", + "file": "./interface.ts-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Default", + "extension": "ts", + "file": "./default.ts-template", + "parameters": "./template-parameters.js" + }, + { + "name": "Class", + "extension": "vb", + "file": "./class.vb-template", + "parameters": "./template-parameters.js" + } + ] +} \ No newline at end of file diff --git a/.vscode/solution-explorer/template-parameters.js b/.vscode/solution-explorer/template-parameters.js new file mode 100644 index 0000000..daba8b2 --- /dev/null +++ b/.vscode/solution-explorer/template-parameters.js @@ -0,0 +1,17 @@ +var path = require("path"); + +module.exports = function(filename, projectPath, folderPath) { + var namespace = "Unknown"; + if (projectPath) { + namespace = path.basename(projectPath, path.extname(projectPath)); + if (folderPath) { + namespace += "." + folderPath.replace(path.dirname(projectPath), "").substring(1).replace(/[\\\/]/g, "."); + } + namespace = namespace.replace(/[\\\-]/g, "_"); + } + + return { + namespace: namespace, + name: path.basename(filename, path.extname(filename)) + } +}; \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..adf4d42 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/DiscordBot/DiscordBot.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/DiscordBot/DiscordBot.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/DiscordBot/DiscordBot.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/BUILDS/net5.0/CMD_LevelingSystem.dll b/BUILDS/net5.0/CMD_LevelingSystem.dll index 7fd2b21..61627ef 100644 Binary files a/BUILDS/net5.0/CMD_LevelingSystem.dll and b/BUILDS/net5.0/CMD_LevelingSystem.dll differ diff --git a/BUILDS/net5.0/CMD_Utils.dll b/BUILDS/net5.0/CMD_Utils.dll index bb1fc45..0980936 100644 Binary files a/BUILDS/net5.0/CMD_Utils.dll and b/BUILDS/net5.0/CMD_Utils.dll differ diff --git a/BUILDS/net5.0/PluginManager.dll b/BUILDS/net5.0/PluginManager.dll index b24bd2d..1bc4197 100644 Binary files a/BUILDS/net5.0/PluginManager.dll and b/BUILDS/net5.0/PluginManager.dll differ diff --git a/BUILDS/net5.0/ref/CMD_LevelingSystem.dll b/BUILDS/net5.0/ref/CMD_LevelingSystem.dll index 46b3c6f..03da7d1 100644 Binary files a/BUILDS/net5.0/ref/CMD_LevelingSystem.dll and b/BUILDS/net5.0/ref/CMD_LevelingSystem.dll differ diff --git a/BUILDS/net5.0/ref/CMD_Utils.dll b/BUILDS/net5.0/ref/CMD_Utils.dll index 782729d..5a716e7 100644 Binary files a/BUILDS/net5.0/ref/CMD_Utils.dll and b/BUILDS/net5.0/ref/CMD_Utils.dll differ diff --git a/CMD_LevelingSystem/Commands/level.cs b/CMD_LevelingSystem/Commands/level.cs index ef5f8dc..7bea3e4 100644 --- a/CMD_LevelingSystem/Commands/level.cs +++ b/CMD_LevelingSystem/Commands/level.cs @@ -20,6 +20,8 @@ public class level : DBCommand public bool canUseServer => true; + public bool requireAdmin => false; + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { diff --git a/CMD_Utils/Echo.cs b/CMD_Utils/Echo.cs index 90286a8..fdf7fe1 100644 --- a/CMD_Utils/Echo.cs +++ b/CMD_Utils/Echo.cs @@ -14,6 +14,8 @@ internal class Echo : DBCommand public bool canUseDM => true; public bool canUseServer => true; + public bool requireAdmin => false; + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { string m = message.Content.Substring(6); diff --git a/CMD_Utils/FlipCoin.cs b/CMD_Utils/FlipCoin.cs index 04446f4..e84f83b 100644 --- a/CMD_Utils/FlipCoin.cs +++ b/CMD_Utils/FlipCoin.cs @@ -23,6 +23,8 @@ namespace CMD_Utils public bool canUseServer => true; + public bool requireAdmin => false; + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { System.Random random = new System.Random(); diff --git a/CMD_Utils/Poll.cs b/CMD_Utils/Poll.cs new file mode 100644 index 0000000..bf81df5 --- /dev/null +++ b/CMD_Utils/Poll.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Discord; +using Discord.Commands; +using Discord.WebSocket; + +using PluginManager.Interfaces; + +namespace CMD_Utils +{ + public class Poll : DBCommand + { + public string Command => "poll"; + + public string Description => "Create a poll with options"; + + public string Usage => "poll [This-is-question] [This-is-answer-1] [This-is-answer-2] ... "; + + public bool canUseDM => false; + + public bool canUseServer => true; + + public bool requireAdmin => true; + + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + { + if (isDM) return; + string question = message.Content.Split(' ')[1].Replace('-', ' '); + string[] answers = PluginManager.Others.Functions.MergeStrings(message.Content.Split(' '), 2).Split(' '); + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.Title = question; + int len = answers.Length; + for (int i = 0; i < len; i++) + embedBuilder.AddField($"Answer {i + 1}", answers[i].Replace('-', ' '), true); + var msg = await context.Channel.SendMessageAsync(embed: embedBuilder.Build()); + + List emotes = new List(); + emotes.Add(Emoji.Parse(":one:")); + emotes.Add(Emoji.Parse(":two:")); + emotes.Add(Emoji.Parse(":three:")); + emotes.Add(Emoji.Parse(":four:")); + emotes.Add(Emoji.Parse(":five:")); + emotes.Add(Emoji.Parse(":six:")); + + for (int i = 0; i < len; i++) + await msg.AddReactionAsync(emotes[i]); + } + } +} diff --git a/CMD_Utils/Random.cs b/CMD_Utils/Random.cs index 4794634..4075588 100644 --- a/CMD_Utils/Random.cs +++ b/CMD_Utils/Random.cs @@ -13,6 +13,7 @@ public class Random : DBCommand public bool canUseDM => true; public bool canUseServer => true; + public bool requireAdmin => false; public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { diff --git a/DiscordBot/Data/Languages/Romanian.lng b/DiscordBot/Data/Languages/Romanian.lng new file mode 100644 index 0000000..0535cd0 --- /dev/null +++ b/DiscordBot/Data/Languages/Romanian.lng @@ -0,0 +1,16 @@ +######################## +# # +# Language Config File # +# Romanian # +# # +######################## + +#Language name +LANGUAGE_NAME=Romanian + +#Language on plugins +PLUGIN_LOADING_START=Incarcare extensii... +COMMAND_LOAD_SUCCESS=[COMANDA] Comanda {0} a fost incarcata cu succes +COMMAND_LOAD_FAIL=[COMANDA] Comanda {0} nu a fost incarcata pentru ca {1} +EVENT_LOAD_SUCCESS=[EVENIMENT] Evenimentul {0} a fost initializat cu succes +EVENT_LOAD_FAIL=[EVENIMENT] Evenimentul {0} nu a fost initializat pentru ca {1} \ No newline at end of file diff --git a/DiscordBot/Data/Plugins/Commands/CMD_LevelingSystem.dll b/DiscordBot/Data/Plugins/Commands/CMD_LevelingSystem.dll new file mode 100644 index 0000000..61627ef Binary files /dev/null and b/DiscordBot/Data/Plugins/Commands/CMD_LevelingSystem.dll differ diff --git a/DiscordBot/Data/Plugins/Commands/CMD_Utils.dll b/DiscordBot/Data/Plugins/Commands/CMD_Utils.dll new file mode 100644 index 0000000..0980936 Binary files /dev/null and b/DiscordBot/Data/Plugins/Commands/CMD_Utils.dll differ diff --git a/DiscordBot/Data/Plugins/Events/EVE_LevelingSystem.dll b/DiscordBot/Data/Plugins/Events/EVE_LevelingSystem.dll new file mode 100644 index 0000000..5c2caaa Binary files /dev/null and b/DiscordBot/Data/Plugins/Events/EVE_LevelingSystem.dll differ diff --git a/DiscordBot/Data/Plugins/Events/Leveling System.dll b/DiscordBot/Data/Plugins/Events/Leveling System.dll new file mode 100644 index 0000000..5c2caaa Binary files /dev/null and b/DiscordBot/Data/Plugins/Events/Leveling System.dll differ diff --git a/DiscordBot/Data/Plugins/Events/StartupEvents.dll b/DiscordBot/Data/Plugins/Events/StartupEvents.dll new file mode 100644 index 0000000..ad9fca9 Binary files /dev/null and b/DiscordBot/Data/Plugins/Events/StartupEvents.dll differ diff --git a/DiscordBot/Data/Resources/DiscordBotCore.data b/DiscordBot/Data/Resources/DiscordBotCore.data new file mode 100644 index 0000000..bbbe752 --- /dev/null +++ b/DiscordBot/Data/Resources/DiscordBotCore.data @@ -0,0 +1,2 @@ +BOT_TOKEN NzEzNzczNTM1MTIxNzY4NTU5.Xsk_aA.5ZDlK_bBm_7kqTALuuzhMyjDbXI +BOT_PREFIX ; diff --git a/DiscordBot/Data/Resources/Language.txt b/DiscordBot/Data/Resources/Language.txt new file mode 100644 index 0000000..0c011e3 --- /dev/null +++ b/DiscordBot/Data/Resources/Language.txt @@ -0,0 +1 @@ +Language=English \ No newline at end of file diff --git a/DiscordBot/Data/Resources/LevelingSystem/462627303479050242.data b/DiscordBot/Data/Resources/LevelingSystem/462627303479050242.data new file mode 100644 index 0000000..2dafd9c --- /dev/null +++ b/DiscordBot/Data/Resources/LevelingSystem/462627303479050242.data @@ -0,0 +1 @@ +Level=1,EXP=6,REXP=75 \ No newline at end of file diff --git a/DiscordBot/Data/Resources/LevelingSystem/477015304724021248.data b/DiscordBot/Data/Resources/LevelingSystem/477015304724021248.data new file mode 100644 index 0000000..7213e50 --- /dev/null +++ b/DiscordBot/Data/Resources/LevelingSystem/477015304724021248.data @@ -0,0 +1 @@ +Level=2,EXP=10,REXP=78 \ No newline at end of file diff --git a/DiscordBot/Data/Resources/StartupEvents/LoginEvent.txt b/DiscordBot/Data/Resources/StartupEvents/LoginEvent.txt new file mode 100644 index 0000000..c231e33 --- /dev/null +++ b/DiscordBot/Data/Resources/StartupEvents/LoginEvent.txt @@ -0,0 +1,7 @@ +Enabled=False + +Dynamic Title=False +#For dynamic title add titles like this: +#Title=Hello,World,Test,Test2 +Title=Hello World +Dynamic Title Change Rate=3501 diff --git a/DiscordBot/Data/Resources/StartupEvents/UserJoinEvent.txt b/DiscordBot/Data/Resources/StartupEvents/UserJoinEvent.txt new file mode 100644 index 0000000..6a4f030 --- /dev/null +++ b/DiscordBot/Data/Resources/StartupEvents/UserJoinEvent.txt @@ -0,0 +1,13 @@ +Enabled=True +Embed=True +#Available placeholders: +#{user.Name} => Username of the user +#{time.date} => Current Date +#{time.time} => Current time (hh:mm::ss) +MessageTitle = Welcome {user.Name} +MessageDescription=Embed description +MessageField1Title=Custom Title +MessageFiled1Text=Custom Filed 1 text +MessageField2Title=Custom Title +MessageFiled2Text=Custom Filed 2 text +MessageFooter=Today: {time.date} at {time.time} \ No newline at end of file diff --git a/DiscordBot/Discord/Commands/Help.cs b/DiscordBot/Discord/Commands/Help.cs index 593b9f5..92f4b25 100644 --- a/DiscordBot/Discord/Commands/Help.cs +++ b/DiscordBot/Discord/Commands/Help.cs @@ -3,6 +3,7 @@ using Discord.WebSocket; using PluginManager.Loaders; using PluginManager.Interfaces; +using PluginManager.Others.Permissions; namespace PluginManager.Commands { @@ -17,21 +18,47 @@ namespace PluginManager.Commands public bool canUseDM => true; public bool canUseServer => true; + public bool requireAdmin => false; + public void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) { - if (isDM) + bool isAdmin = ((SocketGuildUser)message.Author).isAdmin(); + if (isAdmin) { - foreach (DBCommand p in PluginLoader.Plugins!) - if (p.canUseDM) - context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + if (isDM) + { + foreach (DBCommand p in PluginLoader.Plugins!) + if (p.canUseDM) + if (p.requireAdmin) + context.Channel.SendMessageAsync("[ADMIN] " + p.Usage + "\t" + p.Description); + else context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + } + else + { + foreach (DBCommand p in PluginLoader.Plugins!) + if (p.canUseServer) + if (p.requireAdmin) + context.Channel.SendMessageAsync("[ADMIN] " + p.Usage + "\t" + p.Description); + else context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + } } else { - foreach (DBCommand p in PluginLoader.Plugins!) - if (p.canUseServer) - context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + if (isDM) + { + foreach (DBCommand p in PluginLoader.Plugins!) + if (p.canUseDM && !p.requireAdmin) + context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + } + else + { + foreach (DBCommand p in PluginLoader.Plugins!) + if (p.canUseServer && !p.requireAdmin) + context.Channel.SendMessageAsync(p.Usage + "\t" + p.Description); + } } + } } } \ No newline at end of file diff --git a/DiscordBot/Discord/Commands/Settings.cs b/DiscordBot/Discord/Commands/Settings.cs new file mode 100644 index 0000000..04ed19c --- /dev/null +++ b/DiscordBot/Discord/Commands/Settings.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Discord; +using Discord.Commands; +using Discord.WebSocket; + +using PluginManager.Core; +using PluginManager.Interfaces; +using PluginManager.Others; +using PluginManager.Others.Permissions; + +namespace DiscordBot.Discord.Commands +{ + class Settings : DBCommand + { + public string Command => "set"; + + public string Description => "This command allows you change all settings. Use \"set help\" to show details"; + + public string Usage => "set [keyword] [new Value]"; + + public bool canUseDM => true; + public bool canUseServer => true; + public bool requireAdmin => true; + + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + { + var channel = message.Channel; + try + { + + string content = message.Content; + string[] data = content.Split(' '); + string keyword = data[1]; + if (keyword.ToLower() == "help") + { + await channel.SendMessageAsync("set token [new value] -- set the value of the new token (require restart)"); + await channel.SendMessageAsync("set prefix [new value] -- set the value of the new preifx (require restart)"); + + return; + } + + switch (keyword.ToLower()) + { + case "token": + if (data.Length != 3) + { + await channel.SendMessageAsync("Invalid token !"); + return; + } + Functions.WriteToSettings("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN", data[2], '\t'); + break; + case "prefix": + if (data.Length != 3) + { + await channel.SendMessageAsync("Invalid token !"); + return; + } + Functions.WriteToSettings("./Data/Resources/DiscordBotCore.data", "BOT_PREFIX", data[2], '\t'); + break; + default: + return; + } + + await channel.SendMessageAsync("Restart required ..."); + } + catch + { + await channel.SendMessageAsync("Unknown usage to this command !\nUsage: " + Usage); + } + + } + } +} diff --git a/DiscordBot/Discord/Core/Boot.cs b/DiscordBot/Discord/Core/Boot.cs index a55bd41..0e47f34 100644 --- a/DiscordBot/Discord/Core/Boot.cs +++ b/DiscordBot/Discord/Core/Boot.cs @@ -46,13 +46,13 @@ namespace PluginManager.Core public async Task ShutDown() { if (client == null) return; + await client.LogoutAsync(); await client.StopAsync(); } private void CommonTasks() { - if (client == null) - return; + if (client == null) return; client.LoggedOut += Client_LoggedOut; client.Log += Log; client.LoggedIn += LoggedIn; diff --git a/DiscordBot/Discord/Core/CommandHandler.cs b/DiscordBot/Discord/Core/CommandHandler.cs index b141584..d2621fc 100644 --- a/DiscordBot/Discord/Core/CommandHandler.cs +++ b/DiscordBot/Discord/Core/CommandHandler.cs @@ -5,10 +5,12 @@ using PluginManager.Interfaces; using System.Reflection; using PluginManager.Others; +using PluginManager.Others.Permissions; using PluginManager.Loaders; using System.Threading.Tasks; using System.Linq; +using Discord; namespace PluginManager.Core { @@ -18,6 +20,9 @@ namespace PluginManager.Core private readonly CommandService commandService; private readonly string botPrefix; + internal static bool awaitRestartOnSetCommand = false; + internal static SocketUser? RestartOnSetCommandCaster = null; + public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix) { this.client = client; @@ -51,7 +56,26 @@ namespace PluginManager.Core } if (!(message.HasStringPrefix(botPrefix, ref argPos) || message.Author.IsBot)) - return; + if (message.Author.IsBot) return; + else + { + if (awaitRestartOnSetCommand && RestartOnSetCommandCaster is not null) + { + if (message.Content.ToLower() == "yes") + { + if (!(((SocketGuildUser)message.Author).hasPermission(GuildPermission.Administrator))) + { + await message.Channel.SendMessageAsync("You do not have permission to use this command !"); + awaitRestartOnSetCommand = false; + RestartOnSetCommandCaster = null; + return; + } + var fileName = Assembly.GetExecutingAssembly().Location; + System.Diagnostics.Process.Start(fileName); + } + } + return; + } var context = new SocketCommandContext(client, message); @@ -63,19 +87,51 @@ namespace PluginManager.Core DBCommand? plugin = PluginLoader.Plugins!.Where(p => p.Command == (message.Content.Split(' ')[0]).Substring(botPrefix.Length)).FirstOrDefault(); + if (plugin != null) { if (message.Channel == await message.Author.CreateDMChannelAsync()) { if (plugin.canUseDM) { + if (plugin.requireAdmin) + { + if (message.Author.isAdmin()) + { + plugin.Execute(context, message, client, true); + Functions.WriteLogFile($"[{message.Author.Id}] Executed command (DM) : " + plugin.Command); + return; + } + await message.Channel.SendMessageAsync("This command is for administrators only !"); + return; + } plugin.Execute(context, message, client, true); - Functions.WriteLogFile("Executed command (DM) : " + plugin.Command); + Functions.WriteLogFile($"[{message.Author.Id}] Executed command (DM) : " + plugin.Command); + return; } + + await message.Channel.SendMessageAsync("This command is not for DMs"); return; } - plugin.Execute(context, message, client, false); - Functions.WriteLogFile("Executed command : " + plugin.Command); + if (plugin.canUseServer) + { + if (plugin.requireAdmin) + { + if (message.Author.isAdmin()) + { + plugin.Execute(context, message, client, false); + Functions.WriteLogFile($"[{message.Author.Id}] Executed command : " + plugin.Command); + return; + } + await message.Channel.SendMessageAsync("This command is for administrators only !"); + return; + } + plugin.Execute(context, message, client, false); + Functions.WriteLogFile($"[{message.Author.Id}] Executed command : " + plugin.Command); + return; + } + return; + } } catch { } diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 93698e0..1d3f018 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -5,10 +5,10 @@ using System.Threading.Tasks; using PluginManager.Core; using PluginManager.Others; - using PluginManager.Loaders; using PluginManager.LanguageSystem; using PluginManager.Online; + namespace DiscordBot { public class Program @@ -26,8 +26,6 @@ namespace DiscordBot public static void Main(string[] args) { - AppDomain.CurrentDomain.AppendPrivatePath(".\\Requirements"); - Console.Clear(); Directory.CreateDirectory("./Data/Resources"); Directory.CreateDirectory("./Data/Languages"); Directory.CreateDirectory("./Data/Plugins/Commands"); @@ -39,10 +37,10 @@ namespace DiscordBot { Console.WriteLine("Please insert your token: "); Console.Write("TOKEN: "); - string botToken = Console.ReadLine(); + string? botToken = Console.ReadLine(); if (botToken.Length == 59) { - string prefix = Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_PREFIX", + string? prefix = Functions.readCodeFromFile("./Data/Resources/DiscordBotCore.data", "BOT_PREFIX", '\t'); if (prefix == String.Empty || prefix == null) prefix = "!"; @@ -123,8 +121,28 @@ namespace DiscordBot break; case "dwlang": string Lname = data.MergeStrings(1); - string[] link = await languageManager.GetDownloadLink(Lname); - if (link[0] == null) + string?[] link = await languageManager.GetDownloadLink(Lname); + try + { + if (link[0] is null || link is null) + { + if (Lname == "") + { + Functions.WriteColorText($"Name is invalid"); + break; + } + Functions.WriteColorText("Failed to find language &b" + Lname + " &c! Use &glistlang &ccommand to display all available languages !"); + break; + } + if (link[1].Contains("CrossPlatform") || link[1].Contains("cp")) + { + Downloader dwn = new Downloader(Lname + ".lng", link[0]); + await dwn.DownloadFileAsync(Functions.langFolder); + } + else Functions.WriteColorText("The language you are trying to download (&b" + Lname + "&c) is not compatible with the version of this bot. User &glistlang &ccommand in order to see all available languages for your current version !\n" + link[1]); + break; + } + catch { if (Lname == "") { @@ -134,13 +152,7 @@ namespace DiscordBot Functions.WriteColorText("Failed to find language &b" + Lname + " &c! Use &glistlang &ccommand to display all available languages !"); break; } - if (link[1].Contains("CrossPlatform") || link[1].Contains("cp")) - { - Downloader dwn = new Downloader(Lname + ".lng", link[0]); - await dwn.DownloadFileAsync(Functions.langFolder); - } - else Functions.WriteColorText("The language you are trying to download (&b" + Lname + "&c) is not compatible with the version of this bot. User &glistlang &ccommand in order to see all available languages for your current version !\n" + link[1]); - break; + case "loadplugins": case "lp": LoadPlugins(discordbooter); @@ -283,18 +295,7 @@ namespace DiscordBot Console.Clear(); Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("Discord BOT for Cross Platform\n\nCreated by: Wizzy\nDiscord: Wizzy#9181\nCommands:"); - Console.WriteLine( - "lp | loadplugins -> load all plugins\n" + - "sd | shutdown->close connection to the server(stop bot)\n" + - "token -> display the current token\n" + - "listplugs -> list all available plugins\n" + - "dwplug [name] -> download plugin by name\n" + - "listlang -> list all available languages\n" + - "dwlang -> download language by name\n" + - "setlang [name] -> set language from the downloaded languages\n" + - "set-setting [setting.path] [value] -> set setting value" - ); + Console.WriteLine("Discord BOT for Cross Platform\n\nCreated by: Wizzy\nDiscord: Wizzy#9181"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("============================ Discord BOT - Cross Platform ============================"); string token = @@ -327,42 +328,22 @@ namespace DiscordBot return Task.CompletedTask; } - /// - /// Replace text in the file - /// - /// The file location (path) - /// The setting key code where to replace - /// The new value - /// If the does not exist, then this error is thrown - private static void ReplaceText(string file, string code, string value) - { - try - { - var f = false; - string[] text = File.ReadAllLines(file); - foreach (string line in text) - if (line.StartsWith(code)) - { - line.Replace(line.Split('\t')[1], value); - f = true; - } - - if (f) - File.WriteAllLines(@"./Data/Resources/DiscordBotCore.data", text); - else throw new FileNotFoundException(); - } - catch (FileNotFoundException) - { - File.AppendAllText(file, code + "\t" + value + "\n"); - } - } - /// /// Handle user input arguments from the startup of the application /// /// The arguments private static async Task HandleInput(string[] args) { + + if (args.Length == 0) + { + if (File.Exists("./ref/startupArguments.txt")) + { + var lines = await File.ReadAllLinesAsync("./ref/startupArguments.txt"); + args = lines; + } + } + int len = args.Length; if (len == 1 && args[0] == "--help") { diff --git a/PluginManager/Interfaces/DBCommand.cs b/PluginManager/Interfaces/DBCommand.cs index 9fce268..785bce9 100644 --- a/PluginManager/Interfaces/DBCommand.cs +++ b/PluginManager/Interfaces/DBCommand.cs @@ -10,6 +10,7 @@ bool canUseDM { get; } bool canUseServer { get; } + bool requireAdmin { get; } void Execute(Discord.Commands.SocketCommandContext context, Discord.WebSocket.SocketMessage message, diff --git a/PluginManager/Others/Permissions.cs b/PluginManager/Others/Permissions/DiscordPermissions.cs similarity index 56% rename from PluginManager/Others/Permissions.cs rename to PluginManager/Others/Permissions/DiscordPermissions.cs index 9d1ea87..5343cf5 100644 --- a/PluginManager/Others/Permissions.cs +++ b/PluginManager/Others/Permissions/DiscordPermissions.cs @@ -3,18 +3,21 @@ using Discord.WebSocket; using System.Linq; -namespace PluginManager.Others +namespace PluginManager.Others.Permissions { - public static class Permissions + public static class DiscordPermissions { public static bool hasPermission(this IRole role, GuildPermission permission) => role.Permissions.Has(permission); public static bool hasRole(this SocketGuildUser user, IRole role) => user.Roles.Contains(role); public static bool hasPermission(this SocketGuildUser user, GuildPermission permission) - => user.Roles.Where(role => role.hasPermission(permission)).Any(); - + => user.Roles.Where(role => role.hasPermission(permission)).Any() || user.Guild.Owner == user; + public static bool isAdmin(this SocketGuildUser user) => user.hasPermission(GuildPermission.Administrator); + public static bool isAdmin(this SocketUser user) => isAdmin((SocketGuildUser)user); } + + } \ No newline at end of file