Ejemplo n.º 1
0
        public static async Task AuditUpdate(SocketGuild guild)
        {
            foreach (SocketTextChannel chan in guild.TextChannels)
            {
                if (chan.Name.ToLower().Contains("security"))
                {
                    ConsolePrint("Security channel found in " + guild.Name);
                    IAsyncEnumerable <IReadOnlyCollection <RestAuditLogEntry> > auditLog  = guild.GetAuditLogsAsync(10);
                    List <IReadOnlyCollection <RestAuditLogEntry> >             auditList = await auditLog.ToList();

                    IAsyncEnumerable <IReadOnlyCollection <IMessage> > messages = chan.GetMessagesAsync(1);
                    List <IReadOnlyCollection <IMessage> >             msgList  = await messages.ToList();

                    IMessage mesg = null;
                    if (msgList[1].Count > 0)
                    {
                        mesg = msgList[1].ToArray()[0];// Newest Message
                    }
                    RestAuditLogEntry[] restAuditLogs = auditList[0].ToArray();
                    if (mesg != null)
                    {
                        if (mesg.Embeds.Count > 0)
                        {
                            if (restAuditLogs[0].Id.ToString() == mesg.Embeds.FirstOrDefault().Footer.Value.Text)
                            {
                                break;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    for (int i = restAuditLogs.Length - 1; i >= 0; i--)
                    {
                        if (mesg != null)
                        {
                            if (mesg.Embeds.Count > 0)
                            {
                                int dif = DateTimeOffset.Compare(mesg.Embeds.FirstOrDefault().Timestamp.Value, restAuditLogs[i].CreatedAt);
                                if (dif >= 0)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        RestAuditLogEntry logEntry = restAuditLogs[i];
                        bool         send          = false;
                        EmbedBuilder embed         = new EmbedBuilder();
                        string       adminUsername = "******" + logEntry.User + "**";
                        string       title         = "Server Report";
                        embed.WithTimestamp(logEntry.CreatedAt);
                        string msg = "";
                        if (logEntry.Action == ActionType.MemberRoleUpdated)
                        {
                            MemberRoleAuditLogData data = (MemberRoleAuditLogData)logEntry.Data;
                            string targetUsername       = "******" + data.Target + "** _(" + data.Target.Id + ")_";
                            string roles = "";
                            foreach (MemberRoleEditInfo role in data.Roles)
                            {
                                roles += string.Format(((role.Added) ? " Added role {0} to " : " Removed role {0} from "), "**_" + role.Name + "_**");
                            }

                            msg = adminUsername + roles + targetUsername;
                            embed.WithColor(new Color(255, 190, 0));
                            send = true;
                        }// end if log entry role updated
                        else if (logEntry.Action == ActionType.MemberUpdated)
                        {
                            MemberUpdateAuditLogData data = (MemberUpdateAuditLogData)logEntry.Data;
                            string targetUsername         = "******" + data.Target + "** _(" + data.Target.Id + ")_";
                            if (data.Before.Deaf != data.After.Deaf)
                            {
                                msg += string.Format(adminUsername + " {0} " + targetUsername + "\n", (data.After.Deaf.Value) ? "deafened" : "undeafened");
                            }

                            if (data.Before.Mute != data.After.Mute)
                            {
                                msg += string.Format(adminUsername + " {0} " + targetUsername + "\n", (data.After.Mute.Value) ? "muted" : "unmuted");
                            }


                            if (data.Before.Nickname != data.After.Nickname)
                            {
                                msg += string.Format(adminUsername + " changed {0}'s nickname from *{1}* to *{2}*\n", targetUsername,
                                                     (data.Before.Nickname == null) ? data.Target.Username : data.Before.Nickname,
                                                     (data.After.Nickname == null) ? data.Target.Username : data.After.Nickname);
                            }

                            Console.WriteLine(msg);
                            embed.WithColor(255, 255, 0);
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.Kick)
                        {
                            KickAuditLogData data           = (KickAuditLogData)logEntry.Data;
                            string           targetUsername = "******" + data.Target + "** _(" + data.Target.Id + ")_";
                            msg = targetUsername + " was kicked by " + adminUsername +
                                  "\nReason: " + logEntry.Reason;

                            embed.WithColor(new Color(255, 0, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.Ban)
                        {
                            BanAuditLogData data           = (BanAuditLogData)logEntry.Data;
                            string          targetUsername = "******" + data.Target + "** _(" + data.Target.Id + ")_";
                            msg = targetUsername + " was banned by " + adminUsername +
                                  "\nReason: " + logEntry.Reason;

                            embed.WithColor(new Color(255, 0, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.Unban)
                        {
                            UnbanAuditLogData data           = (UnbanAuditLogData)logEntry.Data;
                            string            targetUsername = "******" + data.Target + "** _(" + data.Target.Id + ")_";
                            msg = targetUsername + " was unbanned by " + adminUsername +
                                  "\nReason: " + logEntry.Reason;

                            embed.WithColor(new Color(255, 255, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.Prune)
                        {
                            PruneAuditLogData data = (PruneAuditLogData)logEntry.Data;
                            msg = adminUsername + " removed " + data.MembersRemoved + " users in a " + data.PruneDays + " day prune" +
                                  "\nReason: " + logEntry.Reason;

                            embed.WithColor(new Color(255, 0, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.MessageDeleted)
                        {
                            MessageDeleteAuditLogData data = (MessageDeleteAuditLogData)logEntry.Data;
                            msg = adminUsername + " deleted _" + data.MessageCount + "_ messages in **#" + guild.GetChannel(data.ChannelId).Name + "** channel";

                            embed.WithColor(new Color(255, 190, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.InviteCreated)
                        {
                            InviteCreateAuditLogData data = (InviteCreateAuditLogData)logEntry.Data;
                            string creatorUsername        = "******" + data.Creator + "** _(" + data.Creator.Id + ")_";
                            msg = creatorUsername + " created invite **" + data.Code + "**\n" +
                                  "Channel: " + guild.GetChannel(data.ChannelId).Name + "\n" +
                                  "Max Uses: " + data.MaxUses + "\n" +
                                  "Max Age: " + (data.MaxAge / 60 / 60 / 24) + "\n" +
                                  "Temporary: " + data.Temporary;

                            embed.WithColor(new Color(255, 255, 0));
                            send = true;
                        }
                        else if (logEntry.Action == ActionType.InviteDeleted)
                        {
                            InviteDeleteAuditLogData data = (InviteDeleteAuditLogData)logEntry.Data;
                            string creatorUsername        = "******" + data.Creator + "** _(" + data.Creator.Id + ")_";
                            msg = adminUsername + " deleted invite **" + data.Code + "** created by " + creatorUsername + "\n" +
                                  "Channel: " + guild.GetChannel(data.ChannelId).Name + "\n" +
                                  "Uses: " + data.Uses + "\n" +
                                  "Max Uses: " + data.MaxUses + "\n" +
                                  "Max Age: " + (data.MaxAge / 60 / 60 / 24) + "\n" +
                                  "Temporary: " + data.Temporary;

                            embed.WithColor(new Color(255, 255, 0));
                            send = true;
                        }
                        else
                        {
                            ConsolePrint("LE_Action: " + logEntry.Action + "; LE_Data: " + logEntry.Data.ToString() + "; ");
                        }
                        embed.WithAuthor(title, logEntry.User.GetAvatarUrl());
                        embed.WithDescription(msg);
                        embed.WithFooter(logEntry.Id.ToString());
                        if (send)
                        {
                            await chan.SendMessageAsync("", false, embed.Build());
                        }
                        if (send)
                        {
                            ConsolePrint("Sent " + embed.Description);
                        }
                    }//end restAuditLog loop
                    ConsolePrint("Finished Audit Log for " + guild.Name);
                    break;
                } // end if security channel
            }     // end for each text channel in guild
        }
Ejemplo n.º 2
0
        private async Task GuildMemberUpdated(SocketGuildUser before, SocketGuildUser after)
        {
            //Is the user in the dictionary
            if (Config.UserToStickyRoles.ContainsKey(after.Id))
            {
                //Iterate over every role this user is supposed to have
                foreach (ulong roleTheyShouldHaveId in Config.UserToStickyRoles[after.Id])
                {
                    //Continue if they have this role
                    bool hasRole = false;
                    foreach (SocketRole roleTheyHave in after.Roles)
                    {
                        if (roleTheyHave.Id == roleTheyShouldHaveId)
                        {
                            hasRole = true;
                        }
                    }
                    if (hasRole)
                    {
                        continue;
                    }

                    //Right, they don't have the role. Firstly check that we have perms to add the role, then figure out who removed the role, then add the role, then announce it

                    SocketGuildUser botUser            = after.Guild.GetUser(Client.CurrentUser.Id);
                    SocketRole      roleTheyShouldHave = botUser.Guild.GetRole(roleTheyShouldHaveId);

                    //Check we have the manage roles permission
                    if (!botUser.GuildPermissions.ManageRoles)
                    {
                        break;
                    }

                    //Check that the role they should have is lower in the hierarchy than our role
                    if (botUser.Roles.Count == 0)
                    {
                        continue;
                    }
                    SocketRole[] ourRoles       = botUser.Roles.ToArray();
                    SocketRole   ourHighestRole = ourRoles[ourRoles.Length - 1];
                    if (ourHighestRole.Position < roleTheyShouldHave.Position)
                    {
                        continue;
                    }

                    //Find out who did it, 100 entries max
                    SocketGuildUser culprit = null;
                    if (botUser.GuildPermissions.ViewAuditLog)
                    {
                        IEnumerable <RestAuditLogEntry> auditLog = await botUser.Guild.GetAuditLogsAsync(100).FlattenAsync();

                        foreach (RestAuditLogEntry entry in auditLog)
                        {
                            //Check that the action is a member role update
                            if (entry.Action != ActionType.MemberRoleUpdated)
                            {
                                continue;
                            }

                            MemberRoleAuditLogData data = (MemberRoleAuditLogData)entry.Data;

                            //Check that the user who's role was updated matches what we need
                            if (data.Target.Id != after.Id)
                            {
                                continue;
                            }

                            //Check that the role that was updated matches what we need
                            bool containRoleWeNeed = false;
                            foreach (MemberRoleEditInfo info in data.Roles)
                            {
                                if (info.RoleId == roleTheyShouldHave.Id)
                                {
                                    containRoleWeNeed = true;
                                }
                            }
                            if (!containRoleWeNeed)
                            {
                                continue;
                            }

                            //Ok, this entry is the one we need
                            culprit = botUser.Guild.GetUser(entry.User.Id);
                            break;
                        }
                    }

                    //Add the role
                    await after.AddRoleAsync(roleTheyShouldHave);

                    //Announce it
                    SocketTextChannel channelToPostIn = (SocketTextChannel)botUser.Guild.GetChannel(Config.ChannelId);
                    if (culprit == null)
                    {
                        await channelToPostIn.SendMessageAsync("Somebody just tried to remove the role " + roleTheyShouldHave.Name + " from " + MentionUtils.MentionUser(after.Id) + "\nNice try");
                    }
                    else if (culprit.Id == after.Id)
                    {
                        await channelToPostIn.SendMessageAsync(MentionUtils.MentionUser(after.Id) + " just tried to remove the role " + roleTheyShouldHave.Name + " from themself\nNice try");
                    }
                    else
                    {
                        await channelToPostIn.SendMessageAsync(MentionUtils.MentionUser(culprit.Id) + " just tried to remove the role " + roleTheyShouldHave.Name + " from " + MentionUtils.MentionUser(after.Id) + "\nNice try");
                    }
                }
            }
        }