Added support for Custom Activity

This commit is contained in:
2024-08-18 17:57:16 +03:00
parent c080074292
commit 0a64de2439
3 changed files with 44 additions and 54 deletions
+11 -7
View File
@@ -1,6 +1,8 @@
using System; using System;
using DiscordBotCore;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordBotCore;
using Spectre.Console; using Spectre.Console;
namespace DiscordBot; namespace DiscordBot;
@@ -11,34 +13,36 @@ public static class Installer
{ {
var value = AnsiConsole.Ask<string>($"[green]{message}[/]"); var value = AnsiConsole.Ask<string>($"[green]{message}[/]");
if (string.IsNullOrWhiteSpace(value)) if (!string.IsNullOrWhiteSpace(value))
{ {
return value;
}
AnsiConsole.MarkupLine($"Invalid {key} !"); AnsiConsole.MarkupLine($"Invalid {key} !");
Environment.Exit(-20); Environment.Exit(-20);
}
return value; return value;
} }
public static async Task GenerateStartupConfig() public static async Task GenerateStartupConfig()
{ {
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token")) if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token"))
{ {
string response = AskForConfig("token", "Token:"); var response = AskForConfig("token", "Token:");
Application.CurrentApplication.ApplicationEnvironmentVariables.Add("token", response); Application.CurrentApplication.ApplicationEnvironmentVariables.Add("token", response);
} }
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix")) if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix"))
{ {
string response = AskForConfig("prefix", "Prefix:"); var response = AskForConfig("prefix", "Prefix:");
Application.CurrentApplication.ApplicationEnvironmentVariables.Add("prefix", response); Application.CurrentApplication.ApplicationEnvironmentVariables.Add("prefix", response);
} }
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID")) if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID"))
{ {
string response = AskForConfig("ServerID", "Please enter the server Ids where the bot will be used (separated by ;):"); var response = AskForConfig("ServerID", "Please enter the server Ids where the bot will be used (separated by ;):");
Application.CurrentApplication.ApplicationEnvironmentVariables.Add("ServerID", response); Application.CurrentApplication.ApplicationEnvironmentVariables.Add("ServerID", response);
} }
+10 -42
View File
@@ -1,10 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord;
using DiscordBotCore.Bot; using DiscordBotCore.Bot;
using DiscordBotCore.Online; using DiscordBotCore.Online;
using DiscordBotCore.Online.Helpers; using DiscordBotCore.Online.Helpers;
@@ -49,9 +50,9 @@ namespace DiscordBotCore
public InternalActionManager InternalActionManager { get; private set; } = null!; public InternalActionManager InternalActionManager { get; private set; } = null!;
public IPluginManager PluginManager { get; private set; } = null!; public IPluginManager PluginManager { get; private set; } = null!;
/// <summary>
/// Create the application. This method is used to initialize the application. Can not initialize multiple times.
/// </summary>
public static async Task CreateApplication() public static async Task CreateApplication()
{ {
if (!await OnlineFunctions.IsInternetConnected()) if (!await OnlineFunctions.IsInternetConnected())
@@ -60,6 +61,11 @@ namespace DiscordBotCore
Environment.Exit(0); Environment.Exit(0);
} }
if (CurrentApplication is not null)
{
return;
}
CurrentApplication = new Application(); CurrentApplication = new Application();
Directory.CreateDirectory(_ResourcesFolder); Directory.CreateDirectory(_ResourcesFolder);
@@ -93,9 +99,7 @@ namespace DiscordBotCore
CurrentApplication.InternalActionManager = new InternalActionManager(); CurrentApplication.InternalActionManager = new InternalActionManager();
await CurrentApplication.InternalActionManager.Initialize(); await CurrentApplication.InternalActionManager.Initialize();
} }
/// <summary> /// <summary>
/// A special class that is designed to log messages. It is a wrapper around the Logger module /// A special class that is designed to log messages. It is a wrapper around the Logger module
/// The logger module is required to have this specific methods: /// The logger module is required to have this specific methods:
@@ -158,46 +162,10 @@ namespace DiscordBotCore
return result; return result;
} }
public static string GetResourceFullPath() => CurrentApplication.ApplicationEnvironmentVariables.Get<string>("ResourceFolder", _ResourcesFolder);
public static string GetPluginFullPath(string path) public static string GetPluginFullPath(string path)
{ {
string result = Path.Combine(_PluginsFolder, path); string result = Path.Combine(_PluginsFolder, path);
return result; return result;
} }
public static string GetPluginFullPath() => CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder", _PluginsFolder);
public static async Task<string> GetPluginDependencyPath(string dependencyName, string? pluginName = null)
{
string? dependencyLocation;
if (pluginName is null)
dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName);
else
dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName, pluginName);
if (dependencyLocation is null)
throw new DependencyNotFoundException($"Dependency {dependencyName} not found", pluginName);
return dependencyLocation;
}
/// <summary>
/// Invokes a method from a module
/// </summary>
public static async Task InvokeModuleMethod(string moduleName, string methodName, object[] parameters)
{
KeyValuePair<ModuleData, IModule> module = CurrentApplication._ModuleManager.GetModule(moduleName);
await CurrentApplication._ModuleManager.InvokeMethod(module.Value, module.Value.MethodMapping[methodName], parameters);
}
/// <summary>
/// Invokes a method from a module and returns the result
/// </summary>
public static async Task<object?> InvokeModuleMethodWithResponse(string moduleName, string methodName, object[] parameters)
{
KeyValuePair<ModuleData, IModule> module = CurrentApplication._ModuleManager.GetModule(moduleName);
return await CurrentApplication._ModuleManager.InvokeMethodWithReturnValue(module.Value, module.Value.MethodMapping[methodName], parameters);
}
} }
} }
@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
@@ -109,6 +110,23 @@ public class DiscordBotApplication
private Task Ready() private Task Ready()
{ {
IsReady = true; IsReady = true;
if (Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("CustomStatus"))
{
var status = Application.CurrentApplication.ApplicationEnvironmentVariables.GetDictionary<string, string>("CustomStatus");
string type = status["Type"];
string message = status["Message"];
ActivityType activityType = type switch
{
"Playing" => ActivityType.Playing,
"Listening" => ActivityType.Listening,
"Watching" => ActivityType.Watching,
"Streaming" => ActivityType.Streaming,
_ => ActivityType.Playing
};
Client.SetGameAsync(message, null, activityType);
}
return Task.CompletedTask; return Task.CompletedTask;
} }