Ejemplo n.º 1
0
        private async void SendMessageShardAsync(NinjaCatDiscordClient client, DiscordSocketClient shard, string message)
        {
            // Announce in the specified channel of each guild.
            foreach (var guild in shard.Guilds)
            {
                // Get channel.
                var channel = client.GetSpeakingChannelForSocketGuild(guild);

                // If the channel is null, continue on to the next guild.
                if (channel == null)
                {
                    client.LogInfo($"Rolling over server (disabled) {guild.Name}");
                    continue;
                }

                // Verify we have permission to speak.
                if (!guild.CurrentUser.GetPermissions(channel).SendMessages)
                {
                    client.LogInfo($"Rolling over server (no perms) {guild.Name}");
                    continue;
                }

                try {
                    // Wait 2 seconds.
                    await Task.Delay(TimeSpan.FromSeconds(2));

                    // Bot is typing, with added pause for realism.
                    await Context.Channel.TriggerTypingAsync();

                    await Task.Delay(TimeSpan.FromSeconds(2));

                    // Send message.
                    await channel.SendMessageAsync($"Announcement from **{Constants.OwnerName}** (bot owner):\n{message}");
                }
                catch (Exception ex) {
                    client.LogError($"Failed to speak in {guild.Name}: {ex}");
                }
                client.LogInfo($"Spoke in server {guild.Name}");
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> CheckBotGuild(SocketGuild guild)
        {
            // If the server is the Discord bots server, ignore.
            if (guild.Id == Constants.BotsGuildId)
            {
                return(false);
            }

            // Ensure guild is updated.
            if (guild.Users.Count != guild.MemberCount)
            {
                await guild.DownloadUsersAsync();
            }

            // Is this a bot guild?
            if (guild.MemberCount >= 50 && (guild.Users.Count(u => u.IsBot) / (double)guild.MemberCount) >= 0.9)
            {
                client.LogInfo($"Leaving bot server {guild.Name}");
                try {
                    // Bot is typing in default channel.
                    await guild.DefaultChannel.TriggerTypingAsync();

                    // Pause for realism.
                    await Task.Delay(TimeSpan.FromSeconds(1));

                    // Send notice.
                    await guild.DefaultChannel.SendMessageAsync($"It looks like this server is a bot farm, so I'll show myself out. If this is a legitimate server, contact *{Constants.OwnerName}*.");
                }
                catch { }

                // Wait 2 seconds, then leave.
                await Task.Delay(TimeSpan.FromSeconds(2));

                await guild.LeaveAsync();

                // This was a bot server.
                return(true);
            }

            // This is not a bot server.
            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Starts the bot.
        /// </summary>
        private async Task Start()
        {
            client = new NinjaCatDiscordClient();

            // Certain things are to be done when the bot joins a guild.

            /*client.JoinedGuild += async (guild) => {
             *  // Pause for 5 seconds.
             *  await Task.Delay(TimeSpan.FromSeconds(5));
             *
             *  // Check to see if this is a bot farm.
             *  if (await CheckBotGuild(guild))
             *      return;
             *
             *  // Dev began Oct 2. 2016.
             * };*/

            // Log in to Discord. Token is stored in the Credentials class.
            await client.StartBotAsync();

            // Start checking for new builds.
            timerBuild = new Timer(async(s) => {
                // Builds generally release between 10AM and 5PM PST. Do not check outside these times.
                //     if (DateTime.UtcNow.Hour < 17 && !string.IsNullOrWhiteSpace(client.CurrentUrl))
                //     return;

                // If we cannot get the new post, try again later.
                var post = await client.GetLatestBuildPostAsync();
                if (post == null)
                {
                    return;
                }

                // Have we ever seen a post yet? This prevents false announcements if the bot has never seen a post before.
                if (string.IsNullOrWhiteSpace(client.CurrentUrl))
                {
                    client.CurrentUrl = post.Link;
                    client.SaveSettings();
                    client.LogInfo($"Saved post as new latest build: {post.Link}");
                    return;
                }

                // Is the latest post the same? If so, no need to announce it.
                if (client.CurrentUrl == post.Link)
                {
                    return;
                }

                // Stop timer.
                timerBuild.Change(TimeSpan.FromMilliseconds(-1), TimeSpan.FromMilliseconds(-1));
                client.LogInfo($"New build received");

                // Save post.
                client.CurrentUrl = post.Link;
                client.SaveSettings();

                // Send build to guilds.
                foreach (var shard in client.Shards)
                {
                    client.SendNewBuildToShard(shard, post);
                }
                await client.UpdateGameAsync();

                // Restart timer.
                timerBuild.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
            }, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));

            // Wait a minute for bot to start up.
            await Task.Delay(TimeSpan.FromMinutes(1));

            // Create thread for updating game.
            var serverCountThread = new Thread(new ThreadStart(async() => {
                while (true)
                {
                    await client.UpdateGameAsync();
                    await Task.Delay(TimeSpan.FromHours(24));
                }
            }));

            serverCountThread.Start();

            // Wait forever.
            await Task.Delay(-1);
        }