#Global variable keep state

1 messages · Page 1 of 1 (latest)

naive glacier
#

Hi all,

I'm migrating from discord.js to discord.net, I was wondering if there is a way to keep stateful date in a command class?

So this data would be present every run, I noticed that commands are by default transient, so not sure how to go about this.

Using dependency injection btw:

    {
        GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.GuildMembers | GatewayIntents.DirectMessages
    }));
    services.AddHostedService<StartupService>();
    services.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>()));
    services.AddSingleton<InteractionHandler>();
    services.AddSingleton<DiscordLogger>();
    services.AddSingleton<PingCommand>();
public sealed class PingCommand : InteractionModuleBase
{
    private readonly ILogger<PingCommand> _logger;
    private string test;

    public PingCommand(ILogger<PingCommand> logger, InteractionHandler interactionHandler)
    {
        _logger = logger;
        test = "FIRST";
    }

    // This command will look like
    // group-name ping
    [SlashCommand("ping", "Get a pong")]
    public async Task Ping()
    {
        test = "SECOND";
        await Context.Interaction.RespondAsync("pong1");
    }

    // This command will look like
    // group-name ping
    [SlashCommand("ping2", "Get a pong")]
    public async Task Ping2()
    {
        await Context.Interaction.RespondAsync(test);
    }
}

Best regards!

manic umbra
#

Keep state in a separate service that's registered as a singleton

#

Inject it into your commands

naive glacier
#

oh yeah, the singleton stuff was to test, doesn't seem to matter

#

well, that's an awesome idea haha

#

forogt I was doing .net

#

😄

#

so would you recommend keeping it all transient? Or doesn't that matter

manic umbra
#

All what exactly?

naive glacier
#

addtransient instead of addsingleton

#

for the interactionservice, discordclient, etc.

manic umbra
#

Doesn't matter could leave as singletons