Search code examples
c#dsharp+

I am trying to set the Discord bot's status with D#+ but it gives me a nullreference exception every time


This also might not be the way to do it but if it isn't I can't find the right one anywhere. Anyways this is my code:

await discord.UpdateStatusAsync(new DiscordGame("?help")); // Line 101

And this is the error

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=DSharpPlus
  StackTrace:
   at DSharpPlus.DiscordClient.InternalUpdateStatusAsync(DiscordGame game, Nullable`1 user_status, Nullable`1 idle_since)
   at DSharpPlus.DiscordClient.UpdateStatusAsync(DiscordGame game, Nullable`1 user_status, Nullable`1 idle_since)
   at theorangeguy.Program.<MainAsync>d__7.MoveNext() in C:\1notrllysure\coding\theorangeguy\C#\theorangeguy\theorangeguy\Program.cs:line 101

Btw the bot name is a joke with me and some friends if you were wondering.


Solution

  • A bit late but here's the answer:

    You can simply make a command/class that sets it for you. I did it like this:

        [Hidden]
        [Command("setactivity")]
        private async Task setactivity(CommandContext ctx)
        {
            if (ctx.User.Id == YourDiscordID)
            {
                DiscordActivity activity = new DiscordActivity();
                DiscordClient discord = ctx.Client;
                string input = Console.ReadLine();
                activity.Name = input;
                await discord.UpdateStatusAsync(activity);
                return;
            }
            else
            {
                return;
            }
        }
    

    I just use the Console.ReadLine(); prompt here so I can simply change it whenever I want to. I also put my ID there so only I can run the command. If the bot is for a larger scale I'd recommend making the Name not changeable like that. (Also as a heads up, you can only set the game it is playing, not a custom status.)