Search code examples
c#exittargetingdiscord.net

How to target a specific user


I am currently in the process of making my own discord bot using C# and the Discord.NET API, but as the documentation is limited and i've only been coding in C# for a limited time, I've encountered an issue.

I've made a command to shut down the bot from inside the Discord chat, my problem is that everyone can actually use this command to shut it down, and I have no idea how to target a specific user so that it won't be executed when someone else uses the command.

My code:

private void RegisterShutdownCommand()
{
    commands.CreateCommand("exit")
        .Do((e) =>
            {
                Environment.Exit(0);
            });

}

Thanks in advance, if there is any information I missed that could be useful for a fix, ask me. :)


Solution

  • Since you didn't specify your who you wanted to be able to use your commands only, I will take it as that only you(the bot owner) can be able to use the command.

    Firstly, go copy your user ID in discord. You can do so by right-clicking your name in discord and click on 'copy ID' : http://prnt.sc/dw9zcw [Make sure you have developer mode turned on in order to do so]

    Then you can try this :

        private void RegisterShutdownCommand()
    {
        commands.CreateCommand("exit")
            .Do((e) =>
                {
                    if (e.User.Id != <Your_User_ID>)
                    {
                        /*Code to execute when its not the owner.*/
                    }
                    else
                    {
                        Environment.Exit(0);
                    }
                });
    
    }
    

    Basically just make use of if-else statements to check if the user's ID in discord is the owner. Alternatively , what @chessburgur suggested also works in the if condition.

    Hope this answers your question.