This commit is contained in:
Wizzy69
2022-01-12 15:37:45 +02:00
parent 84ee5c6235
commit bb5260ce68
40 changed files with 484 additions and 73 deletions

View File

@@ -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);
}
}
}
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -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;

View File

@@ -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 { }