Esempio n. 1
0
        internal static void Read()
        {
            var json = string.Empty;

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

            config = JsonConvert.DeserializeObject <ConfigJson>(json);
        }
Esempio n. 2
0
        public async Task RunAsync(ConfigJson config)
        {
            //Discord Client config
            var discordConfig = new DiscordConfiguration
            {
                Token         = config.BotConfig.Token,
                TokenType     = TokenType.Bot,
                AutoReconnect = config.BotConfig.AutoReconnect,
                Intents       = DiscordIntents.All
            };

            //Initialize Discord Client
            Client = new DiscordClient(discordConfig);

            #region Events
            //Get Client Interactivity
            Client.UseInteractivity(new InteractivityConfiguration
            {
                Timeout = TimeSpan.FromMinutes(5)
            });

            //On new reaction
            Client.MessageReactionAdded += async(s, e) =>
            {
                //Grant Roles Anni Discord
                Functions.Functions.GrantRolesByReaction(s, e);
            };

            //On removed reaction
            Client.MessageReactionRemoved += async(s, e) =>
            {
                //Revoke Roles Anni Discord
                Functions.Functions.RemoveRolesByReaction(s, e);
            };

            //On message created
            Client.MessageCreated += async(s, e) =>
            {
                if (e.Message.Content.ToLower().Contains("datboi") && e.Author.Id != 853976207493038090)
                {
                    await e.Message.RespondAsync("https://cdn.discordapp.com/attachments/916043520610017301/943957313314754580/shitsfire.jpg");
                }
            };

            //On button press
            Client.ComponentInteractionCreated += async(s, e) =>
            {
                //Functions.Functions.ButtonPressed(s, e);
            };
            #endregion

            //Discord Command config
            var cmdConfig = new CommandsNextConfiguration
            {
                StringPrefixes       = new string[] { config.BotConfig.Prefix },
                EnableMentionPrefix  = config.CommandConfig.EnableMentionPrefix,
                EnableDms            = config.CommandConfig.EnableDms,
                IgnoreExtraArguments = config.CommandConfig.IgnoreExtraArguments
            };

            //Initialize Commands in Client
            Commands = Client.UseCommandsNext(cmdConfig);

            //Register Command Modules
            Commands.RegisterCommands <StandardCommands>();
            Commands.RegisterCommands <FishCommands>();
            Commands.RegisterCommands <TwitterCommands>();
            Commands.RegisterCommands <AnniCommands>();

            //Connect Client
            await Client.ConnectAsync();

            //Endless Task Delay
            await Task.Delay(-1);
        }
Esempio n. 3
0
        public async Task RunAsync()
        {
            string json = string.Empty;

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

            configJson = JsonConvert.DeserializeObject <ConfigJson>(json);
            DiscordConfiguration config = new DiscordConfiguration
            {
                Token           = configJson.Token,
                TokenType       = TokenType.Bot,
                AutoReconnect   = true,
                MinimumLogLevel = LogLevel.Debug
            };

            Client = new DiscordClient(config);

            Client.Ready += OnClientReady;

            //DiscordGuild server = await Client.GetGuildAsync(Convert.ToUInt64(configJson.ServerId));
            //DiscordChannel channel = server.GetChannel(Convert.ToUInt64(configJson.ChannelId));
            //await channel.SendMessageAsync("@everyone: Bot Ready " + DateTime.Now.ToString("h:mm:ss tt"));

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

            Commands = Client.UseCommandsNext(commandsConfig);
            //Add commands here
            Commands.RegisterCommands <InfoCommands>();
            //
            await Client.ConnectAsync();

            GetTimetable();
            //Timetable function
            async void GetTimetable()
            {
                short dayNumberOfWeek = (short)DateTime.Now.DayOfWeek;

                if (dayNumberOfWeek >= 1 & dayNumberOfWeek <= 6)
                {
                    dayNumberOfWeek += -1;
                    StdSchedulerFactory factory   = new StdSchedulerFactory();
                    IScheduler          scheduler = await factory.GetScheduler();

                    //Timetable Website
                    string  html = @configJson.Website;
                    HtmlWeb web  = new HtmlWeb();

                    var htmlDoc = web.Load(html);

                    string[] activites = new string[30]; //Activities array

                    short[][] clockDays =
                    {
                        new short[6],
                        new short[6],
                        new short[6],
                        new short[6],
                        new short[6],
                    };

                    string[][] activityDays =
                    {
                        new string[6],
                        new string[6],
                        new string[6],
                        new string[6],
                        new string[6],
                        new string[6],
                    };


                    short indexActivity = 0;
                    foreach (HtmlNode activityNode in htmlDoc.DocumentNode.SelectNodes("//body/table[2]/tbody/td[1]")) //Goes through subject name html
                    {
                        string subjectName = (activityNode.InnerHtml);
                        if (!subjectName.Contains("table"))         //filters out the useless information
                        {
                            activites[indexActivity] = subjectName; //puts it in array for later use
                            indexActivity++;
                        }
                    }

                    short currentActivity    = -1;
                    short currentWeekday     = -1;
                    short currentEntryAmount = 0;
                    foreach (HtmlNode timeNode in htmlDoc.DocumentNode.SelectNodes("//body/table/tr/td[4]")) // Goes through time html
                    {
                        currentActivity++;
                        string clockTime = (timeNode.InnerHtml);
                        if (clockTime == "Start")
                        {
                            currentWeekday++;
                            currentEntryAmount = 0;
                        }
                        else
                        {
                            clockTime = clockTime.Replace(":00", "");
                            activityDays[currentWeekday][currentEntryAmount] = activites[currentActivity];
                            clockDays[currentWeekday][currentEntryAmount]    = Int16.Parse(clockTime);
                            currentEntryAmount++;
                        }
                    }

                    IJobDetail job = JobBuilder.Create <HelloJob>()
                                     .WithIdentity("Job", "Subject")
                                     .StoreDurably()
                                     .Build();
                    await scheduler.AddJob(job, false);

                    for (short s = 0; s < clockDays[dayNumberOfWeek].Length; s++)
                    {
                        short  clockTime = clockDays[dayNumberOfWeek][s];
                        string activity  = activityDays[dayNumberOfWeek][s];
                        if (clockTime > 0)
                        {
                            await scheduler.Start();

                            ITrigger trigger = (ITrigger)TriggerBuilder.Create()
                                               .WithIdentity((activity + s), "Subject")
                                               .StartAt(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).Add(new TimeSpan((clockTime - 1), 45, 0)))
                                               .ForJob(job)
                                               .Build();
                            trigger.JobDataMap["data"] = new string[] { clockTime.ToString(), activity };
                            await scheduler.ScheduleJob(trigger);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("This Program only works on weekdays.");
                }
            }

            await Task.Delay(-1);
        }