예제 #1
0
        public async Task ReminderBase(CommandContext ctx,
                                       string action,
                                       [RemainingText]
                                       string args = @"")
        {
            // Check if they have the permissions to call this command.
            if (await Permissions.HandlePermissionsCheck(ctx))
            {
                switch (action)
                {
                case "new":
                case "add":
                    await ReminderSystem.AddReminder(ctx, args);

                    break;

                case "remove":
                case "delete":
                    // Check if this is even a reminder.

                    Reminder possibleReminder = await ReminderSystem.GetReminderFromDatabase(args);

                    if (!possibleReminder.Equals(Reminder.Invalid))
                    {       // It's a reminder.
                        await ReminderSystem.RemoveReminder(ctx, possibleReminder);
                    }
                    else
                    {       // It's not a reminder.
                        await GenericResponses.SendGenericCommandError(
                            ctx.Channel,
                            ctx.Member.Mention,
                            "Unable to remove reminder",
                            $"The reminder id `{args}` does not exist...");
                    }
                    break;

                case "list":
                    await ReminderSystem.ListReminders(ctx);

                    break;
                }
            }
        }
예제 #2
0
        static RemindUtility()
        {
            Ditto.Exit += () =>
            {
                _cancellationTokenSource?.Cancel();
                _reminders.Clear();
                return(Task.CompletedTask);
            };

            Ditto.Connected += () =>
            {
                _cancellationTokenSource?.Cancel();
                _cancellationTokenSource = new CancellationTokenSource();
                var initialTime = DateTime.Now;

                // Load intiial reminders to _reminders
                IEnumerable <Reminder> remindersDb = null;
                Ditto.Database.Read((uow) =>
                {
                    remindersDb = uow.Reminders.GetAll();
                });
                foreach (var reminder in remindersDb)
                {
                    _reminders.TryAdd(reminder.Id, reminder);
                }

                // Start reading reminders
                try
                {
                    Task.Run(async() =>
                    {
                        while (Ditto.Running)
                        {
                            try
                            {
                                foreach (var reminderPair in _reminders.ToList())
                                {
                                    // check our reminder
                                    var reminder = reminderPair.Value;
                                    if (DateTime.Now > reminder.EndTime)
                                    {
                                        // Verify that the reminder has not already been removed
                                        Reminder reminderDb = null;
                                        await Ditto.Database.ReadAsync((uow) =>
                                        {
                                            reminderDb = uow.Reminders.Get(reminderPair.Key);
                                        });

                                        // If the value is no longer in our database, remove it from our local records.
                                        if (reminderDb == null)
                                        {
                                            _reminders.TryRemove(reminderPair.Key, out Reminder value);
                                            continue;
                                        }
                                        // if for some reason the database version differs, update our local value.
                                        else if (!reminderDb.Equals(reminder))
                                        {
                                            _reminders.TryUpdate(reminderPair.Key, reminderDb, reminder);
                                            continue;
                                        }

                                        // Execute
                                        try
                                        {
                                            var dmChannels = new List <IDMChannel>();
                                            if (reminder.RoleId != null)
                                            {
                                                dmChannels.AddRange(
                                                    (await reminder.Guild.GetUsersAsync())
                                                    .Where(r => r.RoleIds.ToList().Contains(reminder.RoleId.Value))
                                                    .Where(u => !u.IsBot)
                                                    .Select(async u => await u.CreateDMChannelAsync())
                                                    .Select(t => t.Result)
                                                    );
                                            }
                                            else if (reminder.ChannelId != null && reminder.Channel is IVoiceChannel)
                                            {
                                                // target everyone in a specific voice channel
                                                dmChannels.AddRange(
                                                    (await(reminder.Channel as IVoiceChannel).GetUsersAsync().ToListAsync()).SelectMany(e => e)
                                                    .Where(u => !u.IsBot)
                                                    .Select(async u => await u.CreateDMChannelAsync()).Select(t => t.Result)
                                                    );
                                            }
                                            else if (reminder.UserId != null)
                                            {
                                                dmChannels.Add(await reminder.User?.CreateDMChannelAsync());
                                            }

                                            var embedBuilder = reminder.Message.Contains("\n") ?
                                                               new EmbedBuilder()
                                                               .WithColor(250, 166, 26) // default mention colour
                                                               .WithTitle("\\❗\\⏰ Reminder \\⏰\\❗")
                                                               .WithDescription($"**{reminder.Message}**")
                                                               .WithFooter(new EmbedFooterBuilder()
                                                                           .WithText(reminder.Self ? "" : $"sent by {reminder.Creator}")
                                                                           )
                                                : null;

                                            var textMessage = $"\\❗⏰ Hey ${{USER}}, {(reminder.Self ? "it's time" : $"I was told to remind you by **{reminder.Creator}**")} to **{reminder.Message}** ⏰\\❗";
                                            if (dmChannels.Count > 0)
                                            {
                                                foreach (var dmChannel in dmChannels)
                                                {
                                                    await(embedBuilder != null ?
                                                          dmChannel.EmbedAsync(embedBuilder, $"Hey {dmChannel.Recipient.Mention}")
                                                        : dmChannel.SendMessageAsync(textMessage.Replace("${USER}", dmChannel.Recipient.Mention))
                                                          );
                                                }
                                            }
                                            else
                                            {
                                                // In case we just don't have any targets
                                                if (reminder.Channel is IMessageChannel channel)
                                                {
                                                    await(embedBuilder != null ?
                                                          channel.EmbedAsync(embedBuilder, $"Hey {channel.Mention()}")
                                                        : channel.SendMessageAsync(textMessage.Replace("${USER}", channel.Mention()))
                                                          );
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Log.Error(ex);
                                        }

                                        if (reminder.Repeat)
                                        {
                                            // Update
                                            var timeDifference = reminderDb.EndTime - reminderDb.StartTime;
                                            var startTime      = DateTime.Now;
                                            var endTime        = startTime + timeDifference;

                                            await Ditto.Database.WriteAsync((uow) =>
                                            {
                                                reminderDb = uow.Reminders.Get(reminderPair.Key);
                                                if (reminderDb != null)
                                                {
                                                    reminderDb.StartTime = startTime;
                                                    reminderDb.EndTime   = endTime;
                                                }
                                            }).ConfigureAwait(false);

                                            if (_reminders.TryGetValue(reminderPair.Key, out Reminder value))
                                            {
                                                value.StartTime = startTime;
                                                value.EndTime   = endTime;
                                            }
                                        }
                                        else
                                        {
                                            // Remove
                                            await Ditto.Database.WriteAsync((uow) =>
                                            {
                                                uow.Reminders.Remove(reminderPair.Key);
                                            });
                                            _reminders.TryRemove(reminderPair.Key, out Reminder value);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex);
                            }
                            await Task.Delay(Delay);
                        }
                    }, _cancellationTokenSource.Token);
        public bool Equals(ContentTypeDescription input)
        {
            if (input == null)
            {
                return(false);
            }

            return
                ((
                     CType == input.CType ||
                     (CType != null && CType.Equals(input.CType))
                     ) &&
                 (
                     Name == input.Name ||
                     (Name != null && Name.Equals(input.Name))
                 ) &&
                 (
                     ContentDescription == input.ContentDescription ||
                     (ContentDescription != null && ContentDescription.Equals(input.ContentDescription))
                 ) &&
                 (
                     PreviewImage == input.PreviewImage ||
                     (PreviewImage != null && PreviewImage.Equals(input.PreviewImage))
                 ) &&
                 (
                     Priority == input.Priority ||
                     (Priority.Equals(input.Priority))
                 ) &&
                 (
                     Reminder == input.Reminder ||
                     (Reminder != null && Reminder.Equals(input.Reminder))
                 ) &&
                 (
                     Properties == input.Properties ||
                     (Properties != null && Properties.SequenceEqual(input.Properties))
                 ) &&
                 (
                     TagMetadata == input.TagMetadata ||
                     (TagMetadata != null && TagMetadata.SequenceEqual(input.TagMetadata))
                 ) &&
                 (
                     TagMetadataItems == input.TagMetadataItems ||
                     (TagMetadataItems != null && TagMetadataItems.SequenceEqual(input.TagMetadataItems))
                 ) &&
                 (
                     UsageExamples == input.UsageExamples ||
                     (UsageExamples != null && UsageExamples.SequenceEqual(input.UsageExamples))
                 ) &&
                 (
                     ShowInContentEditor == input.ShowInContentEditor ||
                     (ShowInContentEditor != null && ShowInContentEditor.Equals(input.ShowInContentEditor))
                 ) &&
                 (
                     TypeOf == input.TypeOf ||
                     (TypeOf != null && TypeOf.Equals(input.TypeOf))
                 ) &&
                 (
                     BindIdentifierToProperty == input.BindIdentifierToProperty ||
                     (BindIdentifierToProperty != null && BindIdentifierToProperty.Equals(input.BindIdentifierToProperty))
                 ) &&
                 (
                     BoundRegex == input.BoundRegex ||
                     (BoundRegex != null && BoundRegex.Equals(input.BoundRegex))
                 ) &&
                 (
                     ForceIdentifierBinding == input.ForceIdentifierBinding ||
                     (ForceIdentifierBinding != null && ForceIdentifierBinding.Equals(input.ForceIdentifierBinding))
                 ) &&
                 (
                     AllowComments == input.AllowComments ||
                     (AllowComments != null && AllowComments.Equals(input.AllowComments))
                 ) &&
                 (
                     AutoEnglishPropertyFallback == input.AutoEnglishPropertyFallback ||
                     (AutoEnglishPropertyFallback != null && AutoEnglishPropertyFallback.Equals(input.AutoEnglishPropertyFallback))
                 ) &&
                 (
                     BulkUploadable == input.BulkUploadable ||
                     (BulkUploadable != null && BulkUploadable.Equals(input.BulkUploadable))
                 ) &&
                 (
                     Previews == input.Previews ||
                     (Previews != null && Previews.SequenceEqual(input.Previews))
                 ) &&
                 (
                     SuppressCmsPath == input.SuppressCmsPath ||
                     (SuppressCmsPath != null && SuppressCmsPath.Equals(input.SuppressCmsPath))
                 ) &&
                 (
                     PropertySections == input.PropertySections ||
                     (PropertySections != null && PropertySections.SequenceEqual(input.PropertySections))
                 ));
        }