首页 > 解决方案 > 遍历列表并检查内容?

问题描述

我正在尝试设置一个简单的命令,用于将所有调用者输入到 .txt 文件中,除了我想循环遍历列表并检查没有人试图输入超过一次的部分之外,一切都运行良好,它不会对应用程序本身造成任何问题,但它没有做它应该做的事情。

任何帮助将非常感激。

string filePath = Secret.Secrets.fileDestination;

            var username = msg.Author.Username;
            //ulong
            var ID = msg.Author.Id;

            string IDout = ID.ToString();

            List<string> entries = File.ReadAllLines(filePath).ToList();

            if(entries.Contains(IDout))
            {
                await msg.Channel.SendMessageAsync($"{msg.Author.Mention}, you have already been entered.");

                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine($"Denied entry for {msg.Author.Mention}");

                return;
            }

            entries.Add($"{username}, {ID}");

            File.WriteAllLines(filePath, entries);

            await msg.Channel.SendMessageAsync($"{msg.Author.Mention} has been entered!");

标签: c#listbotsdiscorddiscord.net

解决方案


推荐阅读