-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (94 loc) · 3.26 KB
/
app.js
File metadata and controls
97 lines (94 loc) · 3.26 KB
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
const Discord = require('discord.js');
const DotEnv = require('dotenv')
DotEnv.config()
const client = new Discord.Client();
const config = require('./config.json')
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity("!!deploy", {"type": "PLAYING"});
});
client.on('message', msg => {
if (msg.author.id === client.user.id) { return }
if (msg.content.substr(0, 2) === '!!') {
// process as a command
// msg.channel.startTyping()
let commandRow = msg.content.substr(2, msg.length)
let commandArgs = commandRow.split(' ')
let canDeploy = false
if (!msg.author.bot) {
canDeploy = msg.member.roles.cache.array().filter(role => role.name === 'can-deploy').length !== 0
} else {
canDeploy = true
}
switch (commandArgs[0]) {
case 'ping':
msg.reply('Pong!')
break;
case 'list':
if (!canDeploy) {
msg.reply("Nop, you can't see that")
} else {
let lines = config.applications.map(item => {
return item.name
})
lines = lines.join('\n')
msg.channel.send('```' + lines + '```')
}
break;
case 'view':
if (!canDeploy) {
msg.reply("Nop, you can't see that")
} else {
let applications = config.applications.filter(app => app.name === commandArgs[1])
if (applications.length === 0) {
msg.reply(`I can't find any application with the name "${commandArgs[1]}"`)
} else {
let application = applications[0]
let embed = new Discord.RichEmbed({
title: application.name,
fields: [
{
name: "Path",
value: application.path
},
{
name: "Commandes",
value: application.scripts.deploy.join(' && ')
}
]
});
msg.channel.send(embed)
}
}
break;
case 'deploy':
// user need to be admin
if (!canDeploy) {
msg.reply("Oh sorry you can't deploy because you are not very wealthy unlike the others...")
} else {
let applications = config.applications.filter(app => app.name === commandArgs[1])
if (applications.length === 0) {
msg.reply(`I can't find any application with the name "${commandArgs[1]}"`)
} else {
msg.reply('Ok, I will deploy that for you...')
let application = applications[0]
application.scripts.deploy.unshift('cd ' + application.path)
let commandToExec = application.scripts.deploy.join(' && ')
let exec = require('node-exec-promise').exec;
exec(commandToExec).then(function(out) {
msg.reply(`\`\`\` ${out.stdout} \`\`\``)
if (out.stderr != "" && out.stderr != " ") {
msg.reply(`\`\`\` ${out.stderr} \`\`\``)
}
msg.reply("It's deploy!")
}, function(err) {
console.error(err);
msg.reply(`\`\`\` ${err} \`\`\``)
});
}
}
break;
}
}
});
client.login(process.env.DISCORD_TOKEN);