Ejemplo n.º 1
0
        public async Task ConnectAndLogin()
        {
            try
            {
                dbid = Convert.ToInt32(configuration.GetSection("Teamspeak:DatabaseID").Value);

                string server   = configuration.GetSection("Teamspeak:Server").Value;
                string port     = configuration.GetSection("Teamspeak:Port").Value;
                string username = configuration.GetSection("Teamspeak:Username").Value;
                string password = configuration.GetSection("Teamspeak:Password").Value;
                nickname = configuration.GetSection("Nickname").Value;

                tsClient = new TeamSpeakClient(server, Convert.ToInt32(port));

                // Create rich client instance
                await tsClient.Connect();                 // connect to the server

                await tsClient.Login(username, password); // login to do some stuff that requires permission

                await tsClient.UseServer(1);              // Use the server with id '1'

#if DEBUG
                await tsClient.ChangeNickName(nickname + "_DEBUG");
#else
                await tsClient.ChangeNickName(nickname);
#endif


                botClient = await tsClient.WhoAmI();

                await tsClient.MoveClient(botClient.ClientId, 1);

                await tsClient.RegisterChannelNotification(1);

                tsClient.Subscribe <ClientMoved>(ClientMoved);

                await tsClient.RegisterTextChannelNotification();

                tsClient.Subscribe <TextMessage>(ChatMessageReceived);

                var pChannel = await tsClient.GetChannels();
            }
            catch (Exception e)
            {
                logger.LogWarning(e.Message);
                logger.LogWarning(e.StackTrace);
            }
        }
Ejemplo n.º 2
0
        private void ChangeUserLobby(UserTeam userTeam, ClientEnterView clientEnterView = null)
        {
            try
            {
                if (_teamSpeakClient == null || !_teamSpeakClient.Client.IsConnected)
                {
                    _teamSpeakClient = ConnectToTeamspeak().Result;
                }

                if (_channelList == null || _channelList.Count == 0)
                {
                    _channelList = GetChannelList().Result;
                }
                var currentClients = GetClients().Result;
                if (currentClients == null)
                {
                    if (ConfigurationManager.AppSettings["Debug"] == "True")
                    {
                        _eventLog1.WriteEntry($"Failed to retrieve CurrentClients");
                    }
                    return;
                }

                TsUser tsUser;
                if (clientEnterView != null)
                {
                    tsUser = new TsUser
                    {
                        ChannelId = clientEnterView.TargetChannelId,
                        UserId    = clientEnterView.Id,
                        UserName  = clientEnterView.NickName
                    };
                }
                else
                {
                    var user = currentClients.FirstOrDefault(x =>
                                                             string.Equals(x.NickName, userTeam.TsUser, StringComparison.CurrentCultureIgnoreCase));
                    if (user == null)
                    {
                        if (ConfigurationManager.AppSettings["Debug"] == "True")
                        {
                            _eventLog1.WriteEntry($"User: {userTeam.TsUser} not found in ts3");
                        }
                        return;
                    }

                    tsUser = new TsUser
                    {
                        ChannelId = user.ChannelId,
                        UserId    = user.Id,
                        UserName  = user.NickName
                    };
                }

                if (ConfigurationManager.AppSettings["Debug"] == "True")
                {
                    _eventLog1.WriteEntry($"User: {userTeam.TsUser} found in ts3");
                }
                var channel = _channelList.FirstOrDefault(x => x.Name == userTeam.TsChannel);
                if (channel == null)
                {
                    if (ConfigurationManager.AppSettings["Debug"] == "True")
                    {
                        _eventLog1.WriteEntry($"Channel: {userTeam.TsChannel} not found in ts3");
                    }
                    return;
                }
                if (ConfigurationManager.AppSettings["Debug"] == "True")
                {
                    _eventLog1.WriteEntry($"Channel: {userTeam.TsChannel} found in ts3 for user {userTeam.TsUser}");
                }
                if (channel.Id == tsUser.ChannelId)
                {
                    if (ConfigurationManager.AppSettings["Debug"] == "True")
                    {
                        _eventLog1.WriteEntry($"User: {userTeam.TsUser} is already in channel: {channel.Name}");
                    }
                    return;
                }
                if (ConfigurationManager.AppSettings["Debug"] == "True")
                {
                    _eventLog1.WriteEntry($"Change user: {tsUser.UserName} to channel {channel.Name}");
                }
                _teamSpeakClient.MoveClient(tsUser.UserId, channel.Id);
            }
            catch (Exception e)
            {
                _eventLog1.WriteEntry(e.Message, EventLogEntryType.Error);
            }
        }