Exemplo n.º 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);
    }
    public Task SetPrefix(CommandContext commandContext, string prefix)
    {
        return(InvokeAsync(commandContext,
                           async commandContextContainer =>
        {
            if (string.IsNullOrWhiteSpace(prefix) == false &&
                prefix.Length > 0 &&
                prefix.Any(char.IsControl) == false)
            {
                PrefixResolvingService.AddOrRefresh(commandContext.Guild.Id, prefix);

                await commandContext.RespondAsync(LocalizationGroup.GetFormattedText("UsingNewPrefix", "I will use the following prefix: {0}", prefix))
                .ConfigureAwait(false);
            }
        }));
    }