public async Task UserVoiceStateUpdatedForCreateVoiceChannel(SocketUser user,
                                                                     SocketVoiceState voiceStateBefore,
                                                                     SocketVoiceState voiceStateAfter)
        {
            var userAccount = _accounts.GetAccount(user);

            if (userAccount.VoiceChannelList.Count <= 0)
            {
                return;
            }
            if (voiceStateAfter.VoiceChannel?.Id != userAccount.VoiceChannelList[0].VoiceChannelId &&
                voiceStateBefore.VoiceChannel.Id != userAccount.VoiceChannelList[0].VoiceChannelId)
            {
                return;
            }


            var voiceChannelId = userAccount.VoiceChannelList[0].VoiceChannelId;
            var guildId        = userAccount.VoiceChannelList[0].GuildId;

            if (voiceStateAfter.VoiceChannel != null && voiceStateAfter.VoiceChannel.Users.Contains(user))
            {
                var newVoice =
                    new AccountSettings.CreateVoiceChannel(DateTime.UtcNow.AddHours(10), voiceChannelId, guildId);
                userAccount.VoiceChannelList[0] = newVoice;
            }

            if (voiceStateAfter.VoiceChannel == null || !voiceStateAfter.VoiceChannel.Users.Contains(user))
            {
                var newVoice = new AccountSettings.CreateVoiceChannel(DateTime.UtcNow, voiceChannelId, guildId);
                userAccount.VoiceChannelList[0] = newVoice;
            }
        }
        public async Task CreateVoiceChannel(int maxSize = 5, string name = null)
        {
            var user = UserAccounts.GetAccount(Context.User, 0);

            if (maxSize > 99)
            {
                maxSize = 99;
            }
            if (user.VoiceChannelList.Count >= 1)
            {
                await ReplyAsync($"You already have a channel.");

                return;
            }

            if (name == null)
            {
                name = $"{Context.User.Username}-Channel";
            }
            else
            {
                if (name.Length > 30)
                {
                    name = $"{Context.User.Username}-Channel";
                }
            }

            var category     = Context.Channel as ITextChannel;
            var voiceChannel = await Context.Guild.CreateVoiceChannelAsync(name, prop =>
            {
                prop.UserLimit  = maxSize;
                prop.Bitrate    = 64000;
                prop.Name       = name;
                prop.CategoryId = category?.GetCategoryAsync().Result.Id;
            });

            await voiceChannel.AddPermissionOverwriteAsync(Context.User, OverwritePermissions.AllowAll(voiceChannel),
                                                           RequestOptions.Default);


            if (category != null)
            {
                var newVoice =
                    new AccountSettings.CreateVoiceChannel(DateTime.UtcNow, voiceChannel.Id, category.Guild.Id);
                user.VoiceChannelList.Add(newVoice);
                UserAccounts.SaveAccounts(0);
            }
            else
            {
                await ReplyAsync("error.");

                return;
            }

            await ReplyAsync(
                $"Voice Channel have been Created, please join it, or I will delete it in 10 min\n" +
                $"{voiceChannel.CreateInviteAsync().Result.Url}");
        }
Example #3
0
        private void CheckToDeleteVoice(object sender, ElapsedEventArgs e)
        {
            var allUserAccounts = _accounts.GetAllAccount();

            foreach (var t in allUserAccounts)
            {
                try
                {
                    var globalAccount = _global.Client.GetUser(t.DiscordId);
                    var account       = _accounts.GetAccount(globalAccount);
                    if (account.VoiceChannelList.Count <= 0)
                    {
                        continue;
                    }

                    var difference = DateTime.UtcNow - account.VoiceChannelList[0].LastTimeLeftChannel;

                    if (difference.Minutes > 10)
                    {
                        var voiceChan = _global.Client.GetGuild(account.VoiceChannelList[0].GuildId)
                                        .GetVoiceChannel(account.VoiceChannelList[0].VoiceChannelId);

                        account.VoiceChannelList = new List <AccountSettings.CreateVoiceChannel>();


                        if (voiceChan.Users.Count <= 0)
                        {
                            voiceChan.DeleteAsync();
                        }
                        else if (voiceChan.Users.Count >= 1)
                        {
                            var usersList = voiceChan.Users.ToList();
                            var newHolder = _accounts.GetAccount(usersList[0]);

                            var newVoice = new AccountSettings.CreateVoiceChannel(DateTime.UtcNow.AddHours(10),
                                                                                  voiceChan.Id, voiceChan.Guild.Id);
                            newHolder.VoiceChannelList.Add(newVoice);

                            var guildUser = _global.Client.GetGuild(newHolder.VoiceChannelList[0].GuildId)
                                            .GetUser(newHolder.Id);
                            var k = voiceChan.AddPermissionOverwriteAsync(guildUser,
                                                                          OverwritePermissions.AllowAll(voiceChan),
                                                                          RequestOptions.Default);
                            var kk = voiceChan.RemovePermissionOverwriteAsync(globalAccount, RequestOptions.Default);
                        }
                    }
                }
                catch
                {
                    //
                }
            }
        }