This commit is contained in:
2023-04-05 20:10:43 +03:00
parent 10b9548c29
commit d5df6cfb9d
7 changed files with 205 additions and 16 deletions

View File

@@ -0,0 +1,72 @@
using Discord;
using PluginManager.Interfaces;
using Discord.WebSocket;
namespace Economy.Commands;
public class Balance : DBSlashCommand
{
public string Name => "balance";
public string Description => "Change or view info about your balance";
public bool canUseDM => false; // server only
public List<SlashCommandOptionBuilder> Options => new List<SlashCommandOptionBuilder>()
{
new SlashCommandOptionBuilder(){Name="send", Description="Send money to another user", Type=ApplicationCommandOptionType.SubCommand, Options=new List<SlashCommandOptionBuilder>()
{
new SlashCommandOptionBuilder(){Name="user", Description="The user to send money to", Type=ApplicationCommandOptionType.User, IsRequired=true},
new SlashCommandOptionBuilder(){Name="amount", Description="The amount of money to send", Type=ApplicationCommandOptionType.Number, IsRequired=true}
}},
new SlashCommandOptionBuilder(){Name="info", Description="View info about your balance", Type=ApplicationCommandOptionType.SubCommand}
};
public async void ExecuteServer(SocketSlashCommand context)
{
var option = context.Data.Options.FirstOrDefault();
var guild = context.User.MutualGuilds.FirstOrDefault(g => g.Id == context.GuildId);
if (option.Name == "send")
{
var options = option.Options.ToArray();
Console.WriteLine(options.Length);
var user = options[0].Value as IUser;
var amount = options[1].Value as float?;
if (amount == null)
{
await context.RespondAsync("The amount is invalid");
return;
}
if (user == null)
{
await context.RespondAsync("The user is invalid");
return;
}
if (user.Id == context.User.Id)
{
await context.RespondAsync("You can't send money to yourself");
return;
}
var balance = await Engine.GetBalance(context.User.Id);
if (balance < amount)
{
await context.RespondAsync("You don't have enough money to send");
return;
}
await Engine.RemoveMoney(context.User.Id, amount.Value);
await Engine.AddMoney(user.Id, amount.Value);
await context.RespondAsync($"You sent {amount} to {guild.GetUser(user.Id).Mention}");
}
else if (option.Name == "info")
{
var balance = await Engine.GetBalance(context.User.Id);
await context.RespondAsync($"Your balance is {balance}");
}
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\PluginManager\PluginManager.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using PluginManager.Database;
namespace Economy;
public static class Engine
{
public static SqlDatabase Database { get; set; }
public static async void AddUser(ulong userID)
{
await Database.InsertAsync("UserBank", userID.ToString(), "0");
}
public static async void RemoveUser(ulong userID)
{
await Database.RemoveKeyAsync("UserBank", "UserID", userID.ToString());
}
public static async Task AddMoney(ulong userID, float amount)
{
var balance = await Database.GetValueAsync("UserBank", "UserID", userID.ToString(), "Balance");
if (balance == null)
{
AddUser(userID);
balance = "0";
}
var float_balance = float.Parse(balance);
float_balance += amount;
await Database.SetValueAsync("UserBank", "UserID", userID.ToString(), "Balance", float_balance.ToString());
}
public static async Task RemoveMoney(ulong userID, float amount)
{
var balance = await Database.GetValueAsync("UserBank", "UserID", userID.ToString(), "Balance");
if (balance == null)
{
AddUser(userID);
balance = "0";
}
var float_balance = float.Parse(balance);
float_balance -= amount;
await Database.SetValueAsync("UserBank", "UserID", userID.ToString(), "Balance", float_balance.ToString());
}
public static async Task<float> GetBalance(ulong userID)
{
var balance = await Database.GetValueAsync("UserBank", "UserID", userID.ToString(), "Balance");
if (balance == null)
{
AddUser(userID);
balance = "0";
}
return float.Parse(balance);
}
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Interfaces;
using PluginManager.Database;
namespace Economy
{
public class EntryEvent : DBEvent
{
public string Name => "Economy Plugin Engine";
public string Description => "The economy plugin main engine";
public async void Start(global::Discord.WebSocket.DiscordSocketClient client)
{
Console.WriteLine("Economy Plugin Engine Started");
Directory.CreateDirectory(PluginManager.Others.Functions.dataFolder + "/Economy");
Engine.Database = new SqlDatabase(PluginManager.Others.Functions.dataFolder + "/Economy/Economy.db");
await Engine.Database.Open();
await Engine.Database.CreateTableAsync("UserBank", "UserID INT", "Balance FLOAT");
client.Disconnected += (e) =>
{
Engine.Database.Stop();
return Task.CompletedTask;
};
}
}
}