Exemplo n.º 1
0
        public async Task DenyAccessToUserAsync(IUser roomOwnerUser, IUser targetUser)
        {
            if (!(roomOwnerUser is SocketGuildUser roomOwnerSgUser) ||
                !(targetUser is SocketGuildUser targetSgUser))
            {
                return;
            }

            if (roomOwnerSgUser.VoiceChannel is null)
            {
                return;
            }

            if (targetSgUser.VoiceChannel is null)
            {
                return;
            }

            if (roomOwnerSgUser.VoiceChannel.Id != targetSgUser.VoiceChannel.Id)
            {
                return;
            }

            var channel = roomOwnerSgUser.VoiceChannel;

            var channelPerms = channel.GetPermissionOverwrite(roomOwnerSgUser);

            if (!channelPerms.HasValue || channelPerms.Value.ManageChannel != PermValue.Allow)
            {
                return;
            }

            var targetPerms = channel.GetPermissionOverwrite(targetSgUser);

            if (targetPerms.HasValue && targetPerms.Value.Connect == PermValue.Deny)
            {
                return;
            }

            try
            {
                var permissions = new OverwritePermissions(connect: PermValue.Deny);
                await channel.AddPermissionOverwriteAsync(targetSgUser, permissions);
            }
            catch (Exception ex)
            {
                RiftBot.Log.Warning(ex, "Channel was disposed while executing command, skipping");
                return;
            }

            try
            {
                if (!IonicHelper.GetVoiceChannel(Settings.App.MainGuildId, Settings.ChannelId.Afk, out var afkChannel))
                {
                    return;
                }

                await targetSgUser.ModifyAsync(x => { x.Channel = afkChannel; });
            }
            catch (Exception ex)
            {
                RiftBot.Log.Warning(ex, $"User {targetSgUser.ToLogString()} wasn't in any channel when kick issued, skipping");
                return;
            }

            // TODO: done
        }
Exemplo n.º 2
0
        public async Task SelfTest()
        {
            var skipChecks = false;

            var errors     = new List <string>();
            var fixedRoles = 0u;

            var eb = new RiftEmbed().WithTitle("Self-test");

            if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild))
            {
                errors.Add($"Guild is null: {nameof(Settings.App.MainGuildId)}");
                skipChecks = true;
            }

            var channelNames = Settings.ChannelId.GetNames();

            foreach (var field in Settings.ChannelId.GetType().GetProperties())
            {
                if (skipChecks)
                {
                    break;
                }

                if (field.GetValue(Settings.ChannelId, null) is ulong value)
                {
                    if (value == 0ul)
                    {
                        if (channelNames.ContainsKey(field.Name))
                        {
                            var channelName = channelNames[field.Name];

                            var guildChannel = guild.Channels.FirstOrDefault(
                                x => x.Name.Equals(channelName, StringComparison.InvariantCultureIgnoreCase));

                            if (guildChannel is null)
                            {
                                errors.Add($"Channel ID remains undefined: {field.Name} {channelName}");
                                continue;
                            }

                            Settings.ChannelId.SetValue(field.Name, guildChannel.Id);
                            fixedRoles++;
                        }
                        else
                        {
                            errors.Add($"Channel ID remains undefined: {field.Name}");
                            continue;
                        }
                    }
                    else if (!IonicHelper.GetTextChannel(Settings.App.MainGuildId, value, out var textChannel) &&
                             !IonicHelper.GetVoiceChannel(Settings.App.MainGuildId, value, out var voiceChannel))
                    {
                        errors.Add($"No channel on server: {field.Name}");
                    }
                }
            }

            foreach (var field in Settings.Chat.GetType().GetProperties())
            {
                if (skipChecks)
                {
                    break;
                }

                var obj = field.GetValue(Settings.Chat);

                if (obj is ulong ulongValue)
                {
                    if (ulongValue == 0ul)
                    {
                        errors.Add($"Chat parameter undefined: {field.Name}");
                    }
                }
                else if (obj is uint uintValue)
                {
                    if (uintValue == 0u)
                    {
                        errors.Add($"Chat parameter undefined: {field.Name}");
                    }
                }
            }

            foreach (var field in Settings.Economy.GetType().GetProperties())
            {
                if (skipChecks)
                {
                    break;
                }

                try
                {
                    var obj = field.GetValue(Settings.Economy);

                    if (obj is ulong ulongValue)
                    {
                        if (ulongValue == 0ul)
                        {
                            errors.Add($"Economy parameter undefined: {field.Name}");
                        }
                    }
                    else if (obj is uint uintValue)
                    {
                        if (uintValue == 0u)
                        {
                            errors.Add($"Economy parameter undefined: {field.Name}");
                        }
                    }
                }
                catch (TargetInvocationException ex)
                {
                    errors.Add($"\"{field.Name}\" invokation failed: {ex.Message}");
                }
                catch (Exception ex)
                {
                    errors.Add($"Economy object exception failed: {ex.Message}");
                }
            }

            var serverRoles = Context.Guild.Roles.ToList();
            var roles       = await DB.Roles.GetAllAsync();

            foreach (var role in serverRoles)
            {
                if (skipChecks)
                {
                    break;
                }

                var matchedRole = roles.FirstOrDefault(x => x.Name.Equals(role.Name));

                if (matchedRole is null)
                {
                    await DB.Roles.AddAsync(role);

                    fixedRoles++;
                    continue;
                }

                if (matchedRole.RoleId.Equals(role.Id))
                {
                    continue;
                }

                matchedRole.RoleId = role.Id;
                await DB.Roles.UpdateAsync(matchedRole);

                fixedRoles++;
            }

            if (errors.Count == 0)
            {
                eb.WithColor(0, 255, 0);
                eb.WithDescription("OK 👌");
            }
            else
            {
                eb.WithColor(255, 0, 0);

                var errorList = string.Join('\n', errors);

                if (errorList.Length >= 2048)
                {
                    errorList = string.Join('\n', errors.Take(10));
                    eb.WithDescription($"**{errors.Count.ToString()} error(s), showing first 10**\n\n{errorList}");
                }
                else
                {
                    eb.WithDescription($"**{errors.Count.ToString()} error(s)**\n\n{errorList}");
                }
            }

            await Context.Channel.SendIonicMessageAsync(new IonicMessage(eb));

            if (fixedRoles > 0u)
            {
                var embedMsg = new RiftEmbed()
                               .WithColor(255, 255, 0)
                               .WithAuthor("Self-test")
                               .WithDescription($"Fixed {fixedRoles.ToString()} roles.");

                await Context.Channel.SendIonicMessageAsync(new IonicMessage(embedMsg));
            }
        }