示例#1
0
        public async Task PrintPermsAsync(CommandContext ctx,
                                          [Description("Role.")] DiscordRole role,
                                          [Description("Channel.")] DiscordChannel channel = null)
        {
            if (role.Position > ctx.Member.Hierarchy)
            {
                throw new CommandFailedException("You cannot view permissions for roles which have position above your highest role position.");
            }

            channel = channel ?? ctx.Channel;

            DiscordOverwrite overwrite = null;

            foreach (var o in channel.PermissionOverwrites.Where(o => o.Type == OverwriteType.Role))
            {
                var r = await o.GetRoleAsync();

                if (r.Id == role.Id)
                {
                    overwrite = o;
                    break;
                }
            }

            var emb = new DiscordEmbedBuilder
            {
                Title = $"Permissions for {role.ToString()} in {channel.ToString()}:",
                Color = this.ModuleColor
            };

            if (!(overwrite is null))
            {
                emb.AddField("Allowed", overwrite.Allowed.ToPermissionString())
                .AddField("Denied", overwrite.Denied.ToPermissionString());
            }
        public static async Task ChannelUpdateEventHandlerAsync(TheGodfatherShard shard, ChannelUpdateEventArgs e)
        {
            if (e.ChannelBefore.Position != e.ChannelAfter.Position)
            {
                return;
            }

            DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild);

            if (logchn == null)
            {
                return;
            }

            if (await shard.DatabaseService.IsExemptedAsync(e.Guild.Id, e.ChannelAfter.Id, EntityType.Channel))
            {
                return;
            }

            DiscordEmbedBuilder  emb   = FormEmbedBuilder(EventOrigin.Channel, "Channel updated");
            DiscordAuditLogEntry entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.ChannelUpdate);

            if (entry != null && entry is DiscordAuditLogChannelEntry centry)       // Regular update
            {
                emb.WithDescription(centry.Target.ToString());
                emb.AddField("User responsible", centry.UserResponsible?.Mention ?? _unknown, inline: true);
                if (centry.BitrateChange != null)
                {
                    emb.AddField("Bitrate changed to", centry.BitrateChange.After.ToString(), inline: true);
                }
                if (centry.NameChange != null)
                {
                    emb.AddField("Name changed to", centry.NameChange.After, inline: true);
                }
                if (centry.NsfwChange != null)
                {
                    emb.AddField("NSFW flag changed to", centry.NsfwChange.After.Value.ToString(), inline: true);
                }
                if (centry.OverwriteChange != null)
                {
                    emb.AddField("Permissions overwrites changed", $"{centry.OverwriteChange.After.Count} overwrites after changes");
                }
                if (centry.TopicChange != null)
                {
                    string ptopic = Formatter.BlockCode(Formatter.Sanitize(string.IsNullOrWhiteSpace(centry.TopicChange.Before) ? " " : centry.TopicChange.Before));
                    string ctopic = Formatter.BlockCode(Formatter.Sanitize(string.IsNullOrWhiteSpace(centry.TopicChange.After) ? " " : centry.TopicChange.After));
                    emb.AddField("Topic changed", $"From:{ptopic}\nTo:{ctopic}");
                }
                if (centry.TypeChange != null)
                {
                    emb.AddField("Type changed to", centry.TypeChange.After.Value.ToString());
                }
                if (!string.IsNullOrWhiteSpace(centry.Reason))
                {
                    emb.AddField("Reason", centry.Reason);
                }
                emb.WithFooter($"At {centry.CreationTimestamp.ToUniversalTime().ToString()} UTC", centry.UserResponsible.AvatarUrl);
            }
            else        // Overwrite update
            {
                AuditLogActionType type;
                if (e.ChannelBefore.PermissionOverwrites.Count > e.ChannelAfter.PermissionOverwrites.Count)
                {
                    type  = AuditLogActionType.OverwriteCreate;
                    entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.OverwriteCreate);
                }
                else if (e.ChannelBefore.PermissionOverwrites.Count < e.ChannelAfter.PermissionOverwrites.Count)
                {
                    type  = AuditLogActionType.OverwriteDelete;
                    entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.OverwriteDelete);
                }
                else
                {
                    if (e.ChannelBefore.PermissionOverwrites.Zip(e.ChannelAfter.PermissionOverwrites, (o1, o2) => o1.Allowed != o1.Allowed && o2.Denied != o2.Denied).Any())
                    {
                        type  = AuditLogActionType.OverwriteUpdate;
                        entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.OverwriteUpdate);
                    }
                    else
                    {
                        type  = AuditLogActionType.ChannelUpdate;
                        entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.ChannelUpdate);
                    }
                }

                if (entry != null && entry is DiscordAuditLogOverwriteEntry owentry)
                {
                    emb.WithDescription($"{owentry.Channel.ToString()} ({type})");
                    emb.AddField("User responsible", owentry.UserResponsible?.Mention ?? _unknown, inline: true);

                    DiscordUser member = null;
                    DiscordRole role   = null;

                    try {
                        bool isMemberUpdated = owentry.Target.Type.HasFlag(OverwriteType.Member);
                        if (isMemberUpdated)
                        {
                            member = await e.Client.GetUserAsync(owentry.Target.Id);
                        }
                        else
                        {
                            role = e.Guild.GetRole(owentry.Target.Id);
                        }
                        emb.AddField("Target", isMemberUpdated ? member.ToString() : role.ToString(), inline: true);
                        if (owentry.AllowChange != null)
                        {
                            emb.AddField("Allowed", $"{owentry.Target.Allowed.ToPermissionString() ?? _unknown}", inline: true);
                        }
                        if (owentry.DenyChange != null)
                        {
                            emb.AddField("Denied", $"{owentry.Target.Denied.ToPermissionString() ?? _unknown}", inline: true);
                        }
                    } catch {
                        emb.AddField("Target ID", owentry.Target.Id.ToString(), inline: true);
                    }

                    if (!string.IsNullOrWhiteSpace(owentry.Reason))
                    {
                        emb.AddField("Reason", owentry.Reason);
                    }
                    emb.WithFooter(owentry.CreationTimestamp.ToUtcTimestamp(), owentry.UserResponsible.AvatarUrl);
                }
                else
                {
                    return;
                }
            }

            await logchn.SendMessageAsync(embed : emb.Build());
        }