某些用户只能使用命令

certain user(s) can only use a command

基本上这个程序允许用户使用命令!weaponrequest,然后将他们的请求保存到一个列表中,使用!nextweapon 你可以看到列表中的下一个武器是什么,这允许流媒体接受武器请求在具有全自动系统的游戏中。

无论如何,转到我的问题,我需要一种方法来使特定用户只能使用命令。我知道我需要一个列表来存储用户。我将手动将它们写入,因此我不需要任何类型的系统。我想知道的是使用 IF 语句如何检查用户是否在此列表中,然后进行设置,以便只有该用户可以激活该命令并收到响应。

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;
}

这是我希望仅供特定用户使用的命令。

您将文件读入缓冲区,逐行拆分,循环遍历这些行,并在找到用户名时中断循环。我不习惯 C#,但以下伪代码应该突出我的想法:

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

我确信有更好的方法来做到这一点,因为正如我所说,我不习惯 C#。另一方面,MyFile.txt 可能不是平面文件数据库的最佳名称。希望这对您有所帮助!

使用 List<string>.Add(strUserName) 函数将源(代码内、文本、数据库等)中的特殊用户添加到 List<string> 变量中。

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

然后,获取用户列表并检查它是否包含特定用户。

// Check if user has access to special commands
if (lstCertainUsers.Contains(strUserName)) 
{
    /* nextweapon code here */
}
if ((username == "") || (username == "") || (username == ""))
{

}

这就是我解决问题的方法,这不是最有效的方法,但我需要的用户数量有限,因此无需列出列表。

显然,您需要将这些名称替换为您希望能够使用该命令的自己的名称。例如:

(username == "RandomStranger")
{

}