Kick message
[IRC-YouTube-Bot.git] / src / 9main.ts
blob344fc4342735d792af44ff7c624b1ac6e16a0a9d
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                         console.log("M " + text);
27             switch(text){
28                 case "!YTBot -v": 
29                 Main.linkifier_bot.say(channel, "IRC-YouTube-Bot " + this.data_constants["VERSION"] +" - @Verniy\nhttps://github.com/ECHibiki/IRC-YouTube-Bot");
30                 break;
31                 case "!YTBot -h": 
32                 Main.linkifier_bot.say(channel, "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, "!YTBot -v : Output version info\n!YTBot -h : Output help info");
36                     break;
37                 default:
38                     var reg_pag = /\b(www\.youtube\.com\/watch\?[\w?=\-&_]+|youtu\.be\/[\w?=\-&_]+)\b/gu;
39                     if(reg_pag.test(text)){
40                         // Main.linkifier_bot.say(channel, "!YTBot: Recieved");
41                         this.details_fetcher.fetchYoutubeDetails(text.match(reg_pag), Main.displayYouTubeDetails, channel);
42                     }
43                     break;
44             }       
45         });
46                 
47                 Main.linkifier_bot.addListener('kick', (channel:string, nick:string, by:string, reason:string, message_obj:any) =>{
48             console.log("Bot was kicked from " + channel);
49                 }
50     }
52     static displayYouTubeDetails(details_obj:any, channel:string){
53         if(details_obj == undefined || details_obj == "" || details_obj.items.length == 0){
54             console.log("Failed");
55             Main.linkifier_bot.say(channel, "!YTBot: link error");
56                         return;
57         }
58         details_obj.items.forEach((details:any, ind:number)=>{
59             console.log(ind,channel,details.snippet.title + " [" + details.snippet.channelTitle + "]");
60             Main.linkifier_bot.say(channel,details.snippet.title + " [" + details.snippet.channelTitle + "]");
61         });
62     }
64     static init():void{
65         this.getConnectionProperties();        
66         this.initBot();
67         console.log("Bot Listening");
68     }
70 Main.init();