How do I check if a user exists in Dsharplus (C# discord bot)

176 Views Asked by At

Basically I want if someone types "whois (mention user)" the bot to send a message like "(user mentioned in first message) is (random message from array)" I just need to know 2 things How do I only send a message if it's an actual mentioned user and How do I set a variable something only if a certain user (my disc account) is mention

        [Description("Mention someone after and it will tell you all about them")]
        public async Task WhoIs(CommandContext ctx, [Description("Mention a User")] string name)
        {   string[] descriptions = {"", "", "", "", ""};
            Random rng = new Random();
            int rngOk = rng.Next(descriptions.Length);
            //if (name == "a certain user")
            //{
            //    rngOk = 3;
            //}
            await ctx.Channel.SendMessageAsync(name + descriptions[rngOk]).ConfigureAwait(false);
        }

This is my code so far If you need any additional information, just ask Any help would be appreciated

1

There are 1 best solutions below

0
On

I don't know precisely what you are trying to ask, your question is very vague but I will do my best to answer it

First Question

For the first part of your question "How do I only send a message if it's an actual mentioned user", you need to have a DiscordUser parameter in your command like this

[Command("test")]
public async Task MentionUserExample(CommandContext ctx, DiscordUser user)
{
    if (user.Id == "Put the ID of the user here")
    {
        //Write whatever you are trying to do here. This will only trigger if the 
        //user you mention, matches the ID of the one you set here
    }
    else
    {
        //Show an error message or something similar
    }
}

By doing this, when a user does "!help @RandomUser", the if statement will compare the ID of the mentioned user, to the ID of the user you set in the if statement

Second Question

Your second question "How do I set a variable something only if a certain user (my disc account) is mention", I don't understand properly. What are you trying to achieve here.

If we are mentioning your account as an example, why do you want to store it in a variable, if you are using it for something else it makes sense but once again your question is very vague

Once again you need the DiscordUser parameter in your command

[Command("test")]
public async Task MentionUserExample(CommandContext ctx, DiscordUser user)
{
   if (user.Id = "The ID of the user you want to store")
   {
      DiscordUser UserToStore = user;
   }
}

From here on you can use this variable and access many different properties as you would normally like Id, Username etc