예제 #1
0
        public static async Task <TimerData> UnscheduleTimerAsync(DatabaseTimer timer, DiscordClient shard, DatabaseContextBuilder database, SharedData shared)
        {
            // lock the timers
            await shared.TimerSempahore.WaitAsync();

            try
            {
                // remove the requested timer
                using (var db = database.CreateContext())
                {
                    db.Timers.Remove(timer);
                    await db.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Caught Exception in Timer Unschedule: {ex.GetType().ToString()}\n{ex.StackTrace}");
            }
            finally
            {
                // release the lock
                shared.TimerSempahore.Release();
            }

            // trigger a reschedule
            return(await RescheduleTimers(shard, database, shared));
        }
예제 #2
0
        public async Task SetAsync(CommandContext ctx, [Description("When the reminder is to be sent"), RemainingText] string dataToParse)
        {
            await ctx.TriggerTypingAsync();

            var(duration, text) = Dates.ParseTime(dataToParse);

            if (string.IsNullOrWhiteSpace(text) || text.Length > 128)
            {
                await ctx.ElevatedRespondAsync(
                    "Reminder text must to be no longer than 128 characters, not empty and not whitespace.");

                return;
            }
#if !DEBUG
            if (duration < TimeSpan.FromSeconds(30))
            {
                await ctx.ElevatedRespondAsync("Minimum required time span to set a reminder is 30 seconds.");

                return;
            }
#endif

            if (duration > TimeSpan.FromDays(365)) // 1 year is the maximum
            {
                await ctx.ElevatedRespondAsync("Maximum allowed time span to set a reminder is 1 year.");

                return;
            }

            var now        = DateTimeOffset.UtcNow;
            var dispatchAt = now + duration;

            // create a new timer
            var reminder = new DatabaseTimer
            {
                GuildId    = (long)ctx.Guild.Id,
                ChannelId  = (long)ctx.Channel.Id,
                UserId     = (long)ctx.User.Id,
                DispatchAt = dispatchAt.LocalDateTime,
                ActionType = TimerActionType.Reminder
            };
            reminder.SetData(new TimerReminderData {
                ReminderText = text
            });
            using (var db = this.Database.CreateContext())
            {
                db.Timers.Add(reminder);
                await db.SaveChangesAsync();
            }

            // reschedule timers
            await Timers.RescheduleTimers(ctx.Client, this.Database, this.Shared);

            var emoji = DiscordEmoji.FromName(ctx.Client, ":alarm_clock:");
            await ctx.SafeRespondAsync(
                $"{emoji} Ok, in {duration.Humanize(4, minUnit: TimeUnit.Second)} I will remind you about the following:\n\n{text.BreakMentions()}");
        }
예제 #3
0
 /// <summary>
 /// Creates new timer dispatcher information model.
 /// </summary>
 /// <param name="task">Task which will be dispatched.</param>
 /// <param name="dbtimer">Database model with data about the timer.</param>
 /// <param name="context">Context in which this timer is to be dispatched.</param>
 /// <param name="db">Database connection builder for this timer.</param>
 /// <param name="shared">Data shared across the shard.</param>
 /// <param name="cts">Cancellation token source for this timer.</param>
 public TimerData(Task task, DatabaseTimer dbtimer, DiscordClient context, DatabaseContextBuilder db, SharedData shared, CancellationTokenSource cts)
 {
     this.Timer    = task;
     this.DbTimer  = dbtimer;
     this.Context  = context;
     this.Database = db;
     this.Shared   = shared;
     this.Cancel   = cts;
 }
예제 #4
0
        public async Task AboutAsync(CommandContext ctx, DiscordMessage message, [RemainingText] string when)
        {
            await ctx.TriggerTypingAsync();

            var(duration, text) = Dates.ParseTime(when);

            if (duration > TimeSpan.FromDays(365))             // 1 year is the maximum
            {
                await ctx.ElevatedRespondAsync("Maximum allowed time span to set a reminder is 1 year.");

                return;
            }

            string content = message.Content;

            if (content.Length > 50)
            {
                content.Remove(50);
                content += "...";
            }

            text = $"**{message.Author.Username}#{message.Author.Discriminator}:**\n{content}\n\n({message.Id})\n{Shared.Emojis.JumpLink.ToString()} {message.JumpLink}";

            var now        = DateTimeOffset.UtcNow;
            var dispatchAt = now + duration;

            // create a new timer
            var reminder = new DatabaseTimer
            {
                GuildId    = (long)ctx.Guild.Id,
                ChannelId  = (long)ctx.Channel.Id,
                UserId     = (long)ctx.User.Id,
                DispatchAt = dispatchAt.LocalDateTime,
                ActionType = TimerActionType.Reminder
            };

            reminder.SetData(new TimerReminderData {
                ReminderText = text
            });
            using (var db = this.Database.CreateContext())
            {
                db.Timers.Add(reminder);
                await db.SaveChangesAsync();
            }

            // reschedule timers
            await Timers.RescheduleTimers(ctx.Client, this.Database, this.Shared);

            var emoji = DiscordEmoji.FromName(ctx.Client, ":alarm_clock:");
            await ctx.SafeRespondAsync(
                $"{emoji} Ok, in {duration.Humanize(4, minUnit: TimeUnit.Second)} I will remind you about the following message:\n\n{content}\n\n({message.Id})");
        }