Ejemplo n.º 1
0
        public EventAndNotifications GetEventDetails(ulong gid, ulong evid)
        {
            using (var database = new GuildDB())
            {
                var events = (from g in database.Guilds
                              where g.GuildId == gid
                              select g.Events).FirstOrDefault();

                if (events == null)
                {
                    return(null);
                }

                EventTB ev = events.FirstOrDefault(x => x.EventId == evid);
                if (ev == null)
                {
                    return(null);
                }

                var notifications = (from n in database.EventNotifications
                                     where n.Event == ev
                                     select new Notification {
                    Date = n.Date, Response = n.ResponseKeyword
                }).ToArray();

                return(new EventAndNotifications {
                    Event = new Event {
                        Id = ev.EventId, Date = ev.Date, Name = ev.Name
                    }, Notifications = notifications
                });
            }
        }
Ejemplo n.º 2
0
        public bool Cancel(EventTB Event, GuildDB database)
        {
            // cancel the notifications
            CancelNotifier(Event);

            // remove from the database
            return(RemoveEventFromDatabase(Event, database));
        }
Ejemplo n.º 3
0
 private void CancelNotifier(EventTB ev)
 {
     if (!notifiercollection.Remove(ev.EventId, out CancellationTokenSource token))
     {
         logger.Log(new LogMessage(LogSeverity.Warning, "Agenda", $"Something went wrong while cancelling the event {ev.Name}"));
     }
     token.Cancel();
 }
Ejemplo n.º 4
0
        public async Task Events(EventSpecifier eventSpecifier, [Remainder] string rest = null)
        {
            using (var database = new GuildDB())
            {
                // log execution
                CommandMethods.LogExecution(logger, "get event", Context);

                // indicate that the command is being worked on
                await Context.Channel.TriggerTypingAsync();

                GuildTB         gtb      = statecollection.GetGuildEntry(Context.Guild, database);
                StringConverter language = statecollection.GetLanguage(Context.Guild, database, gtb);

                // make sure that the user has the right permissions
                if (!PermissionHelper.UserHasPermission(Context.User as SocketGuildUser, PermissionHelper.Member, database))
                {
                    await Context.Channel.SendMessageAsync(language.GetString("command.nopermission"));

                    return;
                }

                var events = agenda.GetEvents(Context.Guild, database);

                // make sure that the agenda is not empty
                if (events.Count() == 0)
                {
                    await Context.Channel.SendMessageAsync(language.GetString("command.event.empty"));

                    return;
                }

                // create an embed
                EmbedBuilder eb = new EmbedBuilder()
                                  .WithColor(Color.DarkRed);

                switch (eventSpecifier)
                {
                case EventSpecifier.All:
                    eb.Title = $":calendar_spiral: All events for '{Context.Guild.Name}'";
                    foreach (EventTB e in events)
                    {
                        eb.AddField(e.Name, e.Date.ToString(@"dd MMMM \a\t hh:mm tt UTC"));
                    }
                    break;

                case EventSpecifier.First:
                    eb.Title = $":calendar_spiral: First event for '{Context.Guild.Name}'";
                    EventTB ev = events.First();
                    eb.AddField(ev.Name, ev.Date.ToString(@"dd MMMM \a\t hh:mm tt UTC"));
                    break;
                }

                Embed embed = eb.Build();
                await Context.Channel.SendMessageAsync(language.GetString("present"), embed : embed);
            }
        }
Ejemplo n.º 5
0
        private EventAndNotifications StoreEventInDatabase(SocketGuild guild, GuildDB database, string name, DateTime date, SocketTextChannel channel = null, bool doNotifications = true, IEnumerable <TimeSpan> notifications = null)
        {
            // Get the data for this guild
            GuildTB g = statecollection.GetGuildEntry(guild, database);

            // first plan the event
            EventTB ev = new EventTB
            {
                Guild = g,
                Name  = name,
                Date  = date,
                NotificationChannel = channel != null ? (ulong?)channel.Id : null,
            };

            IEnumerable <EventNotificationTB> ens = null;

            if (doNotifications)
            {
                ens = CreateNotificationEntries(ev, notifications);
            }

            ev.Notifications = ens.ToArray();

            database.Events.Add(ev);
            try
            {
                // try to save addition to database
                database.SaveChanges();
            }
            catch (Exception e)
            {
                // log failure
                logger.Log(new LogMessage(LogSeverity.Error, "Agenda", $"Attempted to write event to database, but failed: {e.Message}\n{e.StackTrace}"));
            }

            // return the event and the notifications
            return(new EventAndNotifications
            {
                Event = ev,
                Notifications = ens,
            });
        }
Ejemplo n.º 6
0
        private SocketTextChannel GetChannel(SocketGuild guild, EventTB ev)
        {
            // return null if no channel has been set
            if (ev.NotificationChannel == null)
            {
                return(null);
            }

            try
            {
                // try to get the channel from the guild
                return(guild.GetTextChannel(ev.NotificationChannel.Value));
            }
            catch (Exception e)
            {
                // log failure
                logger.Log(new LogMessage(LogSeverity.Warning, "Agenda", $"Attempted to get channel from '{guild.Name}', but failed: {e.Message}\n{e.StackTrace}"));
                return(null);
            }
        }
Ejemplo n.º 7
0
        private IEnumerable <EventNotificationTB> CreateNotificationEntries(EventTB ev, IEnumerable <TimeSpan> notifications)
        {
            // first return all the notifications in the list
            foreach (var ts in notifications)
            {
                yield return(new EventNotificationTB
                {
                    Date = ev.Date - ts,
                    Event = ev,
                    ResponseKeyword = "notifications.timeleft",
                });
            }

            // return a final notification on the deadline itself
            yield return(new EventNotificationTB
            {
                Date = ev.Date,
                Event = ev,
                ResponseKeyword = "notifications.deadline",
            });
        }
Ejemplo n.º 8
0
        private bool CheckEventValidity(EventTB ev, IEnumerable <EventNotificationTB> ens, GuildDB database)
        {
            // check if this event has already passed
            if (ev.Date < DateTime.UtcNow)
            {
                // log removal
                logger.Log(new LogMessage(LogSeverity.Info, "Agenda", $"event '{ev.Name}' is in the past and will be removed from the agenda."));

                // remove from the database
                database.EventNotifications.RemoveRange(ens);
                database.Events.Remove(ev);
                try
                {
                    database.SaveChanges();
                }
                catch (Exception e)
                {
                    logger.Log(new LogMessage(LogSeverity.Warning, "Agenda", $"Attempted to remove invalid event from database, but failed: {e.Message}\n{e.StackTrace}"));
                }
                return(false);
            }
            return(true);
        }
Ejemplo n.º 9
0
        private bool RemoveEventFromDatabase(EventTB ev, GuildDB database)
        {
            // get all the notifications from the database
            var en = from e in database.EventNotifications
                     where e.Event.EventId == ev.EventId
                     select e;

            database.EventNotifications.RemoveRange(en);
            database.Events.Remove(ev);

            try
            {
                // try to apply removal to database
                database.SaveChanges();
                return(true);
            }
            catch (Exception e)
            {
                // log failure
                logger.Log(new LogMessage(LogSeverity.Error, "Agenda", $"Attempted to remove event from database, but failed: {e.Message}\n{e.StackTrace}"));
                return(false);
            }
        }