Beispiel #1
0
            public async Task Delete(CommandContext ctx, int index)
            {
                await ctx.TriggerTypingAsync();

                if (!await this.CheckPermission(_permissionService, typeof(DeleteCommands), nameof(DeleteCommands.Delete), ctx.Member))
                {
                    await ctx.RespondAsync("You are not permitted to use this command.");

                    return;
                }

                if (index <= 0)
                {
                    await ctx.RespondAsync("Event index must be greater than 0.");

                    return;
                }

                Event deletedEvent;

                try
                {
                    deletedEvent = await _eventService.DeleteEventAsync(ctx.Guild.Id, index - 1);
                }
                catch (ArgumentOutOfRangeException)
                {
                    await ctx.RespondAsync("Event not found.");

                    return;
                }
                catch (EventNotFoundException)
                {
                    await ctx.RespondAsync("Event not found.");

                    return;
                }
                catch (CalendarNotFoundException)
                {
                    await ctx.RespondAsync("Calendar not initialised. Run `init <timezone>` to initialise the calendar.");

                    return;
                }

                if (deletedEvent == null)
                {
                    await ctx.RespondAsync("Event not found.");

                    return;
                }

                await _eventScheduler.UnscheduleEvent(deletedEvent);

                var embed = EventEmbedFactory.GetDeleteEventEmbed(deletedEvent);
                await ctx.RespondAsync("Deleted event.", embed : embed);
            }
Beispiel #2
0
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                JobDataMap jobDataMap = context.MergedJobDataMap;
                var        evt        = (Event)jobDataMap["event"];
                var        client     = (DiscordClient)jobDataMap["client"];
                var        channel    = (DiscordChannel)jobDataMap["channel"];

                var embed = EventEmbedFactory.GetNotifyEventEmbed(evt);
                if (evt.Mentions != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var mention in evt.Mentions)
                    {
                        switch (mention.Type)
                        {
                        case Data.Models.MentionType.Role:
                            sb.Append($"{mention.TargetId.AsRoleMention()} ");
                            break;

                        case Data.Models.MentionType.User:
                            sb.Append($"{mention.TargetId.AsUserMention()} ");
                            break;

                        case Data.Models.MentionType.Everyone:
                            sb.Append("@everyone");
                            break;

                        case Data.Models.MentionType.RSVP:
                            foreach (var rsvp in evt.RSVPs)
                            {
                                sb.Append($"{rsvp.UserId.AsUserMention()} ");
                            }
                            break;
                        }
                    }
                    await client.SendMessageAsync(channel, sb.ToString(), embed : embed);

                    return;
                }

                await client.SendMessageAsync(channel, embed : embed);
            }
            catch (UnauthorizedException) { }
            catch
            {
                throw;
            }
        }
Beispiel #3
0
        public async Task ListOne(CommandContext ctx, int index)
        {
            await ctx.TriggerTypingAsync();

            if (!await this.CheckPermission(_permissionService, typeof(EventCommands), nameof(EventCommands.ListOne), ctx.Member))
            {
                await ctx.RespondAsync("You are not permitted to use this command.");

                return;
            }

            if (index <= 0)
            {
                await ctx.RespondAsync("Event index must be greater than 0.");

                return;
            }

            Event evt;

            try
            {
                evt = await _eventService.GetEventByIndexAsync(ctx.Guild.Id, index - 1);
            }
            catch (ArgumentOutOfRangeException)
            {
                await ctx.RespondAsync("Event not found.");

                return;
            }
            catch (CalendarNotFoundException)
            {
                await ctx.RespondAsync("Calendar not initialised. Run `init <timezone>` to initialise the calendar.");

                return;
            }

            if (evt == null)
            {
                await ctx.RespondAsync("Event not found.");

                return;
            }

            var embed = EventEmbedFactory.GetViewEventEmbed(evt);
            await ctx.RespondAsync(embed : embed);
        }
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                JobDataMap jobDataMap = context.MergedJobDataMap;
                var        evt        = (Event)jobDataMap["event"];
                var        client     = (DiscordClient)jobDataMap["client"];
                var        channel    = (DiscordChannel)jobDataMap["channel"];

                var embed = EventEmbedFactory.GetRemindEventEmbed(evt);
                await client.SendMessageAsync(channel, embed : embed);
            }
            catch (UnauthorizedException) { }
            catch
            {
                throw;
            }
        }
Beispiel #5
0
        public async Task Create(CommandContext ctx, params string[] args)
        {
            await ctx.TriggerTypingAsync();

            if (!await this.CheckPermission(_permissionService, typeof(EventCommands), nameof(EventCommands.Create), ctx.Member))
            {
                await ctx.RespondAsync("You are not permitted to use this command.");

                return;
            }

            var timezone = await _calendarService.GetCalendarTimezoneAsync(ctx.Guild.Id);

            if (string.IsNullOrEmpty(timezone))
            {
                await ctx.RespondAsync("Calendar not initialised. Run `init <timezone>` to initialise the calendar.");

                return;
            }

            Event evt;

            try
            {
                evt = EventParser.ParseNewEvent(args, timezone);
            }
            catch (DateTimeInPastException)
            {
                await ctx.RespondAsync("Cannot create an event that starts or ends in the past, or has a reminder that is in the past.");

                return;
            }
            catch (EventEndBeforeStartException)
            {
                await ctx.RespondAsync("Cannot create an event that ends before it starts.");

                return;
            }
            catch (EventParseException)
            {
                await ctx.RespondAsync("Failed to parse event data.");

                return;
            }

            Event savedEvent;

            try
            {
                savedEvent = await _eventService.CreateEventAsync(ctx.Guild.Id, evt);
            }
            catch (CalendarNotFoundException)
            {
                await ctx.RespondAsync("Calendar not initialised. Run `init <timezone>` to initialise the calendar.");

                return;
            }

            var defaultChannelId = await _calendarService.GetCalendarDefaultChannelAsync(ctx.Guild.Id);

            await _eventScheduler.ScheduleEvent(savedEvent, ctx.Client, defaultChannelId, ctx.Guild.Id);

            var embed = EventEmbedFactory.GetCreateEventEmbed(savedEvent);
            await ctx.RespondAsync("New event created.", embed : embed);
        }
Beispiel #6
0
        public async Task Update(CommandContext ctx, int index, [RemainingText] string args)
        {
            await ctx.TriggerTypingAsync();

            if (!await this.CheckPermission(_permissionService, typeof(EventCommands), nameof(EventCommands.Update), ctx.Member))
            {
                await ctx.RespondAsync("You are not permitted to use this command.");

                return;
            }

            if (index <= 0)
            {
                await ctx.RespondAsync("Event index must be greater than 0.");

                return;
            }

            if (string.IsNullOrEmpty(args))
            {
                await ctx.RespondAsync("No arguments given for updating the event.");

                return;
            }

            var timezone = await _calendarService.GetCalendarTimezoneAsync(ctx.Guild.Id);

            if (string.IsNullOrEmpty(timezone))
            {
                await ctx.RespondAsync("Calendar not initialised. Run `init <timezone>` to initialise the calendar.");

                return;
            }

            Event evt;

            try
            {
                evt = await _eventService.GetEventByIndexAsync(ctx.Guild.Id, index - 1);
            }
            catch (ArgumentOutOfRangeException)
            {
                await ctx.RespondAsync("Event not found.");

                return;
            }

            if (evt.HasStarted())
            {
                await ctx.RespondAsync("Cannot update an event that is in progress.");

                return;
            }

            Event updatedEvent;

            try
            {
                updatedEvent = EventParser.ParseUpdateEvent(evt, args, timezone);
            }
            catch (DateTimeInPastException)
            {
                await ctx.RespondAsync("Cannot create an event that starts or ends in the past, or has a reminder that is in the past.");

                return;
            }
            catch (EventEndBeforeStartException)
            {
                await ctx.RespondAsync("Cannot create an event that ends before it starts.");

                return;
            }
            catch (EventParseException)
            {
                await ctx.RespondAsync("Failed to parse event data.");

                return;
            }

            Event savedEvent;

            savedEvent = await _eventService.UpdateEventAsync(evt);

            var defaultChannelId = await _calendarService.GetCalendarDefaultChannelAsync(ctx.Guild.Id);

            await _eventScheduler.RescheduleEvent(evt, ctx.Client, defaultChannelId);

            var embed = EventEmbedFactory.GetUpdateEventEmbed(savedEvent);
            await ctx.RespondAsync("Updated event.", embed : embed);
        }