-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (91 loc) · 2.91 KB
/
server.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
const http = require('http');
const querystring = require('querystring');
const Discord = require('discord.js');
const client = new Discord.Client();
http.createServer(function(req, res){
if (req.method == 'POST'){
var data = "";
req.on('data', function(chunk){
data += chunk;
});
req.on('end', function(){
if(!data){
res.end("No post data");
return;
}
var dataObject = querystring.parse(data);
console.log("post:" + dataObject.type);
if(dataObject.type == "wake"){
console.log("Woke up in post");
res.end();
return;
}
res.end();
});
}
else if (req.method == 'GET'){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Discord Bot is active now\n');
}
}).listen(3000);
client.on('ready', message =>{
console.log(Discord.version);
global.announcePost = null;
client.user.setPresence({ game: { name: 'チーム分け' } });
});
client.on('message', message =>{
if (message.author.id == client.user.id || message.author.bot){
return;
}
if(message.content.match(/コアタイム|コアタイム/)
&& message.content.match(/告知|告知/)) {
sendMsg(message.channel.id, "新入生コアタイムだョ! 全員集合!");
return;
}
});
client.on('message', message => {
if (message.author.id == client.user.id || message.author.bot
&& message.content.match("新入生コアタイムだョ! 全員集合!")) {
global.announcePost = message;
const filter = (reaction ) => reaction.emoji.name === '🆗';
global.rc = global.announcePost.createReactionCollector(filter, {time : 86400000});
global.rc.on('collect', r => console.log(r.users));
global.rc.on('end', collected => {
const userIds = [ ...collected.get('🆗').users.keys() ];
console.log(userIds);
var post = "発表します!";
for (var i=0;i<userIds.length;i++) {
if (i%3 == 0) {
post += `\n\n カラ館 ${i/3 + 1}号室`;
}
post += " <@" + userIds[i] + "> ";
}
sendMsg(message.channel.id, post);
})
return ;
}
});
client.on('message', message => {
if (message.author.id == client.user.id || message.author.bot) {
return ;
}
if (message.content.match(/チーム分け|チーム分け/)) {
// get member who reactioned to announce post id
global.rc.stop();
}
});
if(process.env.DISCORD_BOT_TOKEN == undefined){
console.log('DISCORD_BOT_TOKENが設定されていません。');
process.exit(0);
}
client.login( process.env.DISCORD_BOT_TOKEN );
function sendReply(message, text){
message.reply(text)
.then(console.log("リプライ送信: " + text))
.catch(console.error);
}
function sendMsg(channelId, text, option={}){
client.channels.get(channelId).send(text, option)
.then(console.log("メッセージ送信: " + text + JSON.stringify(option)))
.catch(console.error);
}