regex fix
[IRC-YouTube-Bot.git] / src / 9main.ts
blob006eb133cba07240811b3d10d4b14005dc3e1cd0
1 class Main{
3     static data_constants:any;
4     static irc = require("irc");
5     static details_fetcher = new DetailsFetcher();
6     static linkifier_bot:any;
8     static getConnectionProperties(){
9         console.log("Reading Constants");
10         var fs = require('fs');
11         this.data_constants = JSON.parse(fs.readFileSync('bot-properties.json','utf8'));
12     }
14     static initBot(){
15         console.log("Initializing Bot");
17         Main.linkifier_bot = new Main.irc.Client(this.data_constants["SERVER"], this.data_constants["NICK"], {
18             channels: this.data_constants["CHANNELS"],
19             port: this.data_constants["SERVER-PORT"],
20             floodProtection: true
21         });
23         console.log(this.data_constants["CHANNELS"]);
25         Main.linkifier_bot.addListener('message', (sender:string, channel:string, text:string, message_obj:any) => {
26             switch(text){
27                 case "!YTBot -v": 
28                 Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_blue", "IRC-YouTube-Bot " + this.data_constants["VERSION"] +" - @Verniy\n"
29                 + Main.irc.colors.wrap("dark_blue", "https://github.com/ECHibiki/IRC-YouTube-Bot")));
30                 break;
31                 case "!YTBot -h": 
32                 Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_blue", "Enter a YouTube link in the form of 'www.youtube.com/watch?*' or 'youtu.be/*' and this bot will output the details."));
33                     break;
34                 case "!YTBot": 
35                 Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_blue", "!YTBot -v :") 
36                     + " Output version info\n " + Main.irc.colors.wrap("dark_blue", "!YTBot -h :") 
37                     + " Output help info");
38                     break;
39                 case "#kissu": 
40                     Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_blue", "!YTBot Active"));
41                         break;
42                 default:
43                     var reg_pat_youtube = /\b(www\.youtube\.com\/watch\?[\w?=\-&_]+|youtu\.be\/[\w?=\-&_]+)\b/gu;
44                     var reg_pat_url = /\b(http[s]{0,1}:\/\/[\w?=\-&_%\.]+\.[\w?=\-&_%\.\/]+)\b/gu;
45                     if(reg_pat_youtube.test(text)){
46                         // Main.linkifier_bot.say(channel, "!YTBot: Recieved");
47                         this.details_fetcher.fetchYoutubeDetails(text.match(reg_pat_youtube), Main.displayYouTubeDetails, channel);
48                     }
49                     else if(reg_pat_url.test(text)){
50                         this.details_fetcher.fetchLinkDetails(text.match(reg_pat_url), Main.displayLinkDetails, channel);
51                     }
52                     break;
53             }       
54         });
55                 
56                 Main.linkifier_bot.addListener('kick', (channel:string, nick:string, by:string, reason:string, message_obj:any) =>{
57             console.log("Bot was kicked from " + channel);
58                 });
59     }
61     static displayYouTubeDetails(details_obj:any, channel:string){
62         if(details_obj == undefined || details_obj == "" || details_obj.items.length == 0){
63             console.log("Failed");
64             Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_blue", "!YTBot:") + Main.irc.colors.wrap("dark_red", " link error"));
65                         return;
66         }
67         details_obj.items.forEach((details:any, ind:number)=>{
68             console.log(ind,channel,details.snippet.title + " [" + details.snippet.channelTitle + "]");
69             Main.linkifier_bot.say(channel, Main.irc.colors.wrap("dark_red",details.snippet.title)
70                 + Main.irc.colors.wrap("dark_green"," [" + details.snippet.channelTitle + "]"));
71         });
72     }
74     static displayLinkDetails(details:any, channel:string){
75         console.log(details, channel, " _");
76         Main.linkifier_bot.say(channel, Main.irc.colors.wrap("gray","<" + details + ">"));
77     }
79     static init():void{
80         this.getConnectionProperties();        
81         this.initBot();
82         console.log("Bot Listening");
83     }
85 Main.init();