예제 #1
0
        public async void HandleFeed(FeedData feedData, string url, ulong channelID, ulong?roleID, string message)
        {
            try
            {
                HttpWebRequest  webRequest   = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse webResponse  = (HttpWebResponse)webRequest.GetResponse();
                Stream          dataStream   = webResponse.GetResponseStream();
                StreamReader    streamReader = new StreamReader(dataStream, Encoding.UTF8);
                string          response     = streamReader.ReadToEnd();
                streamReader.Close();
                response = Utils.SanitizeXml(response);
                XmlReader reader = new XmlTextReader(new StringReader(response));

                SyndicationFeed feed    = SyndicationFeed.Load(reader);
                var             channel = _client.GetChannel(channelID) as ISocketMessageChannel;
                foreach (var item in feed.Items.Take(MAXIMUM_CHECK))
                {
                    if (!feedData.PostedIds.Contains(item.Id))
                    {
                        feedData.PostedIds.Add(item.Id);

                        string messageToSend = string.Format(message, item.Title.Text, item.Links[0].Uri.ToString());

                        var  role = _client.GetGuild(_settings.guildId).GetRole(roleID ?? 0);
                        bool wasRoleMentionable = false;
                        if (role != null)
                        {
                            wasRoleMentionable = role.IsMentionable;
                            await role.ModifyAsync(properties => { properties.Mentionable = true; });

                            messageToSend = $"{role.Mention} {messageToSend}";
                        }

                        await channel.SendMessageAsync(messageToSend);

                        if (role != null)
                        {
                            await role.ModifyAsync(properties => { properties.Mentionable = wasRoleMentionable; });
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
예제 #2
0
        public async void CheckUnityReleases(FeedData feedData)
        {
            try
            {
                HttpWebRequest  webRequest   = (HttpWebRequest)WebRequest.Create(RELEASE_URL);
                HttpWebResponse webResponse  = (HttpWebResponse)webRequest.GetResponse();
                Stream          dataStream   = webResponse.GetResponseStream();
                StreamReader    streamReader = new StreamReader(dataStream, Encoding.UTF8);
                string          response     = streamReader.ReadToEnd();
                streamReader.Close();
                response = Utils.SanitizeXml(response);
                XmlReader reader = new XmlTextReader(new StringReader(response));

                SyndicationFeed feed    = SyndicationFeed.Load(reader);
                var             channel = _client.GetChannel(_settings.UnityNewsChannel.Id) as ISocketMessageChannel;
                var             role    = _client.GetGuild(_settings.guildId).GetRole(_settings.SubsReleasesRoleId);

                foreach (var item in feed.Items.Take(MAXIMUM_CHECK))
                {
                    if (!feedData.PostedIds.Contains(item.Id))
                    {
                        feedData.PostedIds.Add(item.Id);

                        await role.ModifyAsync(properties => { properties.Mentionable = true; });

                        string message =
                            $"{role.Mention} New unity release ! **{item.Title.Text}** \n <{item.Links[0].Uri.ToString().Replace("/fr/", "/")}>";

                        await channel.SendMessageAsync(message);

                        await role.ModifyAsync(properties => { properties.Mentionable = false; });
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
예제 #3
0
        public async void CheckUnityBlog(FeedData feedData)
        {
            try
            {
                SyndicationFeed feed    = SyndicationFeed.Load(XmlReader.Create(BLOG_URL));
                var             channel = _client.GetChannel(_settings.UnityNewsChannel.Id) as ISocketMessageChannel;

                foreach (var item in feed.Items.Take(MAXIMUM_CHECK))
                {
                    if (!feedData.PostedIds.Contains(item.Id))
                    {
                        feedData.PostedIds.Add(item.Id);

                        string message = $"New unity blog post ! **{item.Title.Text}**\n{item.Links[0].Uri}";
                        await channel.SendMessageAsync(message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
예제 #4
0
파일: FeedService.cs 프로젝트: Zod-/UDHBot
        public async void CheckUnityReleases(FeedData feedData)
        {
            try
            {
                SyndicationFeed feed    = SyndicationFeed.Load(XmlReader.Create(RELEASE_URL));
                var             channel = _client.GetChannel(_settings.UnityNewsChannel.Id) as ISocketMessageChannel;

                foreach (var item in feed.Items.Take(MAXIMUM_CHECK))
                {
                    if (!feedData.PostedIds.Contains(item.Id))
                    {
                        feedData.PostedIds.Add(item.Id);

                        string message = $"New unity release ! **{item.Title.Text}** \n <{item.Links[0].Uri.ToString().Replace("/fr/", "/")}>";
                        await channel.SendMessageAsync(message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
예제 #5
0
        private void ReadDataFromFile()
        {
            if (File.Exists($"{_settings.ServerRootPath}/botdata.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/botdata.json");
                _botData = JsonConvert.DeserializeObject <BotData>(json);
            }
            else
            {
                _botData = new BotData();
            }

            if (File.Exists($"{_settings.ServerRootPath}/animedata.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/animedata.json");
                _animeData = JsonConvert.DeserializeObject <AnimeData>(json);
            }
            else
            {
                _animeData = new AnimeData();
            }

            if (File.Exists($"{_settings.ServerRootPath}/userdata.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/userdata.json");
                _userData = JsonConvert.DeserializeObject <UserData>(json);

                Task.Run(
                    async() =>
                {
                    while (_client.ConnectionState != ConnectionState.Connected || _client.LoginState != LoginState.LoggedIn)
                    {
                        await Task.Delay(100, _token);
                    }
                    await Task.Delay(10000, _token);
                    //Check if there are users still muted
                    foreach (var userID in _userData.MutedUsers)
                    {
                        if (_userData.MutedUsers.HasUser(userID.Key, evenIfCooldownNowOver: true))
                        {
                            SocketGuild guild   = _client.Guilds.First(g => g.Id == _settings.guildId);
                            SocketGuildUser sgu = guild.GetUser(userID.Key);
                            if (sgu == null)
                            {
                                continue;
                            }

                            IGuildUser user = sgu as IGuildUser;

                            IRole mutedRole = user.Guild.GetRole(_settings.MutedRoleId);
                            //Make sure they have the muted role
                            if (!user.RoleIds.Contains(_settings.MutedRoleId))
                            {
                                await user.AddRoleAsync(mutedRole);
                            }

                            //Setup delay to remove role when time is up.
                            Task.Run(async() =>
                            {
                                await _userData.MutedUsers.AwaitCooldown(user.Id);
                                await user.RemoveRoleAsync(mutedRole);
                            }, _token);
                        }
                    }
                }, _token);
            }
            else
            {
                _userData = new UserData();
            }

            if (File.Exists($"{_settings.ServerRootPath}/casinodata.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/casinodata.json");
                _casinoData = JsonConvert.DeserializeObject <CasinoData>(json);
            }
            else
            {
                _casinoData = new CasinoData();
            }

            if (File.Exists($"{_settings.ServerRootPath}/FAQs.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/FAQs.json");
                _faqData = JsonConvert.DeserializeObject <List <FaqData> >(json);
            }
            else
            {
                _faqData = new List <FaqData>();
            }

            if (File.Exists($"{_settings.ServerRootPath}/feeds.json"))
            {
                string json = File.ReadAllText($"{_settings.ServerRootPath}/feeds.json");
                _feedData = JsonConvert.DeserializeObject <FeedData>(json);
            }
            else
            {
                _feedData = new FeedData();
            }
        }
예제 #6
0
 public async Task CheckUnityBlogAsync(FeedData feedData)
 {
     await HandleFeed(feedData, BlogUrl, _settings.UnityNewsChannel.Id, _settings.SubsNewsRoleId, "New unity blog post ! **{0}**\n{1}");
 }
예제 #7
0
 public async Task CheckUnityReleasesAsync(FeedData feedData)
 {
     await HandleFeed(feedData, ReleaseUrl, _settings.UnityReleasesChannel.Id, _settings.SubsReleasesRoleId, "New unity release ! **{0}** \n <{1}>");
 }
예제 #8
0
 public void CheckUnityBlog(FeedData feedData)
 {
     this.HandleFeed(feedData, BLOG_URL, _settings.UnityNewsChannel.Id, _settings.SubsNewsRoleId, "New unity blog post ! **{0}**\n{1}");
 }
예제 #9
0
 public void CheckUnityReleases(FeedData feedData)
 {
     this.HandleFeed(feedData, RELEASE_URL, _settings.UnityReleasesChannel.Id, _settings.SubsReleasesRoleId, "New unity release ! **{0}** \n <{1}>");
 }
예제 #10
0
 public void CheckUnityBetas(FeedData feedData)
 {
     this.HandleFeed(feedData, BETA_URL, _settings.UnityReleasesChannel.Id, _settings.SubsReleasesRoleId, "New unity **beta **release !** {0}** \n <{1}>");
 }