Exemple #1
0
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
        public static async Task <RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
                                                                            string name, RequestOptions options, Action <VoiceChannelProperties> func = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException(paramName: nameof(name));
            }

            var props = new VoiceChannelProperties();

            func?.Invoke(props);

            var args = new CreateGuildChannelParams(name, ChannelType.Voice)
            {
                CategoryId = props.CategoryId,
                Bitrate    = props.Bitrate,
                UserLimit  = props.UserLimit,
                Position   = props.Position,
                Overwrites = props.PermissionOverwrites.IsSpecified
                    ? props.PermissionOverwrites.Value.Select(overwrite => new API.Overwrite
                {
                    TargetId   = overwrite.TargetId,
                    TargetType = overwrite.TargetType,
                    Allow      = overwrite.Permissions.AllowValue.ToString(),
                    Deny       = overwrite.Permissions.DenyValue.ToString()
                }).ToArray()
                    : Optional.Create <API.Overwrite[]>(),
            };
            var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);

            return(RestVoiceChannel.Create(client, guild, model));
        }
Exemple #2
0
        public static async Task <Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
                                                     Action <VoiceChannelProperties> func,
                                                     RequestOptions options)
        {
            var args = new VoiceChannelProperties();

            func(args);
            var apiArgs = new API.Rest.ModifyVoiceChannelParams
            {
                Bitrate    = args.Bitrate,
                Name       = args.Name,
                RTCRegion  = args.RTCRegion,
                Position   = args.Position,
                CategoryId = args.CategoryId,
                UserLimit  = args.UserLimit.IsSpecified ? (args.UserLimit.Value ?? 0) : Optional.Create <int>(),
                Overwrites = args.PermissionOverwrites.IsSpecified
                    ? args.PermissionOverwrites.Value.Select(overwrite => new API.Overwrite
                {
                    TargetId   = overwrite.TargetId,
                    TargetType = overwrite.TargetType,
                    Allow      = overwrite.Permissions.AllowValue.ToString(),
                    Deny       = overwrite.Permissions.DenyValue.ToString()
                }).ToArray()
                    : Optional.Create <API.Overwrite[]>(),
            };

            return(await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false));
        }
Exemple #3
0
        public static async Task <Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
                                                     Action <VoiceChannelProperties> func,
                                                     RequestOptions options)
        {
            var args = new VoiceChannelProperties();

            func(args);
            var apiArgs = new API.Rest.ModifyVoiceChannelParams
            {
                Bitrate   = args.Bitrate,
                Name      = args.Name,
                Position  = args.Position,
                UserLimit = args.UserLimit.IsSpecified ? (args.UserLimit.Value ?? 0) : Optional.Create <int>()
            };

            return(await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false));
        }
Exemple #4
0
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
        public static async Task <RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
                                                                            string name, RequestOptions options, Action <VoiceChannelProperties> func = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException(paramName: nameof(name));
            }

            var props = new VoiceChannelProperties();

            func?.Invoke(props);

            var args = new CreateGuildChannelParams(name, ChannelType.Voice)
            {
                CategoryId = props.CategoryId,
                Bitrate    = props.Bitrate,
                UserLimit  = props.UserLimit
            };
            var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);

            return(RestVoiceChannel.Create(client, guild, model));
        }
Exemple #5
0
        public async Task ChangeGameAndRole(SocketGuildUser beforeChangeUser, SocketGuildUser afterChangeUser)
        {
            SocketGuildUser user     = afterChangeUser;
            IRole           gameRole = null;
            SocketGuild     guild    = user.Guild;

            if (!string.IsNullOrWhiteSpace(beforeChangeUser.Activity?.Name) || !string.IsNullOrWhiteSpace(afterChangeUser.Activity?.Name))
            {
                if (beforeChangeUser.Activity?.Name != afterChangeUser.Activity?.Name)
                {
                    await new MarvBotBusinessLayer(new DataAccess(new DatabaseContext())).SaveUserAcitivity(user, beforeChangeUser.Activity?.Name ?? "", afterChangeUser.Activity?.Name ?? "");
                }
            }

            var beforeName = beforeChangeUser.Activity?.Name.Trim() ?? null;
            var afterName  = afterChangeUser.Activity?.Name.Trim() ?? null;

            if (afterChangeUser.Activity != null)
            {
                if (beforeChangeUser.Activity != null)
                {
                    if (beforeName == afterName)
                    {
                        return;
                    }

                    gameRole = guild.Roles.Where(x => x.ToString().Equals(beforeName) && !x.IsMentionable).FirstOrDefault();
                    await DeleteGameRoleAndVoiceChannel(guild, gameRole, user);

                    gameRole = null;
                }
                gameRole = guild.Roles.Where(x => x.ToString().Equals(afterName) && !x.IsMentionable).FirstOrDefault();

                if (gameRole == null) // if role does not exist, create it
                {
                    gameRole = await guild.CreateRoleAsync(afterName, permissions : GuildPermissions.None, color : Color.Default, isHoisted : false, false);
                }
                var channel = guild.VoiceChannels.Where(x => x.ToString().Equals(gameRole.Name) && x.Bitrate == 96000).FirstOrDefault();
                if (channel == null)
                {
                    var properties = new VoiceChannelProperties
                    {
                        Bitrate = 96000
                    };
                    var altChannel = await guild.CreateVoiceChannelAsync(gameRole.Name);

                    await altChannel.AddPermissionOverwriteAsync(guild.EveryoneRole, new OverwritePermissions(connect : PermValue.Deny, viewChannel : PermValue.Deny));

                    await altChannel.AddPermissionOverwriteAsync(gameRole, new OverwritePermissions(connect : PermValue.Allow, viewChannel : PermValue.Allow));

                    await altChannel.ModifyAsync(x => x.Bitrate = properties.Bitrate);

                    //await user.ModifyAsync(x => x.Channel = altChannel); // Flyttar användaren
                }
                //else
                //{
                //    await user.ModifyAsync(x => x.Channel = altChannel); // Flyttar användaren
                //}
                await user.AddRoleAsync(gameRole);
            }
            else if (beforeChangeUser.Activity != null && afterChangeUser.Activity == null)
            {
                gameRole = guild.Roles.Where(x => x.ToString().Equals(beforeName) && !x.IsMentionable).FirstOrDefault();

                if (gameRole == null)
                {
                    return;
                }

                await DeleteGameRoleAndVoiceChannel(guild, gameRole, user);

                //Discord.Rest.RestVoiceChannel altChannel = null;
                //SocketVoiceChannel channel = guild.VoiceChannels.Where(input => input.ToString().Equals("General")).FirstOrDefault();
                //if (channel == null)
                //{
                //    altChannel = await guild.CreateVoiceChannelAsync("General");
                //    await user.ModifyAsync(x => x.Channel = altChannel);
                //}
                //else
                //{
                //    await user.ModifyAsync(x => x.Channel = channel);
                //}
            }
        }