This commit is contained in:
2022-12-17 12:35:26 +02:00
parent bd53d099d1
commit 79731a9704
2 changed files with 100 additions and 30 deletions

View File

@@ -16,6 +16,7 @@ namespace PluginManager
public static void Initialize(bool console) public static void Initialize(bool console)
{ {
if (isInitialized) throw new Exception("Logger is already initialized"); if (isInitialized) throw new Exception("Logger is already initialized");
if (!Config.Variables.Exists("LogFolder")) if (!Config.Variables.Exists("LogFolder"))
@@ -27,7 +28,7 @@ namespace PluginManager
isInitialized = true; isInitialized = true;
logFolder = Config.Variables.GetValue("LogFolder"); logFolder = Config.Variables.GetValue("LogFolder");
errFolder = Config.Variables.GetValue("ErrorFolder"); errFolder = Config.Variables.GetValue("ErrorFolder");
isConsole = console; isConsole = console
} }

119
README.md
View File

@@ -15,6 +15,7 @@ This project is based on:
Plugin Types: Plugin Types:
1. Commands 1. Commands
2. Events 2. Events
3. Slash Commands
### How to create a plugin ### 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 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. 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 ```cs
using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using PluginManager.Interfaces; using PluginManager.Interfaces;
namespace CMD_Utils namespace LevelingSystem;
internal class LevelCommand : DBCommand
{ {
class FlipCoin : DBCommand public string Command => "level";
{
public string Command => "flip";
public string Description => "Flip a coin"; public List<string> Aliases => new() { "lvl" };
public string Usage => "flip"; public string Description => "Display tour current level";
public bool canUseDM => true; public string Usage => "level";
public bool canUseServer => true;
public bool requireAdmin => false; public bool requireAdmin => false;
public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) public async void ExecuteServer(SocketCommandContext context)
{ {
System.Random random = new System.Random(); object[] user = await Variables.database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID='{context.Message.Author.Id}'");
int r = random.Next(1, 3); if (user is null)
if (r == 1) {
await message.Channel.SendMessageAsync("Heads"); await context.Channel.SendMessageAsync("You are now unranked !");
else await message.Channel.SendMessageAsync("Tails"); 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: #### Code description:
- Command - The keyword that triggers the execution for the command. This is what players must type in order to execute your command - 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 - 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 - 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 - 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 - 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 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/<optional subfolder>/yourDLLName.dll` `{bot_executable}/Data/Plugins/<optional subfolder>/[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 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. 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 - Start() - The main body of your event. This is executed when the bot loads all plugins
- client - the discord bot client - 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<SlashCommandOptionBuilder> Options => new List<SlashCommandOptionBuilder>()
{
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