Пример #1
0
 public bool Enqueue(RemindedTask task)
 {
     if (List.Contains(task))
     {
         return(false);
     }
     List.Enqueue(task, task.unixTime);
     return(true);
 }
Пример #2
0
    public async Task Remind(int h, int m, int day, int month, int year, [Remainder] string text)
    {
        try
        {
            for (int i = 0; i < 5 && _TaskList.isLocked(); i++)
            {
                var msg = await DiscordWrapper.SendMessage(Context, "Có lỗi xảy ra. Tự động thử lại sau 5s.");

                await Task.Delay(TimeSpan.FromSeconds(3));

                await msg.DeleteAsync();
            }
            if (_TaskList.isLocked())
            {
                DiscordWrapper.Log($"Reminder: {h} {m} {day} {month} {year} {text} *from* {Context.Message.Author.Username} failed due to locked TaskList.");
                await Context.Channel.TriggerTypingAsync();
                await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, đã thử lại quá 5 lần. Bạn vui lòng hãy đợi 1 phút và thử lại.", timeout : TimeSpan.FromMinutes(1));

                return;
            }

            _TaskList.Lock();
            DateTimeOffset dto  = new DateTimeOffset(year, month, day, h, m, 0, TimeSpan.FromHours(7));
            RemindedTask   task = new RemindedTask(dto.ToUnixTimeSeconds(), text, Context.Message.Author.Id, Context.Guild.Id, Context.Channel.Id);

            if (task.unixTime < DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 30)
            {
                DiscordWrapper.Log($"Reminder: {h} {m} {day} {month} {year} {text} *from* {Context.Message.Author.Username} failed due to invalid time.");
                await Context.Channel.TriggerTypingAsync();
                await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, thời gian không hợp lệ. Hãy đặt thời gian cách hiện tại ít nhất 1 phút.", timeout : TimeSpan.FromMinutes(1));

                return;
            }
            if (!_TaskList.Enqueue(task))
            {
                await Context.Channel.TriggerTypingAsync();
                await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, bạn đã lưu lời nhắc này trước đây.", timeout : TimeSpan.FromSeconds(10));

                return;
            }

            DiscordWrapper.Log("saving");
            if (!Save(_TaskList))
            {
                DiscordWrapper.Log($"Reminder: {h} {m} {day} {month} {year} {text} *from* {Context.Message.Author.Username} save failed.");
                await Context.Channel.TriggerTypingAsync();
                await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, có lỗi xảy ra trong quá trình lưu. Bạn hãy thử lại.", timeout : TimeSpan.FromMinutes(1));

                _TaskList.Unlock();
                return;
            }
            _TaskList.Unlock();
            DiscordWrapper.Log("saved");

            DiscordWrapper.Log($"Reminder: {h} {m} {day} {month} {year} {text} *from* {Context.Message.Author.Username} saved.");
            await Context.Channel.TriggerTypingAsync();
            await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, đã lưu lời nhắc thành công!", timeout : TimeSpan.FromMinutes(1));
        }
        catch (Exception e)
        {
            DiscordWrapper.Log($"{log} {e}");

            _TaskList.Unlock();
            DiscordWrapper.Log($"Reminder: {h} {m} {day} {month} {year} {text} *from* {Context.Message.Author.Username} save failed.");
            await Context.Channel.TriggerTypingAsync();
            await ReplyAndDeleteAsync($"{Context.Message.Author.Mention}, có lỗi xảy ra. Bạn hãy thử lại.", timeout : TimeSpan.FromMinutes(1));
        }
    }