Ejemplo n.º 1
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (!(data is DiscordMessage message))
            {
                return;
            }
            if (message.IsDm())
            {
                return;
            }

            bool isSnippetChannel = false;

            foreach (ChannelLink link in DLConfig.Data.SnippetChannels)
            {
                if (!link.IsValid())
                {
                    continue;
                }

                if (link.DiscordGuild.ToLower() == message.Channel.Guild.Name.ToLower() &&
                    link.DiscordChannel == message.Channel.Name)
                {
                    isSnippetChannel = true;
                    break;
                }
            }
            if (isSnippetChannel)
            {
                await ReloadSnippets();
            }
        }
Ejemplo n.º 2
0
        public void HandleEvent(DLEventType eventType, object data)
        {
            switch (eventType)
            {
            case DLEventType.Trade:
                if (!(data is CurrencyTrade tradeEvent))
                {
                    return;
                }

                // Store the event in a list in order to accumulate trade events that should be considered as one. We do this as each item in a trade will fire an individual event and we want to summarize them
                Tuple <int, int> IDTuple = new Tuple <int, int>(tradeEvent.Citizen.Id, (tradeEvent.WorldObject as WorldObject).ID);
                _accumulatedTrades.TryGetValue(IDTuple, out List <CurrencyTrade> trades);
                if (trades == null)
                {
                    trades = new List <CurrencyTrade>();
                    _accumulatedTrades.Add(IDTuple, trades);
                }

                trades.Add(tradeEvent);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 3
0
 private void FireEvent(DLEventType evetType, object data)
 {
     if (OnEventFired != null)
     {
         OnEventFired.Invoke(this, new DLEventArgs(evetType, data));
     }
 }
Ejemplo n.º 4
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (!Initialized)
            {
                return;
            }

            string username;
            string content;

            if (data is ChatSent ecoMessage)
            {
                username = ecoMessage.Citizen.Name;
                content  = ecoMessage.Message;
            }
            else if (data is DiscordMessage discordMessage)
            {
                username = discordMessage.Author.Username;
                content  = discordMessage.Content;
            }
            else
            {
                return;
            }

            DateTime time      = DateTime.Now;
            int      utcOffset = TimeZoneInfo.Local.GetUtcOffset(time).Hours;

            _writer.WriteLine("[Discord] [" + DateTime.Now.ToString("yyyy-MM-dd : HH:mm", CultureInfo.InvariantCulture) + " UTC " + (utcOffset != 0 ? (utcOffset >= 0 ? "+" : "-") + utcOffset : "") + "] "
                              + $"{MessageUtil.StripTags(username) + ": " + MessageUtil.StripTags(content)}");
            ++_opsCount;
        }
Ejemplo n.º 5
0
        public void HandleEvent(DLEventType eventType, object data)
        {
            switch (eventType)
            {
            // Keep track of the amount of trades per currency
            case DLEventType.AccumulatedTrade:
                if (!(data is IEnumerable <List <CurrencyTrade> > accumulatedTrade))
                {
                    return;
                }

                foreach (var list in accumulatedTrade)
                {
                    if (list.Count <= 0)
                    {
                        continue;
                    }

                    // Make sure an entry exists for the currency
                    int currencyID = accumulatedTrade.First()[0].Currency.Id;
                    if (!WorldData.CurrencyToTradeCountMap.ContainsKey(currencyID))
                    {
                        WorldData.CurrencyToTradeCountMap.Add(currencyID, 0);
                    }

                    WorldData.CurrencyToTradeCountMap.TryGetValue(currencyID, out int tradeCount);
                    WorldData.CurrencyToTradeCountMap[currencyID] = tradeCount + 1;
                }
                break;

            default:
                break;
            }
        }
Ejemplo n.º 6
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (!(data is DiscordMessage message))
            {
                return;
            }

            ChatChannelLink channelLink = plugin.GetLinkForEcoChannel(message.Channel.Name) ?? plugin.GetLinkForEcoChannel(message.Channel.Id.ToString());
            string          channel     = channelLink?.EcoChannel;

            if (string.IsNullOrWhiteSpace(channel))
            {
                return;
            }

            if (channelLink.Direction == ChatSyncDirection.DiscordToEco || channelLink.Direction == ChatSyncDirection.Duplex)
            {
                await ForwardMessageToEcoChannel(plugin, message, channel);
            }
        }
Ejemplo n.º 7
0
        public virtual async Task Update(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (plugin == null)
            {
                return;
            }

            // Check if this module should execute on the supplied trigger
            if ((GetTriggers() & trigger) == 0)
            {
                return;
            }

            using (await _overlapLock.LockAsync()) // Make sure that the Update function doesn't get overlapping executions
            {
                if (_isShuttingDown)
                {
                    return;
                }
                await UpdateInternal(plugin, trigger, data);
            }
        }
Ejemplo n.º 8
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (!(data is ChatSent message))
            {
                return;
            }

            // Remove the # character from the start.
            var channelLink = plugin.GetLinkForDiscordChannel(message.Tag.Substring(1));
            var channel     = channelLink?.DiscordChannel;
            var guild       = channelLink?.DiscordGuild;

            if (string.IsNullOrWhiteSpace(channel) || string.IsNullOrWhiteSpace(guild))
            {
                return;
            }

            if (channelLink.Direction == ChatSyncDirection.EcoToDiscord || channelLink.Direction == ChatSyncDirection.Duplex)
            {
                ForwardMessageToDiscordChannel(plugin, message, channel, guild, channelLink.HereAndEveryoneMentionPermission);
            }
        }
Ejemplo n.º 9
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (!(data is WorkOrderAction craftingEvent))
            {
                return;
            }
            if (craftingEvent.Citizen == null)
            {
                return;                                // Happens when a crafting table contiues crafting after finishing an item
            }
            if (craftingEvent.MarkedUpName != "Create Work Order")
            {
                return;                                                    // Happens when a player feeds materials to a blocked work order
            }
            string itemName = craftingEvent.OrderCount > 1 ? craftingEvent.CraftedItem.DisplayNamePlural : craftingEvent.CraftedItem.DisplayName;
            string message  = $"**{craftingEvent.Citizen.Name}** started crafting {craftingEvent.OrderCount} `{itemName}` at {(craftingEvent.WorldObject as WorldObject).Name}.";

            foreach (ChannelLink craftingChannel in DLConfig.Data.CraftingChannels)
            {
                if (!craftingChannel.IsValid())
                {
                    continue;
                }
                DiscordGuild discordGuild = plugin.GuildByNameOrId(craftingChannel.DiscordGuild);
                if (discordGuild == null)
                {
                    continue;
                }
                DiscordChannel discordChannel = discordGuild.ChannelByNameOrId(craftingChannel.DiscordChannel);
                if (discordChannel == null)
                {
                    continue;
                }
                await DiscordUtil.SendAsync(discordChannel, message);

                ++_opsCount;
            }
        }
Ejemplo n.º 10
0
        protected sealed override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            // Avoid hitting the rate limitation by not allowig events that can be fired often to pass straight through.
            if ((trigger & HighFrequencyTriggerFlags) == trigger)
            {
                if (_HighFrequencyEventTimer == null)
                {
                    _HighFrequencyEventTimer = new Timer(this.TriggerTimedUpdate, null, HighFrequencyEventDelayMS, Timeout.Infinite);
                }
                return;
            }

            if (_dirty || _targetDisplays.Count <= 0)
            {
                await FindMessages(plugin);

                if (_dirty || _targetDisplays.Count <= 0)
                {
                    return;                                       // If something went wrong, we should just retry later
                }
            }

            bool                  createdOrDestroyedMessage = false;
            List <string>         matchedTags       = new List <string>();
            List <DiscordMessage> unmatchedMessages = new List <DiscordMessage>();

            foreach (TargetDisplayData channelDisplayData in _targetDisplays)
            {
                DiscordTarget target      = channelDisplayData.Target;
                ChannelLink   channelLink = target as ChannelLink;
                UserLink      userLink    = target as UserLink;
                if (channelLink == null && userLink == null)
                {
                    continue;
                }

                DiscordChannel targetChannel = null;
                if (channelLink != null)
                {
                    // Get the channel and verify permissions
                    DiscordGuild discordGuild = plugin.GuildByNameOrId(channelLink.DiscordGuild);
                    if (discordGuild == null)
                    {
                        continue;
                    }
                    targetChannel = discordGuild.ChannelByNameOrId(channelLink.DiscordChannel);
                    if (targetChannel == null)
                    {
                        continue;
                    }
                    if (!DiscordUtil.ChannelHasPermission(targetChannel, Permissions.ReadMessageHistory))
                    {
                        continue;
                    }
                }
                else if (userLink != null)
                {
                    targetChannel = await userLink.Member.CreateDmChannelAsync();
                }

                GetDisplayContent(target, out List <Tuple <string, DiscordLinkEmbed> > tagsAndContent);

                foreach (ulong messageID in channelDisplayData.MessageIDs)
                {
                    DiscordMessage message = await DiscordUtil.GetMessageAsync(targetChannel, messageID);

                    if (message == null)
                    {
                        _dirty = true;
                        return; // We cannot know which messages are wrong and duplicates may be created if we continue.
                    }
                    if (!message.Content.StartsWith(BaseTag))
                    {
                        continue;                                       // The message belongs to a different display
                    }
                    bool found = false;
                    foreach (var tagAndContent in tagsAndContent)
                    {
                        if (message.Content.Contains(tagAndContent.Item1))
                        {
                            _ = DiscordUtil.ModifyAsync(message, tagAndContent.Item1, tagAndContent.Item2);
                            matchedTags.Add(tagAndContent.Item1);
                            found = true;
                            ++_opsCount;
                            break;
                        }
                    }

                    if (!found)
                    {
                        unmatchedMessages.Add(message);
                    }
                }

                // Delete the messages that are no longer relevant
                foreach (DiscordMessage message in unmatchedMessages)
                {
                    DiscordUtil.DeleteAsync(message).Wait();
                    createdOrDestroyedMessage = true;
                    ++_opsCount;
                }
                unmatchedMessages.Clear();

                // Send the messages that didn't already exist
                foreach (var tagAndContent in tagsAndContent)
                {
                    if (!matchedTags.Contains(tagAndContent.Item1))
                    {
                        DiscordUtil.SendAsync(targetChannel, tagAndContent.Item1, tagAndContent.Item2).Wait();
                        createdOrDestroyedMessage = true;
                        ++_opsCount;
                    }
                }
                matchedTags.Clear();
            }

            if (createdOrDestroyedMessage)
            {
                await FindMessages(plugin);
            }

            LastUpdateTime = DateTime.Now;
        }
Ejemplo n.º 11
0
 protected abstract Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data);
Ejemplo n.º 12
0
 public DLEventArgs(DLEventType eventType, object data)
 {
     EventType = eventType;
     Data      = data;
 }
Ejemplo n.º 13
0
 private void UpdateModules(DLEventType trigger, object data)
 {
     Modules.ForEach(async module => await module.Update(this, trigger, data));
 }
Ejemplo n.º 14
0
 public void HandleEvent(DLEventType eventType, object data)
 {
     EventConverter.Instance.HandleEvent(eventType, data);
     DLStorage.Instance.HandleEvent(eventType, data);
     UpdateModules(eventType, data);
 }
Ejemplo n.º 15
0
        protected override async Task UpdateInternal(DiscordLink plugin, DLEventType trigger, object data)
        {
            if (DLConfig.Data.TradeChannels.Count <= 0)
            {
                return;
            }
            if (!(data is IEnumerable <List <CurrencyTrade> > accumulatedTrades))
            {
                return;
            }


            // Each entry is the summarized trade events for a player and a store
            foreach (List <CurrencyTrade> accumulatedTradeList in accumulatedTrades)
            {
                if (accumulatedTradeList.Count <= 0)
                {
                    continue;
                }

                CurrencyTrade firstTrade = accumulatedTradeList[0];

                DiscordLinkEmbed embed     = new DiscordLinkEmbed();
                string           leftName  = firstTrade.Citizen.Name;
                string           rightName = (firstTrade.WorldObject as WorldObject).Name;
                embed.WithTitle($"{leftName} traded at {MessageUtil.StripTags(rightName)}");

                // Go through all acumulated trade events and create a summary
                string boughtItemsDesc = string.Empty;
                float  boughtTotal     = 0;
                string soldItemsDesc   = string.Empty;
                float  soldTotal       = 0;
                foreach (CurrencyTrade trade in accumulatedTradeList)
                {
                    if (trade.BoughtOrSold == Shared.Items.BoughtOrSold.Buying)
                    {
                        boughtItemsDesc += trade.NumberOfItems + " X " + trade.ItemUsed.DisplayName + " * " + trade.CurrencyAmount / trade.NumberOfItems + " = " + trade.CurrencyAmount + "\n";
                        boughtTotal     += trade.CurrencyAmount;
                    }
                    else if (trade.BoughtOrSold == Shared.Items.BoughtOrSold.Selling)
                    {
                        soldItemsDesc += trade.NumberOfItems + " X " + trade.ItemUsed.DisplayName + " * " + trade.CurrencyAmount / trade.NumberOfItems + " = " + trade.CurrencyAmount + "\n";
                        soldTotal     += trade.CurrencyAmount;
                    }
                }

                if (!boughtItemsDesc.IsEmpty())
                {
                    boughtItemsDesc += "\nTotal = " + boughtTotal.ToString("n2");
                    embed.AddField("Bought", boughtItemsDesc);
                }

                if (!soldItemsDesc.IsEmpty())
                {
                    soldItemsDesc += "\nTotal = " + soldTotal.ToString("n2");
                    embed.AddField("Sold", soldItemsDesc);
                }

                float subTotal = soldTotal - boughtTotal;
                char  sign     = (subTotal > 0.0f ? '+' : '-');
                embed.AddField("Total", sign + Math.Abs(subTotal).ToString("n2") + " " + MessageUtil.StripTags(firstTrade.Currency.Name));

                // Post the trade summary in all trade channels
                foreach (ChannelLink tradeChannel in DLConfig.Data.TradeChannels)
                {
                    if (!tradeChannel.IsValid())
                    {
                        continue;
                    }
                    DiscordGuild discordGuild = plugin.GuildByNameOrId(tradeChannel.DiscordGuild);
                    if (discordGuild == null)
                    {
                        continue;
                    }
                    DiscordChannel discordChannel = discordGuild.ChannelByNameOrId(tradeChannel.DiscordChannel);
                    if (discordChannel == null)
                    {
                        continue;
                    }

                    _ = DiscordUtil.SendAsync(discordChannel, string.Empty, embed);
                    ++_opsCount;
                }
            }
        }