Beispiel #1
0
    /// <summary>
    /// Start the discord bot
    /// </summary>
    /// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
    public async Task StartAsync()
    {
        var config = new DiscordConfiguration
        {
            Token                 = Environment.GetEnvironmentVariable("SCRUFFY_DISCORD_TOKEN"),
            TokenType             = TokenType.Bot,
            AutoReconnect         = true,
            Intents               = DiscordIntents.All,
            LogTimestampFormat    = "yyyy-MM-dd HH:mm:ss",
            ReconnectIndefinitely = true              // TODO Connection handling
        };

        _discordClient = new DiscordClient(config);

        _discordClient.UseInteractivity(new InteractivityConfiguration
        {
            Timeout = TimeSpan.FromMinutes(2)
        });

        GlobalServiceProvider.Current.AddSingleton(_discordClient);

        _prefixResolver = new PrefixResolvingService();
        GlobalServiceProvider.Current.AddSingleton(_prefixResolver);

        _administrationPermissionsValidationService = new AdministrationPermissionsValidationService();
        GlobalServiceProvider.Current.AddSingleton(_administrationPermissionsValidationService);

        GlobalServiceProvider.Current.AddSingleton(new DiscordStatusService(_discordClient));

#if RELEASE
        _messageImportService = new MessageImportService(_discordClient);
        GlobalServiceProvider.Current.AddSingleton(_messageImportService);
#endif

        _commands = _discordClient.UseCommandsNext(new CommandsNextConfiguration
        {
            PrefixResolver      = _prefixResolver.OnPrefixResolver,
            EnableDms           = true,
            EnableMentionPrefix = true,
            CaseSensitive       = false,
            DmHelp            = false,
            EnableDefaultHelp = false,
            Services          = GlobalServiceProvider.Current.GetServiceProvider()
        });

        _commands.SetHelpFormatter <HelpCommandFormatter>();

        _commands.RegisterCommands(Assembly.Load("Scruffy.Commands"));

        _errorHandler = new DiscordErrorHandler(_commands);

        await _discordClient.ConnectAsync().ConfigureAwait(false);
    }
Beispiel #2
0
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
    /// </summary>
    /// <returns>A task that represents the asynchronous dispose operation.</returns>
    public async ValueTask DisposeAsync()
    {
        if (_discordClient != null)
        {
            _errorHandler?.Dispose();
            _errorHandler = null;

            await _discordClient.DisconnectAsync().ConfigureAwait(false);

            if (_messageImportService != null)
            {
                await _messageImportService.DisposeAsync().ConfigureAwait(false);

                _messageImportService = null;
            }

            _discordClient.Dispose();
        }
    }