コード例 #1
0
        public DiscordBot(IServiceProvider services)
        {
            var json = string.Empty;

            using (var fs = File.OpenRead("config.json"))
                using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
                    json = sr.ReadToEnd();

            var configJson = JsonConvert.DeserializeObject <ConfigJson>(json);

            var config = new DiscordConfiguration
            {
                Token                 = configJson.token,
                TokenType             = TokenType.Bot,
                AutoReconnect         = true,
                LogLevel              = LogLevel.Debug,
                UseInternalLogHandler = true,
            };

            Client = new DiscordClient(config);

            Client.Ready += OnClientReady;

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

            var commandsConfig = new CommandsNextConfiguration
            {
                StringPrefixes      = new string[] { configJson.prefix },
                EnableDms           = false,
                EnableMentionPrefix = true,
                DmHelp   = true,
                Services = services
            };

            Commands = Client.UseCommandsNext(commandsConfig);

            Commands.RegisterCommands <GameCommands>();
            Commands.RegisterCommands <RequestBuyCommands>();
            Commands.RegisterCommands <AdminCommands>();
            Commands.RegisterCommands <ProfileCommands>();

            _profileService = services.GetRequiredService <IProfileService>();

            Client.MessageCreated += async e =>
            {
                if (_profileService == null)
                {
                    return;
                }

                if (e.Author.IsBot == true)
                {
                    return;
                }

                var profile = await _profileService.GetOrCreateProfileAsync(e.Author.Id, e.Guild.Id);

                if (profile == null)
                {
                    return;
                }

                if (profile.Message == 20)
                {
                    await _profileService.ChangeUserGold(profile.DiscordId, profile.GuildId, 500, false); await _profileService.WipeMessage(profile.DiscordId, profile.GuildId);
                }
                if (profile.Message < 20 && profile.Message > 0)
                {
                    await _profileService.AddOneToMessage(profile.DiscordId, profile.GuildId);
                }
                else
                {
                    await _profileService.WipeMessage(profile.DiscordId, profile.GuildId);
                }
            };

            Client.ConnectAsync();
        }