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

0

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.

c#
dsharp+
asked on Stack Overflow Oct 13, 2020 by jamesina

1 Answer

0

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.)

answered on Stack Overflow Apr 1, 2021 by Ice

User contributions licensed under CC BY-SA 3.0