コード例 #1
0
        /// <summary>
        /// Создаёт новый обьект бота.
        /// </summary>
        /// <param name="token">Токен для подключения.</param>
        /// <param name="loger">Пользовательский класс для логирования.</param>
        public DiscordBot(string token, ILoger loger = null, ICommandManager commandManager = null)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException(nameof(token));
            }



            Loger          = loger;
            CommandManager = commandManager;



            if (Loger is null)
            {
                Loger = new DefaultLoger();
            }

            if (CommandManager is null)
            {
                CommandManager = new DefaultManager();
            }



            Init(token);
        }
コード例 #2
0
        public DiscordBot(IBotConfigurations botConfigurations)
        {
            if (botConfigurations == null)
            {
                throw new ArgumentNullException(nameof(botConfigurations));
            }

            if (string.IsNullOrEmpty(botConfigurations.Token))
            {
                throw new ArgumentNullException(nameof(botConfigurations.Token));
            }



            Loger          = botConfigurations.Loger;
            CommandManager = botConfigurations.CommandManager;
            HiPeople       = botConfigurations.HiPeople;



            if (Loger is null)
            {
                Loger = new DefaultLoger();
            }

            if (CommandManager is null)
            {
                CommandManager = new DefaultManager();
            }



            Init(botConfigurations.Token);
        }
コード例 #3
0
ファイル: DiscordBot.cs プロジェクト: 8ByteCore8/DiscordBot
        /// <summary>
        /// Создаёт новый обьект бота.
        /// </summary>
        /// <param name="token">Токен для подключения.</param>
        /// <param name="loger">Пользовательский класс для логирования.</param>
        public DiscordBot(string token, ILoger loger = null, ICommandManager controler = null)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (loger == null)
            {
                Loger = new DefaultLoger();
            }
            else
            {
                Loger = loger;
            }

            if (controler == null)
            {
                CommandManager = new DefaultManager();
            }
            else
            {
                CommandManager = controler;
            }

            CommandManager.Sturtup();

            CommandManager.LoadCommands();

            //инициализация ботa

            DiscordClient = new DiscordSocketClient();

            CommandService command = new CommandService();

            IServiceProvider provider = new ServiceCollection()
                                        .AddSingleton(DiscordClient)
                                        .AddSingleton(command)
                                        .BuildServiceProvider();

            DiscordClient.LoginAsync(TokenType.Bot, token);

            #region Обработчики событий

            DiscordClient.MessageReceived += DiscordClient_MessageReceived;
            DiscordClient.MessageUpdated  += DiscordClient_MessageUpdated;;

            #endregion Обработчики событий

            //подключение бота к серверу
            try
            {
                DiscordClient.StartAsync().Wait();
                Mention = DiscordClient.CurrentUser.Mention;
            }
            catch (Exception ex)
            {
                throw new BotException($"Не удалось подключиться к дискорду.\n\tподробности: {ex.Message}", ex);
            }

            Loger.Log($"Бот успешно подключён");
        }