Example Plugin

This commit is contained in:
Mira 2025-01-27 17:26:51 +01:00
commit 03899f8e5b
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36
23 changed files with 1128 additions and 0 deletions

View file

@ -0,0 +1,15 @@
using ProjectMakoto.Database;
using ProjectMakoto.Enums;
namespace ProjectMakoto.Plugins.Example.Entities;
public class ExampleSubTable1(Bot bot, ExampleTable parent) : RequiresParent<ExampleTable>(bot, parent) // You can easily add a Parent to a class by using
{ // RequiresParent, so theres no need for extensive
// constructors.
[ColumnName("example_value3"), ColumnType(ColumnTypes.TinyInt), Default("1")] // In the sub-table, you simply do the same as in the parent.
public bool ExampleValue3
{
get => this.Parent.GetValue<bool>(this.Parent.Id, "example_value3"); // The only difference being that you need to access the Parent
set => this.Parent.SetValue(this.Parent.Id, "example_value3", value); // for the Database Tools.
}
}

46
Entities/ExampleTable.cs Normal file
View file

@ -0,0 +1,46 @@
// Project Makoto Example Plugin
// Copyright (C) 2023 Fortunevale
// This code is licensed under MIT license (see 'LICENSE'-file for details)
using Newtonsoft.Json;
using ProjectMakoto.Database;
using ProjectMakoto.Enums;
namespace ProjectMakoto.Plugins.Example.Entities;
[TableName("exampletable")]
public class ExampleTable : PluginDatabaseTable
{
public ExampleTable(BasePlugin plugin, ulong identifierValue) : base(plugin, identifierValue)
{
this.Id = identifierValue; // Don't forget to initialize the identifier so the getters and setters can do their work.
this.SubTable1 = new(this.Plugin.Bot, this);
}
[ColumnName("userid"), ColumnType(ColumnTypes.BigInt), Primary] // The identifier value is stored twice, once as key for indexing (access via List[id])
internal ulong Id { get; init; } // and inside the value (this class) for easy actually communicating with the database
[ColumnName("example_value1"), ColumnType(ColumnTypes.TinyInt), Default("1")]
public bool ExampleValue1
{
get => this.GetValue<bool>(this.Id, "example_value1"); // This directly gets the value from the database. It can be frequently accessed, theres no need to cache the value.
// Makoto takes care of caching for you. Values are stored for a few seconds.
set => this.SetValue(this.Id, "example_value1", value); // Setting the value removes the cached item. And refetches on next get.
}
[ColumnName("example_value2"), ColumnType(ColumnTypes.LongText), Default("[]")] // Make sure to minify the json if you want to store large amounts of data.
public string[] ExampleValue2 // LongText can store up to 4GiB of text.
{ // Another important note: It's recommended to use arrays instead of
// lists as they do not fire the setters when adding/removing items.
get => JsonConvert.DeserializeObject<string[]>(this.GetValue<string>(this.Id, "example_value2")) ?? []; // Storing more sophisticated data is usually done by converting it to json
// and storing the resulting string in the database.
set => this.SetValue(this.Id, "example_value2", JsonConvert.SerializeObject(value));
}
[ContainsValues] // This attribute signals to Makoto to look for more columns inside this property.
public ExampleSubTable1 SubTable1 { get; init; } // This allows you to sort your table a little more so you don't get bombarded with
} // tons of properties everytime you want to access the table in your code.

10
Entities/PluginConfig.cs Normal file
View file

@ -0,0 +1,10 @@
// Project Makoto Example Plugin
// Copyright (C) 2023 Fortunevale
// This code is licensed under MIT license (see 'LICENSE'-file for details)
namespace ProjectMakoto.Plugins.Example.Entities;
public class PluginConfig
{
public string ExampleString { get; set; } = "Example Config Option";
}

24
Entities/Translations.cs Normal file
View file

@ -0,0 +1,24 @@
// Project Makoto
// Copyright (C) 2023 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Example.Entities;
#pragma warning disable CS8981
#pragma warning disable CS8618
#pragma warning disable IDE1006
public class Translations : ITranslations
{
public Dictionary<string, int> Progress = new();
public CommandTranslation[] CommandList { get; set; }
#region AutoGenerated
public commands Commands;
public sealed class commands
{
public SingleTranslationKey ValueSet;
}
#endregion AutoGenerated
}