Beispiel #1
0
        private async Task ReloadSnippets()
        {
            DiscordLink plugin = DiscordLink.Obj;

            if (plugin == null)
            {
                return;
            }
            foreach (ChannelLink snippetChannel in DLConfig.Data.SnippetChannels)
            {
                // Fetch the channel and validate permissions
                if (!snippetChannel.IsValid())
                {
                    continue;
                }
                DiscordGuild discordGuild = plugin.GuildByNameOrId(snippetChannel.DiscordGuild);
                if (discordGuild == null)
                {
                    continue;
                }
                DiscordChannel discordChannel = discordGuild.ChannelByNameOrId(snippetChannel.DiscordChannel);
                if (discordChannel == null)
                {
                    continue;
                }
                if (!DiscordUtil.ChannelHasPermission(discordChannel, Permissions.ReadMessageHistory))
                {
                    continue;
                }

                IReadOnlyList <DiscordMessage> snippetChannelMessages = await DiscordUtil.GetMessagesAsync(discordChannel);

                if (snippetChannelMessages == null)
                {
                    continue;
                }

                // Go though all the found messages and look for snippet messages matching our regex
                DLStorage.Instance.Snippets.Clear();
                foreach (DiscordMessage channelMessage in snippetChannelMessages)
                {
                    Match match = MessageUtil.SnippetRegex.Match(channelMessage.Content);
                    if (match.Groups.Count == 3)
                    {
                        DLStorage.Instance.Snippets.Add(match.Groups[1].Value.ToLower(), match.Groups[2].Value);
                    }
                }
            }
        }
Beispiel #2
0
        private async Task FindMessages(DiscordLink plugin)
        {
            _channelDisplays.Clear();

            foreach (ChannelLink channelLink in GetChannelLinks())
            {
                if (!channelLink.IsValid())
                {
                    continue;
                }

                DiscordGuild discordGuild = plugin.GuildByNameOrId(channelLink.DiscordGuild);
                if (discordGuild == null)
                {
                    continue;
                }
                DiscordChannel discordChannel = discordGuild.ChannelByNameOrId(channelLink.DiscordChannel);
                if (discordChannel == null)
                {
                    continue;
                }
                if (!DiscordUtil.ChannelHasPermission(discordChannel, Permissions.ReadMessageHistory))
                {
                    continue;
                }

                ChannelDisplayData data = new ChannelDisplayData(channelLink);
                _channelDisplays.Add(data);

                IReadOnlyList <DiscordMessage> channelMessages = await DiscordUtil.GetMessagesAsync(discordChannel);

                if (channelMessages == null)
                {
                    _channelDisplays.Clear();
                    return;
                }

                foreach (DiscordMessage message in channelMessages)
                {
                    if (!message.Content.StartsWith(BaseTag))
                    {
                        continue;
                    }
                    data.MessageIDs.Add(message.Id);
                }
            }
            _dirty = false;
        }
Beispiel #3
0
        private async Task FindMessages(DiscordLink plugin)
        {
            _targetDisplays.Clear();

            foreach (DiscordTarget target in GetDiscordTargets())
            {
                IReadOnlyList <DiscordMessage> targetMessages = null;

                ChannelLink channelLink = target as ChannelLink;
                UserLink    userLink    = target as UserLink;
                if (channelLink == null && userLink == null)
                {
                    continue;
                }

                TargetDisplayData data = new TargetDisplayData(target);
                _targetDisplays.Add(data);
                if (channelLink != null)
                {
                    if (!channelLink.IsValid())
                    {
                        continue;
                    }

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

                    targetMessages = await dmChannel.GetMessagesAsync();
                }

                if (targetMessages == null)
                {
                    // There was an error or no messages exist - Clean up and return
                    _targetDisplays.Clear();
                    return;
                }

                // Go through the messages and find any our tagged messages
                foreach (DiscordMessage message in targetMessages)
                {
                    if (!message.Content.StartsWith(BaseTag))
                    {
                        continue;
                    }
                    data.MessageIDs.Add(message.Id);
                }
            }
            _dirty = false;
        }