コード例 #1
0
        public CommandHandlingService(IServiceProvider services)
        {
            _commands   = services.GetRequiredService <CommandService>();
            _discord    = services.GetRequiredService <DiscordSocketClient>();
            _services   = services;
            _envChecker = services.GetRequiredService <EnvCheckerService>();

            // Hook CommandExecuted to handle post-command-execution logic.
            _commands.CommandExecuted += CommandExecutedAsync;
            // Hook MessageReceived so we can process each message to see
            // if it qualifies as a command.
            _discord.MessageReceived += MessageReceivedAsync;
        }
コード例 #2
0
        public BotService(EnvCheckerService envCheckerService, IServiceProvider serviceProvider,
                          ILoggerFactory loggerFactory)
        {
            _envCheckerService = envCheckerService;
            _serviceProvider   = serviceProvider;
            _loggerFactory     = loggerFactory;
            Logger             = loggerFactory.CreateLogger <BotService>();


            var logFactory = new LoggerFactory().AddSerilog();

            var discordConfiguration = new DiscordConfiguration
            {
                Token         = _envCheckerService.Discord_Token,
                TokenType     = TokenType.Bot,
                LoggerFactory = logFactory
            };

            DiscordClient = new DiscordClient(discordConfiguration);

            var commandsNextConfiguration = new CommandsNextConfiguration
            {
                StringPrefixes = new[] { "!!" },
                Services       = _serviceProvider
            };

            var commands = DiscordClient.UseCommandsNext(commandsNextConfiguration);

            commands.RegisterCommands(Assembly.GetExecutingAssembly());

            DiscordClient.ClientErrored += (sender, args) =>
            {
                Logger.LogError(args.ToString());
                return(Task.CompletedTask);
            };

            DiscordClient.Ready += (sender, args) =>
            {
                Logger.LogInformation("Ready");
                return(Task.CompletedTask);
            };
        }