示例#1
0
        public async Task Run(string[] args)
        {
            log.Info($"OpBot {OpBotUtils.GetVersionText()}");
            OperationRepository operationRepository = new OperationRepository(Properties.Settings.Default.OperationFile);
            IAdminUser          admins;

            try
            {
                admins = new AdminUsers(Properties.Settings.Default.AdminUsers);
            }
            catch (FormatException)
            {
                log.Fatal("There are invalid AdminUsers entries in config file.");
                return;
            }

            _ops = operationRepository.Get();

            if (Properties.Settings.Default.devTrackerChannel != 0)
            {
                _devTracker = new DevTracker();
            }

            _client = new DiscordClient(new DiscordConfiguration()
            {
                Token                 = Properties.Settings.Default.OpBotToken,
                TokenType             = TokenType.Bot,
                LogLevel              = LogLevel.Debug,
                UseInternalLogHandler = false,
                AutoReconnect         = true,
            });
            _client.SetWebSocketClient <WebSocket4NetClient>();
            _client.ClientErrored += Client_ClientErrored;
            _client.DebugLogger.LogMessageReceived += DebugLogger_LogMessageReceived;

            _stopApplication = new CancellationTokenSource();

            _commandProcessor = new CommandProcessor(new CommandProcessorConfig()
            {
                OpBotUserId       = Properties.Settings.Default.OpBotUserId,
                OpBotChannelId    = Properties.Settings.Default.OpBotChannel,
                Names             = _names,
                Repository        = operationRepository,
                AdminUsers        = admins,
                Client            = _client,
                Ops               = _ops,
                CommandCharacters = Properties.Settings.Default.CommandChars,
                StopApplication   = _stopApplication,
            });

            _client.MessageCreated += Client_MessageCreated;
            _client.GuildAvailable += Client_GuildAvailable;

            _client.GuildMemberAdded   += Client_GuildMemberAdded;
            _client.GuildMemberUpdated += Client_GuildMemberUpdated;
            _client.Ready += Client_Ready;

            _botStatus = new BotStatus(_client);

            // this used to throw an UnauthorizedException when login failed, in DSharpPlus 3.2.3 it
            // now throws System.Exception and even if it is caught the DSharpPlus is unstable and will
            // crash out, so no point in catching it, only option is to let the app bomb out.
            await _client.ConnectAsync();

            Console.CancelKeyPress += Console_CancelKeyPress;
            try { await Task.Delay(-1, _stopApplication.Token); } catch (TaskCanceledException) { }

            if (_devTracker != null)
            {
                try { await _devTracker.Stop(); } catch (TaskCanceledException) { }
                _devTracker.Dispose();
            }
            await _client.DisconnectAsync();

            _client.Dispose();
        }
示例#2
0
 private async Task VersionCommand(MessageCreateEventArgs e)
 {
     string text = "Version: " + DiscordText.BigText(OpBotUtils.GetVersionText());
     await e.Channel.SendMessageAsync(text);
 }