I am writing a discord Bot in C# (With the help of DSharpPlus) that uses the VRChatAPI to open instances and invite a list of people to it (plus many more functions)
I have seen here: https://github.com/DSharpPlus/DSharpPlus/blob/master/docs/articles/commands/dependency_injection.md
that you can parse a dependency class to a BaseCommandModule.
Tho I would like to parse an already existing instace of said dependency to my BaseCommandModulebecause I want to have access to the same Information because i use it in many different areas.
Is this possible? if yes, How?
My code looks as follows:
My VRChat API Communicator class that i want to use and instance of:
public class VRCCommunicator
{
public string Status { get; private set; }
Configuration Config;
AuthenticationApi AuthApi;
UsersApi UsersApi;
WorldsApi WorldsApi;
public CurrentUser currentUser { get; private set; }
VRCCommunicator()
{
var json = string.Empty;
using (var sr = new StreamReader("configs\\vrchatconfig.json"))
json = sr.ReadToEnd();
var vRCConfigJson = JsonConvert.DeserializeObject<VRChatConfigJson>(json);
Config = new Configuration()
{
Username = vRCConfigJson.Username,
Password = vRCConfigJson.Password
};
AuthApi = new AuthenticationApi(Config);
UsersApi = new UsersApi(Config);
WorldsApi = new WorldsApi(Config);
}
public async void logIn()
{
currentUser = await AuthApi.GetCurrentUserAsync();
Status = "Logged in as: " + currentUser.DisplayName;
}
}
and the BaseCommandModule i want to use the instance in:
public class DebugCommands : BaseCommandModule
{
private VRCCommunicator VRCApi;
[Command("login")]
public async Task VRCLogIn(CommandContext ctx)
{
VRCApi.logIn();
await ctx.Channel.SendMessageAsync(VRCApi.Status).ConfigureAwait(false);
}
[Command("vrchatstatus")]
public async Task VRCStatus(CommandContext ctx)
{
await ctx.Channel.SendMessageAsync(VRCApi.Status).ConfigureAwait(false);
}
}
The way portrayed in the link above works but seems to create a new instance of VRCCommunicator every time the command is called
Edit: Forgot to add my base Bot class:
public class Bot
{
public DiscordClient Client { get; private set; }
public CommandsNextExtension Commands { get; private set; }
public VRCCommunicator VRCApi { get; private set; }
public async Task RunAsync()
{
var json = string.Empty;
using(var fs = File.OpenRead("configs\\botconfig.json"))
using(var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync().ConfigureAwait(false);
var configJson = JsonConvert.DeserializeObject<BotConfigJson>(json);
var config = new DiscordConfiguration()
{
Token = configJson.Token,
TokenType = TokenType.Bot,
Intents = DiscordIntents.All,
AutoReconnect = true,
MinimumLogLevel = LogLevel.Debug,
};
Client = new DiscordClient(config);
Client.Ready += OnClientReady;
var commandsConfig = new CommandsNextConfiguration
{
StringPrefixes = new string[] { configJson.Prefix },
EnableMentionPrefix = true,
EnableDms = true,
CaseSensitive = true,
DmHelp = true
};
Commands = Client.UseCommandsNext(commandsConfig);
Commands.RegisterCommands<DebugCommands>();
await Client.ConnectAsync();
await Task.Delay(-1);
}
private Task OnClientReady(object sender, ReadyEventArgs e)
{
Console.WriteLine("Bot Ready");
return Task.CompletedTask;
}
}