Exemplo n.º 1
0
        public JosephineBot(JosephineConfig cfg, int shardid)
        {
            Config = cfg;

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

            debugMode = Config.debug;
            Discord   = new DiscordClient(dcfg);

            Discord.DebugLogger.LogMessageReceived += this.DebugLogger_LogMessageRecieved;
            Discord.Ready          += this.Client_Ready;
            Discord.ClientErrored  += this.Client_ClientError;
            Discord.GuildUpdated   += this.Discord_GuildUpdated;
            Discord.ChannelDeleted += this.Discord_ChannelDeleted;
            Discord.MessageCreated += this.Client_Chat;
            Discord.MessageUpdated += this.MessageUpdated;
            //Discord.GuildMemberAdded += this.Join_Chat;

            var vcfg = new VoiceNextConfiguration
            {
                AudioFormat    = AudioFormat.Default,
                EnableIncoming = true
            };

            VoiceService = Discord.UseVoiceNext(vcfg);

            var depco = new ServiceCollection();

            // commandsnext config and the commandsnext service itself
            var cncfg = new CommandsNextConfiguration
            {
                StringPrefixes           = Config.CommandPrefixes,
                EnableDms                = false,
                EnableMentionPrefix      = true,
                CaseSensitive            = false,
                Services                 = depco.BuildServiceProvider(true),
                IgnoreExtraArguments     = false,
                UseDefaultCommandHandler = true,
            };

            this.CommandsNextService = Discord.UseCommandsNext(cncfg);
            this.CommandsNextService.CommandErrored  += this.CommandsNextService_CommandErrored;
            this.CommandsNextService.CommandExecuted += this.Commands_CommandExecuted;
            this.CommandsNextService.RegisterCommands(typeof(JosephineBot).GetTypeInfo().Assembly);
            this.CommandsNextService.SetHelpFormatter <HelpFormatter>();
        }
Exemplo n.º 2
0
        public static async Task MainAsync(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;

            Console.WriteLine("Configuring");
            Console.WriteLine("Checking the config file!");
            var cfg  = new JosephineConfig();
            var json = string.Empty;

            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;
            }

            Console.WriteLine("Checking the prefs file!");
            var savedConfig          = string.Empty;
            List <guildData> dataSet = new List <guildData>();

            if (!File.Exists("prefs.json"))
            {
                savedConfig = JsonConvert.SerializeObject(dataSet);
                File.WriteAllText("prefs.json", savedConfig, new UTF8Encoding(false));
                Console.WriteLine("Written a new prefs file... as it didnt exist before");
            }

            Console.WriteLine("Loading the config file!");
            json = File.ReadAllText("config.json", new UTF8Encoding(false));
            cfg  = JsonConvert.DeserializeObject <JosephineConfig>(json);

            Console.WriteLine("Loading the prefs file!");
            savedConfig       = File.ReadAllText("prefs.json", new UTF8Encoding(false));
            JosephineBot.data = JsonConvert.DeserializeObject <List <guildData> >(savedConfig);

            Console.WriteLine("Checking user data!");
            var savedUsers            = string.Empty;
            List <guildData> dataUser = new List <guildData>();

            if (!File.Exists("savedata.json"))
            {
                savedUsers = JsonConvert.SerializeObject(savedUsers);
                File.WriteAllText("savedata.json", savedUsers, new UTF8Encoding(false));
                Console.WriteLine("written a new savedata file... as it didnt exist before");
            }

            savedUsers            = File.ReadAllText("savedata.json", new UTF8Encoding(false));
            JosephineBot.saveData = JsonConvert.DeserializeObject <List <UserData> >(savedUsers);

            Console.WriteLine("Configured!");

            var tskl = new List <Task>();

            for (var i = 0; i < cfg.ShardCount; i++)
            {
                var bot = new JosephineBot(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 */ }
        }