Beispiel #1
0
        public static async Task <RestVoiceChannel> CreateAutoVCChannel(SocketGuild guild, string baseName)
        {
            RestVoiceChannel vcChannel =
                await(guild).CreateVoiceChannelAsync($"➕ New {baseName} VC");

            ServerVoiceChannel voiceChannel = new ServerVoiceChannel(vcChannel.Id, baseName);

            ServerListsManager.GetServer(guild).AutoVoiceChannels.Add(voiceChannel);
            ServerListsManager.SaveServerList();

            return(vcChannel);
        }
Beispiel #2
0
        public async Task UserVoiceStateUpdated(SocketUser user, SocketVoiceState before,
                                                SocketVoiceState after)
        {
            try
            {
                ServerList server = ServerListsManager.GetServer(((SocketGuildUser)user).Guild);

                //If we are adding an auto voice channel
                if (after.VoiceChannel != null)
                {
                    ServerVoiceChannel voiceChannel = server.GetAutoVoiceChannel(after.VoiceChannel.Id);
                    if (voiceChannel.Name != null)
                    {
                        RestVoiceChannel createdChannel =
                            await after.VoiceChannel.Guild.CreateVoiceChannelAsync(
                                $"{voiceChannel.Name} #" + (server.ActiveAutoVoiceChannels.Count + 1), x =>
                        {
                            x.CategoryId = after.VoiceChannel.CategoryId;
                            x.Bitrate    = after.VoiceChannel.Bitrate;
                            x.Position   = after.VoiceChannel.Position + 1;
                        });

                        if (createdChannel.CategoryId != null)
                        {
                            await createdChannel.SyncPermissionsAsync();
                        }

                        //Move the user who created the channel to the new channel
                        await((SocketGuildUser)user).ModifyAsync(x => { x.ChannelId = createdChannel.Id; });

                        server.ActiveAutoVoiceChannels.Add(createdChannel.Id);
                        ServerListsManager.SaveServerList();
                    }
                }

                //If we are removing an auto voice channel
                if (before.VoiceChannel != null)
                {
                    ulong activeChannel = server.GetActiveVoiceChannel(before.VoiceChannel.Id);
                    if (activeChannel != 0)
                    {
                        //There are no user on the active auto voice channel
                        if (before.VoiceChannel.Users.Count == 0)
                        {
                            await before.VoiceChannel.DeleteAsync();

                            server.ActiveAutoVoiceChannels.Remove(before.VoiceChannel.Id);
                            ServerListsManager.SaveServerList();
                        }
                    }
                }

                //Only check channel user count if the audio services are enabled.
                if (Config.bot.AudioSettings.AudioServicesEnabled)
                {
                    //There is an active song playing on this guild
                    ServerMusicItem musicItem = AudioService.GetMusicList(((SocketGuildUser)user).Guild.Id);
                    if (musicItem != null && musicItem.AudioChannel.Id == before.VoiceChannel?.Id)
                    {
                        //It is literally just us in the voice channel
                        if (before.VoiceChannel.Users.Count == 1)
                        {
                            await AudioService.StopPlayingAudioOnServer(musicItem);

                            //Leave this voice channel
                            await musicItem.AudioClient.StopAsync();

                            await musicItem.StartChannel.SendMessageAsync(
                                ":musical_note: Left the audio channel due to there being no one there :(");

                            AudioService.currentChannels.Remove(musicItem);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Logger.Log(ex.ToString(), LogVerbosity.Error);
#else
                Logger.Log(ex.Message, LogVerbosity.Error);
#endif
            }
        }