Exemplo n.º 1
0
        public Bot(ConfigJson cfg, int shardid)
        {
            // then we want to instantiate our client
            this.Config = cfg;

            // discord instance config and the instance itself
            var dcfg = new DiscordConfiguration
            {
                AutoReconnect         = true,
                LargeThreshold        = 250,
                LogLevel              = LogLevel.Debug,
                Token                 = this.Config.Token,
                TokenType             = TokenType.Bot,
                UseInternalLogHandler = false,
                ShardId               = shardid,
                ShardCount            = this.Config.ShardCount,
                MessageCacheSize      = 2048,
                DateTimeFormat        = "dd-MM-yyyy HH:mm:ss zzz"
            };

            this.Client = new DiscordClient(dcfg);


            this.Client.Ready          += this.Client_Ready;
            this.Client.GuildAvailable += this.Client_GuildAvailable;
            this.Client.ClientErrored  += this.Client_ClientError;
            // up next, let's set up our commands
            var ccfg = new CommandsNextConfiguration
            {
                // let's use the string prefix defined in config.json
                StringPrefixes = this.Config.CommandPrefix,

                // enable responding in direct messages
                EnableDms = true,

                // enable mentioning the bot as a command prefix
                EnableMentionPrefix = true
            };

            // and hook them up
            this.CommandsNextService = this.Client.UseCommandsNext(ccfg);
            this.CommandsNextService.CommandExecuted += this.Commands_CommandExecuted;
            this.CommandsNextService.CommandErrored  += this.Commands_CommandErrored;
            this.Client.GuildBanAdded    += this.Ban;
            this.Client.GuildBanRemoved  += this.Unban;
            this.Client.GuildMemberAdded += this.NewMember;
            System.Threading.Thread liveChecker = new System.Threading.Thread(async e => await CheckLive(this.Client));
            liveChecker.Start();
            // let's add a converter for a custom type and a name

            //general (ping)
            this.CommandsNextService.RegisterCommands <ExampleUngrouppedCommands>();
            //MyAnimeList Commands
            //this.CommandsNextService.RegisterCommands<MyAnimeListCommands>();
            //Programmation
            //this.CommandsNextService.RegisterCommands<Programmation>();

            //Check member
            //this.Commands.RegisterCommands<MemberCheck>();

            /*  //Check nouveaux mambres
             *          var memberTimer = new System.Threading.Timer(
             *              async e => await CheckMember(this.Client), null, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(10));
             */
        }
Exemplo n.º 2
0
        public static async Task RunBotAsync()
        {
            Console.Title = "Zerator Discord";

            var    cfg = new ConfigJson();
            string json;

            if (!File.Exists("config.json"))
            {
                json = JsonConvert.SerializeObject(cfg);
                File.WriteAllText("config.json", json, new UTF8Encoding(false));
                Console.WriteLine("Config file was not found, a new one was generated. Fill it with proper values and rerun this program");
                Console.ReadKey();

                return;
            }
            if (!File.Exists("member.json"))
            {
                var cfgProg = new ListMembers();
                json = JsonConvert.SerializeObject(cfgProg);
                File.WriteAllText("member.json", json, new UTF8Encoding(false));
                Console.WriteLine("member file was not found, a new one was generated.");
            }
            if (!File.Exists("prog.json"))
            {
                var cfgProg = new ProgJson();
                json = JsonConvert.SerializeObject(cfgProg);
                File.WriteAllText("prog.json", json, new UTF8Encoding(false));
                Console.WriteLine("prog file was not found, a new one was generated.");
            }
            if (!File.Exists("twitch.json"))
            {
                var cfgTwitch = new StreamerStatus();
                json = JsonConvert.SerializeObject(cfgTwitch);
                File.WriteAllText("twitch.json", json, new UTF8Encoding(false));
                Console.WriteLine("twitch file was not found, a new one was generated.");
            }
            if (!File.Exists("configMAL.json"))
            {
                var cfgTwitch = new Dictionary <string, string>(new Dictionary <string, string>());;
                json = JsonConvert.SerializeObject(cfgTwitch);
                File.WriteAllText("configMAL.json", json, new UTF8Encoding(false));
                Console.WriteLine("configMAL file was not found, a new one was generated.");
            }
            json = File.ReadAllText("config.json", new UTF8Encoding(false));
            cfg  = JsonConvert.DeserializeObject <ConfigJson>(json);


            var tskl = new List <Task>();

            for (var i = 0; i < cfg.ShardCount; i++)
            {
                var bot = new Bot(cfg, i);
                Shards.Add(bot);
                tskl.Add(bot.RunAsync());
                await Task.Delay(7500).ConfigureAwait(false);
            }

            await Task.WhenAll(tskl).ConfigureAwait(false);

            try
            {
                await Task.Delay(-1, CancelToken).ConfigureAwait(false);
            }
            catch (Exception) { /* shush */ }
        }