Exemplo n.º 1
0
        public static async Task Post(this Event.Instance self, Event evt)
        {
            SocketTextChannel?channel = evt.GetChannel();

            if (channel is null)
            {
                return;
            }

            EmbedBuilder builder = new EmbedBuilder
            {
                Color       = evt.Color.ToDiscordColor(),
                Title       = evt.Name,
                Description = evt.Description,
                ImageUrl    = evt.Image,
            };

            /*
             * Due to a bug on Android, we cannot use the Timestamp field for dates in the future.
             * If discord ever fixes this, we should use timestamps as they automatically adjust to the clients
             * time zone.
             * https://trello.com/c/RO4zrt25
             */
            /*StringBuilder footerBuilder = new StringBuilder();
             * footerBuilder.Append(TimeUtils.GetDurationString(evt.Duration));
             * footerBuilder.Append(" ");
             * footerBuilder.Append(evt.GetRepeatsString());
             *
             * builder.Footer = new EmbedFooterBuilder();
             * builder.Footer.Text = footerBuilder.ToString();
             * builder.Timestamp = evt.NextOccurance();*/

            StringBuilder descBuilder = new StringBuilder();

            // Starts in: 1 hour 45 minutes
            descBuilder.Append("__");
            descBuilder.Append(evt.GetWhenString());
            descBuilder.AppendLine("__");
            descBuilder.AppendLine();

            Occurance?occurance = evt.GetNextOccurance();

            if (occurance != null)
            {
                descBuilder.AppendLine(occurance.GetDisplayString());
                descBuilder.AppendLine();
            }

            // description
            descBuilder.AppendLine(evt.Description);
            builder.Description = descBuilder.ToString();

            if (evt.StatusType == Event.Statuses.Roles)
            {
                builder.AddStatus(evt, Emotes.Tank);
                builder.AddStatus(evt, Emotes.Healer);
                builder.AddStatus(evt, Emotes.DPS);
            }
            else if (evt.StatusType == Event.Statuses.Attending)
            {
                builder.AddStatus(evt, Emotes.Yes);
                builder.AddStatus(evt, Emotes.Maybe);
                builder.AddStatus(evt, Emotes.No);
            }

            // Notification bell field
            bool shouldBell = true;

            if (occurance != null)
            {
                Duration timeTill = occurance.GetInstant() - TimeUtils.RoundInstant(TimeUtils.Now);
                if (timeTill.TotalMinutes < 60)
                {
                    shouldBell = false;
                }
            }

            if (shouldBell)
            {
                builder.AddField(Emotes.Bell.GetString() + " Notify", evt.GetReminderString());
            }

            RestUserMessage?message = await self.GetMessage(evt);

            if (message is null)
            {
                Log.Write("Posting Instance: \"" + evt.Name + "\" (" + evt.Id + ") in channel: \"" + channel.Name + "\" (" + channel.Id + ")", "Bot");

                message = await channel.SendMessageAsync(evt.Message, false, builder.Build());

                self.MessageId = message.Id.ToString();

                List <IEmote> reactions = new List <IEmote>();
                if (evt.StatusType == Event.Statuses.Roles)
                {
                    reactions.Add(Emotes.Tank);
                    reactions.Add(Emotes.Healer);
                    reactions.Add(Emotes.DPS);
                }
                else
                {
                    reactions.Add(Emotes.Yes);
                    reactions.Add(Emotes.Maybe);
                }

                reactions.Add(Emotes.No);
                reactions.Add(Emotes.Bell);

                await message.AddReactionsAsync(reactions.ToArray());

                EventsService.Instance.Watch(evt);
            }
            else
            {
                Log.Write("Updating Instance: \"" + evt.Name + "\" (" + evt.Id + ") in channel: \"" + channel.Name + "\" (" + channel.Id + ")", "Bot");

                await message.ModifyAsync(x =>
                {
                    x.Embed   = builder.Build();
                    x.Content = evt.Message;
                });
            }
        }