@jamppunet/

DiscordApi

Nodejs

Api for service

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
  • server.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const Discord = require('discord.js');
const client = new Discord.Client();
const token = process.env.DISCORD_BOT_SECRET;
const rapi = process.env.BOT_BACKEND_SERVICE
const prefix = process.env.BOT_PREFIX
const http = require('http');
const keepAlive = require('./server');

      Date.prototype.getWeek = function() {
        var date = new Date(this.getTime());
        date.setHours(0, 0, 0, 0);
        // Thursday in current week decides the year.
        date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
        // January 4 is always in week 1.
        var week1 = new Date(date.getFullYear(), 0, 4);
        // Adjust to Thursday in week 1 and count number of weeks from date to week1.
        return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                              - 3 + (week1.getDay() + 6) % 7) / 7);
      }

client.on('ready', () => {
  console.log("Bot is running...");
  //console.log(client.user.username);
});

client.on('message', msg => { //special in js commands
  if (msg.author.bot || msg.member.hasPermission("ADMINISTRATOR")) {
    if (msg.content.startsWith('!dtq')) { //datequery
      msg.delete(1000);
      var d = new Date();
      d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);

      let pvm = (d.getDate()+1) + "." + (d.getMonth()+1) + "." + d.getFullYear();
      // Returns the ISO week of the date.
      msg.channel.send('\**Matsipäiväkysely viikolle '+d.getWeek()+' **\nReagoi tähäh viestiin, minä päivinä ensiviikolla voisit osallistua matsiin. Matsit pyritään järjestämään alkamaan 21-22. Pe ja La matsit saattavat alkaa myöhemmin.\n<:ma:606779987873366027> '+(d.getDate()+0) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:ti:606780023734796298> '+(d.getDate()+1) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:ke:606780056618139648> '+(d.getDate()+2) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:to:606780089501483018> '+(d.getDate()+3) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:pe:606780143960064000> '+(d.getDate()+4) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:la:606780143838691351> '+(d.getDate()+5) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n<:su:606780144098738186> '+(d.getDate()+6) + "." + (d.getMonth()+1) + "." + d.getFullYear()+'\n')
      .then(function (msg) {
        msg.react('606779987873366027')
        .then(() => msg.react('606780023734796298'))
        .then(() => msg.react('606780056618139648'))
        .then(() => msg.react('606780089501483018'))
        .then(() => msg.react('606780143960064000'))
        .then(() => msg.react('606780143838691351'))
        .then(() => msg.react('606780144098738186'))
        .catch(() => console.error('One of the emojis failed to react.'));
      });
    }
  }

  //shortcuts
  if(msg.content.startsWith("!match")) {
    msg.content =msg.content.replace("!match","!fws match");
  }

  if(msg.content.startsWith("!reg")) {
    msg.content =msg.content.replace("!reg","!fws match reg");
  }

  if(msg.content.startsWith("!res")) {
    msg.content =msg.content.replace("!res","!fws match res");
  }

  if(msg.content.startsWith("!unreg")) {
    msg.content =msg.content.replace("!unreg","!fws match unreg");
  }

  if(msg.content.startsWith("!edit")) {
    msg.content =msg.content.replace("!edit","!fws match edit");
  }

  if(msg.content.startsWith("!swap")) {
    msg.content =msg.content.replace("!swap","!fws match swap");
  }

  if(msg.content.startsWith("!arc")) {
    msg.content =msg.content.replace("!arc","!fws match arc");
  }

  if(msg.content.startsWith("!del")) {
    msg.content =msg.content.replace("!del","!fws match del");
  }

if(msg.content.startsWith("!stats")) {
    msg.content =msg.content.replace("!stats","!fws stats");
  }

  if(msg.content.startsWith("!joke")) {
    msg.content =msg.content.replace("!joke","!fws joke");
  }

  //console.log(msg.content);
  //check to be valid and not the bot itself
  if (!msg.content.startsWith(prefix) || msg.author.bot) return;

  //only members and admins can use
  if(!msg.member.roles.some(r=>["Member", "Leader"].includes(r.name)) ) return;



  var args = msg.content.slice(prefix.length).split(' ');
  const command = args.shift().toLowerCase();

  //it starts with the prefix but is something else (anoter word)
	if (!args.length) {
		return msg.channel.send(`Niin ${msg.author}, mitä mielessä? Kokeile vaikka \n!fws help`);
	}
  if(msg.member.roles.some(r=>["Leader","Luutnantti"].includes(r.name))) var isAdmin=1;
  else var isAdmin=0;
  
  if(msg.content=="!fws match" && msg.channel.id==470326786643984384){
    msg.delete(1000);
  }
  
  //console.log(msg.member.displayName);
    http.get(encodeURI(rapi + '?u=' + msg.member.displayName + '&a=' + isAdmin + '&d=' + args), (resp) => {
      
      var data = '';
  
      // A chunk of data has been recieved.
      resp.on('data', (chunk) => {
        data += chunk;
      });
  
      // The whole response has been received. Print out the result.
      resp.on('end', () => {
          data = data;
          if(IsJsonString(data)){
          //console.log(JSON.parse(data).output);
            if(JSON.parse(data).type=="img"){
              msg.channel.send(" ", {files: [JSON.parse(data).output]});
            } else {
              try {
                msg.channel.send(JSON.parse(data).output);
              } catch (error) {
                console.error('MSG failed');
              }
              
            }
          } else msg.channel.send("Sorry, palvelu meni rikki...")
      });

    }).on("error", (err) => {
    console.log("Vihre: " + err.message);
    msg.channel.send('Hupsis, en mä osaa - eli pieleen meni!');
  }); 
});
keepAlive();
console.log("Starting...")
client.login(token);

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
node v10.16.0