-1

While making Context.Message.Author as a default value of SocketGuildUser, throws CS1736 error (The expression being assigned to optional parameter `user' must be a constant or default value). Here is my code:

    [Command("whois")]
    [Alias("userinfo")]
    public async Task userinfo(SocketGuildUser user = Context.Message.Author)
    {
        var embed = new EmbedBuilder();
        embed.WithTitle("Info of " + "<@" + user.Id + ">");
        embed.WithDescription(
            "Status: " + user.Status + newline +
            "Joined: " + user.JoinedAt + newline +
            "Registered: " + user.CreatedAt + newline + newline +
            "Roles: " + user.Roles + newline +
            "Key permissions: " + user.GuildPermissions + newline + newline +
            "Voice channel: " + user.VoiceChannel + newline +
            "Voice session ID: " + user.VoiceSessionId + newline +
            "Voice state: " + user.VoiceState
                            );
        embed.WithThumbnailUrl(user.GetAvatarUrl());

        embed.WithFooter("If you think that this is a bit too many info, dm me: Dawid#3216 or join support server: https://discord.gg/E27bFsW");
        embed.WithCurrentTimestamp();

        embed.WithColor(new Color(25, 25, 255));
        ;
        await Context.Channel.SendMessageAsync("", false, embed);
    }
  • 6
    Possible duplicate of [Optional parameters "must be a compile-time constant"](https://stackoverflow.com/questions/7672005/optional-parameters-must-be-a-compile-time-constant) – prd Mar 25 '19 at 18:15

1 Answers1

1

I'd advise a little something like this :

public async Task UserInfo([Summary("The (optional) user to get info for")] IUser user = null)
{
    var userInfo = user ?? Context.User;
    //SOME CODE
}

If the command gets a user, display said user's info. Otherwise, (if null) it's the Context.User

Anu6is
  • 2,137
  • 2
  • 7
  • 17
Keelah
  • 192
  • 3
  • 15