public async Task Repeat(GuildDateTime dt, params string[] options) { if (!_service.RepeaterReady) { return; } var(opts, _) = OptionsParser.ParseFrom(new Repeater.Options(), options); if (string.IsNullOrWhiteSpace(opts.Message)) { return; } var toAdd = new Repeater() { ChannelId = Context.Channel.Id, GuildId = Context.Guild.Id, Interval = TimeSpan.FromMinutes(opts.Interval), Message = opts.Message, NoRedundant = opts.NoRedundant, StartTimeOfDay = dt?.InputTimeUtc.TimeOfDay, }; using (var uow = _db.UnitOfWork) { var gc = uow.GuildConfigs.ForId(Context.Guild.Id, set => set.Include(x => x.GuildRepeaters)); if (gc.GuildRepeaters.Count >= 5) { return; } gc.GuildRepeaters.Add(toAdd); await uow.CompleteAsync(); } var rep = new RepeatRunner((SocketGuild)Context.Guild, toAdd, _service); _service.Repeaters.AddOrUpdate(Context.Guild.Id, new ConcurrentDictionary <int, RepeatRunner>(new[] { new KeyValuePair <int, RepeatRunner>(toAdd.Id, rep) }), (key, old) => { old.TryAdd(rep.Repeater.Id, rep); return(old); }); string secondPart = ""; if (dt != null) { secondPart = GetText("repeater_initial", Format.Bold(rep.InitialInterval.Hours.ToString()), Format.Bold(rep.InitialInterval.Minutes.ToString())); } await Context.Channel.SendConfirmAsync( "🔁 " + GetText("repeater", Format.Bold(((IGuildUser)Context.User).GuildPermissions.MentionEveryone ? rep.Repeater.Message : rep.Repeater.Message.SanitizeMentions()), Format.Bold(rep.Repeater.Interval.Days.ToString()), Format.Bold(rep.Repeater.Interval.Hours.ToString()), Format.Bold(rep.Repeater.Interval.Minutes.ToString())) + " " + secondPart).ConfigureAwait(false); }
public async Task Repeat(GuildDateTime dt, params string[] options) { if (!_service.RepeaterReady) { return; } var(opts, _) = OptionsParser.ParseFrom(new Repeater.Options(), options); if (string.IsNullOrWhiteSpace(opts.Message) || opts.Interval >= 50001) { return; } var startTimeOfDay = dt?.InputTimeUtc.TimeOfDay; // if interval not null, that means user specified it (don't change it) // if interval is null set the default to: // if time of day is specified: 24 * 60 (24h) // else 5 var realInterval = opts.Interval ?? (startTimeOfDay is null ? 5 : 24 * 60); var toAdd = new Repeater() { ChannelId = ctx.Channel.Id, GuildId = ctx.Guild.Id, Interval = TimeSpan.FromMinutes(realInterval), Message = opts.Message, NoRedundant = opts.NoRedundant, StartTimeOfDay = startTimeOfDay, }; using (var uow = _db.GetDbContext()) { var gc = uow.GuildConfigs.ForId(ctx.Guild.Id, set => set.Include(x => x.GuildRepeaters)); if (gc.GuildRepeaters.Count >= 5) { return; } gc.GuildRepeaters.Add(toAdd); await uow.SaveChangesAsync(); } var runner = new RepeatRunner((SocketGuild)ctx.Guild, toAdd, _service); _service.Repeaters.AddOrUpdate(ctx.Guild.Id, new ConcurrentDictionary <int, RepeatRunner>(new[] { new KeyValuePair <int, RepeatRunner>(toAdd.Id, runner) }), (key, old) => { old.TryAdd(runner.Repeater.Id, runner); return(old); }); var description = GetRepeaterInfoString(runner); await ctx.Channel.EmbedAsync(new EmbedBuilder() .WithOkColor() .WithTitle(GetText("repeater_created")) .WithDescription(description)); }