private async Task AddReminderAsync(CommandContext ctx, TimeSpan timespan, DiscordChannel channel, string message, bool repeat = false) { if (string.IsNullOrWhiteSpace(message)) { throw new InvalidCommandUsageException("Missing time or repeat string."); } if (message.Length > 250) { throw new InvalidCommandUsageException("Message must be shorter than 250 characters."); } if (!(channel is null) && !channel.PermissionsFor(ctx.Member).HasPermission(Permissions.AccessChannels | Permissions.SendMessages)) { throw new CommandFailedException("You cannot send reminder to that channel!"); } if (channel is null && await ctx.Client.CreateDmChannelAsync(ctx.User.Id) is null) { throw new CommandFailedException("I cannot send DMs to you, please enable it so that I can remind you."); } bool privileged; using (DatabaseContext db = this.Database.CreateContext()) privileged = db.PrivilegedUsers.Any(u => u.UserId == ctx.User.Id); if (!ctx.Client.CurrentApplication.Owners.Any(o => o.Id == ctx.User.Id) && !privileged) { if (timespan < TimeSpan.Zero || timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Time span cannot be less than 1 minute or greater than 31 days."); } if (this.Shared.RemindExecuters.TryGetValue(ctx.User.Id, out ConcurrentDictionary <int, SavedTaskExecutor> texecs) && texecs.Count >= 20) { throw new CommandFailedException("You cannot have more than 20 reminders scheduled!"); } } DateTimeOffset when = DateTimeOffset.Now + timespan; var task = new SendMessageTaskInfo(channel?.Id ?? 0, ctx.User.Id, message, when, repeat, timespan); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, ctx.Client, task); if (repeat) { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will repeatedly remind {channel?.Mention ?? "you"} every {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))} to:\n\n{message}", important : false); } else { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will remind {channel?.Mention ?? "you"} in {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))} ({when.ToUtcTimestamp()}) to:\n\n{message}", important : false); } }
private async Task AddReminderAsync(CommandContext ctx, TimeSpan timespan, DiscordChannel channel, string message, bool repeat = false) { if (string.IsNullOrWhiteSpace(message)) { throw new InvalidCommandUsageException("Missing time or repeat string."); } if (message.Length > 250) { throw new InvalidCommandUsageException("Message must be shorter than 250 characters."); } if (timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Time span cannot be less than 1 minute or greater than 31 days."); } if (this.Shared.RemindExecuters.ContainsKey(ctx.User.Id) && this.Shared.RemindExecuters[ctx.User.Id].Count >= 20) { throw new CommandFailedException("You cannot have more than 20 reminders scheduled!"); } DateTimeOffset when = DateTimeOffset.Now + timespan; var task = new SendMessageTaskInfo(channel?.Id ?? 0, ctx.User.Id, message, when, repeat, timespan); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, (DiscordClientImpl)ctx.Client, task); if (repeat) { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will repeatedly remind {channel?.Mention ?? "you"} every {Formatter.Bold(timespan.Humanize(5))} to:\n\n{message}", important : false); } else { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will remind {channel?.Mention ?? "you"} in {Formatter.Bold(timespan.Humanize(5))} ({when.ToUtcTimestamp()}) to:\n\n{message}", important : false); } }