예제 #1
0
        private async Task RunBotAsync()
        {
            Global.ReadConfig();
            UserJoinEvent onUserJoinEvent = new UserJoinEvent();

            _client = new DiscordSocketClient(new DiscordSocketConfig
            {
                LogLevel            = LogSeverity.Debug,
                AlwaysDownloadUsers = true,
            });
            _commands = new CommandService();

            _services = new ServiceCollection()
                        .AddSingleton(_client)
                        .AddSingleton(_commands)
                        .BuildServiceProvider();

            _client.Log        += _client_Log;
            _client.UserJoined += onUserJoinEvent.OnUserJoin;

            await _client.SetGameAsync(Global.Status, null, ActivityType.Playing);

            await _client.SetStatusAsync(UserStatus.Online);

            await RegisterCommandsAsync();

            await _client.LoginAsync(TokenType.Bot, Global.Token);

            await _client.StartAsync();

            await Task.Delay(-1);
        }
예제 #2
0
        public async Task MainAsync()
        {
            using var services = ConfigureServices();
            _client            = services.GetRequiredService <DiscordSocketClient>();
            _ranking           = services.GetRequiredService <RankingService>();
            _config            = services.GetRequiredService <ConfigService>();
            _joinEvent         = services.GetRequiredService <UserJoinEvent>();
            _data     = services.GetRequiredService <DataService>();
            _auditer  = services.GetRequiredService <AuditService>();
            _manager  = services.GetRequiredService <RoleService>();
            _database = services.GetRequiredService <DatabaseService>();

            _client.Log   += Log;
            _client.Ready += OnReady;
            await _client.SetGameAsync("the door", type : ActivityType.Watching);

            await _client.LoginAsync(TokenType.Bot, _config.BotConfig.Token);

            await _client.StartAsync();

            await services.GetRequiredService <CommandHandlerService>().InstallCommandsAsync();

            // Block this task until the program is closed.
            await Task.Delay(-1);
        }
        private void UserJoinHandler(byte[] data)
        {
            UserJoin userJoin = NetworkUtils.Deserialize <UserJoin>(data);

            Application.Current.Dispatcher.BeginInvoke(new Action(() => {
                UserJoinEvent?.Invoke(userJoin);
            }));
        }
예제 #4
0
        private void Connector_LoginEvent(object sender, LoginEventArgs e)
        {
            Account account = GetAccount(e.UserID);

            if (account == null)
            {
                connector.SendLoginResult(new UserSocket(e.UserID, " ", e.ReceiveSocket), false);
                return;
            }

            UserSocket newUserSocket = new UserSocket(account.UserID, account.NickName, e.ReceiveSocket);

            if (account.ConfirmPassword(e.PassWord) && GetUserSocket(e.UserID) == null)
            {
                connector.SendLoginResult(newUserSocket, true);
                //connector.SendUserChange(newUserSocket, newUserSocket.ToUser(), CommandType.UserJoin);

                //向已登录的用户发送新登录用户的信息
                foreach (UserSocket oldUser in LoginedUserList)
                {
                    connector.SendUserChange(oldUser, newUserSocket.ToUser(), CommandType.UserJoin);
                }

                //向新登录用户发送已登录的用户的信息
                foreach (UserSocket oldUser in LoginedUserList)
                {
                    connector.SendUserChange(newUserSocket, oldUser.ToUser(), CommandType.UserJoin);
                }

                LoginedUserList.Add(newUserSocket);

                UserJoinEvent?.Invoke(this, new User(newUserSocket.UserID, newUserSocket.NickName));
                return;
            }
            else
            {
                connector.SendLoginResult(newUserSocket, false);
                return;
            }
        }