Search code examples
dsharp+

Setting the "Playing" status of my DIscord Bot, but i want to do it on startup


I have this simple command where I and one other user can only use it to set the "Playing" status of the bot, whilst it's pretty useful for changing it on the fly, I also want to set it when I start up the bot so that I don't have to set it myself every time I restart the bot. Is it possible I can implement this code in the Bot.cs file so that when it starts up, it has the status set and ready

        [Command("status")]
        public async Task SetBotStatus(CommandContext ctx, string message) 
        {
            if (ctx.User.Id == 572877986223751188 || ctx.User.Id == 327845261692895232) 
            {
                DiscordActivity activity = new DiscordActivity();
                DiscordClient discord = ctx.Client;

                activity.Name = message;
                await discord.UpdateStatusAsync(activity);
                return;
            }

Basically, the way this current command works is that it's based on an if statement stating only I and another user can use this command. The command has a requirement that you pass over a string which is the text that will be applied to the status and that is simply updated using discord.UpdateStatusAsync


Solution

  • With Dsharp+ this is a pretty easy fix to implement!

    In your Main Async method or in a global/public context, you will want to define your activity status which will be the default activity and then set a client-ready handler.

    In this example I am using a sharded client, this is not necessary to do so yourself. Your bot must lose its presence in a server before the ready event will fire on starting so it will need to be offline for a couple of minutes for Discord to catch that the client is offline and revoke the presence of the bot from servers. This means in testing you must let the bot lose its presence from being offline, and in production your bot will not lose its set activity from a momentary disconnection or a couple of missed heartbeats.

    If 5 consecutive heartbeats are missed the next successful heartbeat will fire the ready event resetting the bot's activity status to the default status. Dsharp+ will tell you this happened by warning you the client has become a zombie.

    static async Task MainAsync()
    {
        DiscordActivity activity = new();
        activity.Name = "Testing...";
        activity.ActivityType = ActivityType.Streaming;
      //The client I have here is sharded but that is not necessary.
        var discord = new DiscordShardedClient(new DiscordConfiguration()
        {
            Token = botToken,
            TokenType = botTokenType,
            Intents = botIntents,
            MinimumLogLevel = botLogLevel
        });
      // the handler for ready will set with UpdateStatusAsync()
        discord.Ready += async (client, readyEventArgs) =>
            await discord.UpdateStatusAsync(activity);
      // StartAsync() MUST come after this and any other handler.
        await discord.StartAsync();
        await Task.Delay(-1);
                
    }