0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
HallowedFlux
  • 100
  • 2
  • 10
  • You can ether check the user id before executing the command or integrate [permissions](http://rtd.discord.foxbot.me/en/legacy/features/permissions.html) – Jim Dec 15 '16 at 10:55

3 Answers3

2

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.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
1

While the answer by HallowedFlux is good, you should change it around to something like this (clean code + easy to reuse):

commands.CreateCommand("exit")
    .AddCheck((command, user, channel) => user.Id == <Your_User_ID>)
    .Do((e) =>
        {
            Environment.Exit(0);
        });

It can also be used for checking permissions etc. You can alter it to send out a message to the chat aswell if needed like so:

commands.CreateCommand("exit")
    .AddCheck((command, user, channel) => user.Id == <Your_User_ID>, "Only the owner can use this command!")
    .Do((e) =>
        {
            Environment.Exit(0);
        });
TomJ
  • 33
  • 8
0

If you are OK with using the allowed person's discord ID (the 4 numbers) then you can use: e.User.Id.ToString().Equals("####")