コード例 #1
0
        /// <summary>
        /// Checks to see if there is an existing tsv file;
        /// and if so, checks if it is valid and the last saved message exists.
        /// (Note the possible thrown ValidationException).
        /// </summary>
        /// <returns>Null if does not exist. The IMessage of the last saved message if exists.</returns>
        /// <exception cref="ValidationException">Thrown upon invalid file (wrong header, message ID invalid, etc).</exception>
        private async Task <IMessage> CheckExisting()
        {
            string tsvPath = Path.Join(_path, WriterThread.TSV_FILE_NAME);

            if (!File.Exists(tsvPath))
            {
                return(null);
            }

            using (StreamReader stream = new StreamReader(tsvPath))
                using (CsvReader csv = new CsvReader(stream, WriterThread.CSV_CONFIG))
                {
                    string tsvChronPath = Path.Join(_path, TSV_CHRON);
                    if (File.Exists(tsvChronPath))
                    {
                        throw new FieldValidationException(csv.Context, $"{tsvChronPath} exists.");
                    }

                    csv.Read();                                               // advance to beginning of file
                    CsvMessage csvMsg = csv.GetRecords <CsvMessage>().Last(); // expensive?
                    IMessage   msg    = await _channel.GetMessageAsync(csvMsg.Id);

                    if (msg == null)
                    {
                        throw new FieldValidationException(csv.Context, $"Discord message {csvMsg.Id} not found in channel.");
                    }

                    return(msg);
                }
        }
コード例 #2
0
        /// <summary>
        /// Converts the Discord.Net IMessage, downloads atatchments, and writes.
        /// </summary>
        /// <param name="message">The Discord.Net IMessage to be written.</param>
        /// <returns>The CsvMessage version of the Discord.Net IMessage.</returns>
        private CsvMessage ConvertMessageAndWrite(IMessage message)
        {
            DownloadResults attachments = DownloadAttachments(message);
            CsvMessage      msg         = new CsvMessage(message, attachments, _timeZone);

            _csv.WriteRecord(msg);
            _csv.NextRecord();
            Console.WriteLine($"Wrote message #{_msgWritten}");

            _msgWritten++;
            return(msg);
        }