-1

Essentially this program allows a user to use a command !weaponrequest, it then saves their request into a list, with !nextweapon you can see what the next weapon in the list is, this allows a streamer to take weapon requests in a game with a fully automated system.

Anyway moving onto my problem, I need a way to make it so that a certain user(s) can only use a command. I know that I am going to need a list to store the users in. I will write them in manually so I don't need any kind of system for that. All I am wondering is using an IF statement how would I check to see if the user is in this list and then make it so that only that user(s) can activate that command and receive a response.

case "nextweapon":
{
    if (new FileInfo("MyFile.txt").Length == 0)
    {
        irc.sendChatMessage("There are no weapons in the list!");
        break;
    }

    string Lines = File.ReadLines("MyFile.txt").Take(1).First();
    //irc.sendChatMessage(Lines);

    List<string> WeaponList = File.ReadAllLines("MyFile.txt").ToList();
    string FirstItem = WeaponList[0];
    WeaponList.RemoveAt(0);
    File.WriteAllLines("MyFile.txt", WeaponList.ToArray());
    irc.sendChatMessage(Lines);
    break;
}

This is the command that I want to only be used by a certain user(s).

  • 1
    Don't post images of text. Just post your code inline, highlight it, then hit the `{}` button to make sure it's formatted. – Blorgbeard Aug 30 '16 at 02:00
  • So.. you can [edit] your question to add the code. Anyway, to answer your question (maybe), you can use `List.Contains()` to see whether a string is contained in a `List`. – Blorgbeard Aug 30 '16 at 02:15
  • Can you post your solution for this question and accept it? – Simple Sandman Aug 31 '16 at 22:05

3 Answers3

2

Add your special users from a source (in-code, text, database, etc.) into a List<string> variable using the List<string>.Add(strUserName) function.

List<string> lstCertainUsers = new List<string>();
/*
 * ToDo: Add users from source (in-code, text, database, etc.) into lstCertainUsers
*/

Then, get the list of users and check if it contains the certain user.

// Check if user has access to special commands
if (lstCertainUsers.Contains(strUserName)) 
{
    /* nextweapon code here */
}
Simple Sandman
  • 900
  • 3
  • 11
  • 34
0
if ((username == "") || (username == "") || (username == ""))
{

}

This is how I solved the issue, it's not the most efficient way but there are only limited users that I needed so making a list was not necessary.

Obviously you need to replace the names with your own names you want to be able to use the command. For example:

(username == "RandomStranger")
{

}
-1

You'd read the file into a buffer, split it line-by-line, loop through those lines and break the loop if and when their username is found. I'm not used to C# but the following pseudocode should highlight my idea:

username = /* the current user's username */;
userfile = readfile('users.txt');
userlist = split(userfile, "\n");
is_valid = false;

for user in userlist
    if user equals username
        command_is_valid = true;
        break;

if command_is_valid:
    // your code

else
    // do nothing

I'm sure there's a better way to do it because, as I say, I'm not used to C#. On another note, MyFile.txt probably isn't the best name for your flat-file database. Hope this helps!

Connor Gurney
  • 372
  • 2
  • 12