From 79731a97040580650260c412d93bcabbec74f83d Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Sat, 17 Dec 2022 12:35:26 +0200 Subject: [PATCH] --- PluginManager/Logger.cs | 3 +- README.md | 127 +++++++++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 30 deletions(-) diff --git a/PluginManager/Logger.cs b/PluginManager/Logger.cs index 6c23b5f..906e8d4 100644 --- a/PluginManager/Logger.cs +++ b/PluginManager/Logger.cs @@ -16,6 +16,7 @@ namespace PluginManager public static void Initialize(bool console) { + if (isInitialized) throw new Exception("Logger is already initialized"); if (!Config.Variables.Exists("LogFolder")) @@ -27,7 +28,7 @@ namespace PluginManager isInitialized = true; logFolder = Config.Variables.GetValue("LogFolder"); errFolder = Config.Variables.GetValue("ErrorFolder"); - isConsole = console; + isConsole = console } diff --git a/README.md b/README.md index ed4f31b..50b41e4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This project is based on: Plugin Types: 1. Commands 2. Events +3. Slash Commands ### How to create a plugin @@ -39,56 +40,66 @@ after one successfull build. Commands are loaded when all plugins are loaded into memory. When an user executes the command, only then the Execute function is called. Commands are plugins that allow users to interact with them. -Here is an example of class that is a command class +Here is an example: ```cs +using Discord; using Discord.Commands; -using Discord.WebSocket; using PluginManager.Interfaces; -namespace CMD_Utils +namespace LevelingSystem; + +internal class LevelCommand : DBCommand { - class FlipCoin : DBCommand + public string Command => "level"; + + public List Aliases => new() { "lvl" }; + + public string Description => "Display tour current level"; + + public string Usage => "level"; + + public bool requireAdmin => false; + + public async void ExecuteServer(SocketCommandContext context) { - public string Command => "flip"; - - public string Description => "Flip a coin"; - - public string Usage => "flip"; - - public bool canUseDM => true; - - public bool canUseServer => true; - - public bool requireAdmin => false; - - public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + object[] user = await Variables.database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID='{context.Message.Author.Id}'"); + if (user is null) { - System.Random random = new System.Random(); - int r = random.Next(1, 3); - if (r == 1) - await message.Channel.SendMessageAsync("Heads"); - else await message.Channel.SendMessageAsync("Tails"); + await context.Channel.SendMessageAsync("You are now unranked !"); + return; } + + int level = (int)user[1]; + int exp = (int)user[2]; + + var builder = new EmbedBuilder(); + var r = new Random(); + builder.WithColor(r.Next(256), r.Next(256), r.Next(256)); + builder.AddField("Current Level", level, true) + .AddField("Current EXP", exp, true) + .AddField("Required Exp", (level * 8 + 24).ToString(), true); + builder.WithTimestamp(DateTimeOffset.Now); + builder.WithAuthor(context.Message.Author.Mention); + await context.Channel.SendMessageAsync(embed: builder.Build()); } } + ``` #### Code description: - Command - The keyword that triggers the execution for the command. This is what players must type in order to execute your command +- Aliases - The aliases that can be used instead of the full name to execute the command - Description - The description of your command. Can be anything you like - Usage - The usage of your command. This is what `help [Command]` command will display -- canUseDM - true if you plan to let users execute this command in DM chat with bot -- canUseServer - true if you plan to let the users execute this command in a server chat - requireAdmin - true if this command requres an user with Administrator permission in the server -- Execute () - the function of your command. +- ExecuteServer () - the function that is executed only when the command is invoked in a server channel. (optional) + - context - the command context +- ExecuteDM () - the function that is executed only when the command is invoked in a private (DM) channel. (optional) - context - the command context - - message - the message itself - - client - the discord bot client - - isDM - true if the message was sent from DM chat From here on, start coding. When your plugin is done, build it as any DLL project then add it to the following path -`{bot_executable}/Data/Plugins/Commands//yourDLLName.dll` +`{bot_executable}/Data/Plugins//[your dll name].dll` Then, reload bot and execute command `lp` in bot's console. The plugin should be loaded into memory or an error is thrown if not. If an error is thrown, then there is something wrong in your command's code. @@ -125,3 +136,61 @@ public class OnUserJoin : DBEvent - Start() - The main body of your event. This is executed when the bot loads all plugins - client - the discord bot client + +3.Slash Commands + + +Slash commands are server based commands. They work the same way as normal commands, but they require the `/` prefix as they are integrated +with the UI of Discord. +Here is an example: +```cs +using Discord; +using Discord.WebSocket; + +using PluginManager.Interfaces; + +namespace SlashCommands +{ + public class Random : DBSlashCommand + { + public string Name => "random"; + + public string Description => "Generates a random nunber between 2 values"; + + public bool canUseDM => true; + + public List Options => new List() + { + new SlashCommandOptionBuilder() {Name = "min-value", Description = "Minimum value", IsRequired=true, Type = ApplicationCommandOptionType.Integer, MinValue = 0, MaxValue = int.MaxValue-1}, + new SlashCommandOptionBuilder() {Name="max-value", Description = "Maximum value", IsRequired=true, Type=ApplicationCommandOptionType.Integer,MinValue = 0, MaxValue = int.MaxValue-1} + }; + + public async void ExecuteServer(SocketSlashCommand command) + { + var rnd = new System.Random(); + var options = command.Data.Options.ToArray(); + if (options.Count() != 2) + { + await command.RespondAsync("Invalid parameters", ephemeral: true); + return; + } + + Int64 numberOne = (Int64)options[0].Value; + Int64 numberTwo = (Int64)options[1].Value; + + await command.RespondAsync("Your generated number is " + rnd.Next((int)numberOne, (int)numberTwo), ephemeral: true); + + } + } +} +``` + +#### Code description: +- Name - the command name (execute with /{Name}) +- Description - The description of the command +- canUseDM - true id this command can be activated in DM chat, false otherwise +- Options - the arguments of the command +- ExecuteServer() - this function will be called if the command is invoked in a server channel (optional) + - context - the command context +- ExecuteDM() - this function will be called if the command is invoked in a DM channel (optional) + - context - the command context