Last active
April 30, 2024 22:31
-
-
Save aosteraas/978ecb6d534bc1c4592bbd43bf112adc to your computer and use it in GitHub Desktop.
TeamCity Discord Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const Discord = require('discord.io'); | |
const teamcity = require('teamcity'); | |
const auth = require('../auth.json'); | |
// Create Discord Bot | |
const bot = new Discord.Client({ | |
token: auth.token, | |
autorun: true | |
}); | |
// define some actions the bot will respond to | |
const actions = { | |
ping: 'replies with pong', | |
buildStatus: '!buildStatus {id} returns build status by Build ID' | |
}; | |
// create TeamCity client connection | |
const tc = teamcity.create({ | |
url: 'https://your-tc-url.com', | |
username: auth.tcuser, | |
password: auth.tcpass | |
}); | |
const getHelpMessage = () = { | |
let msg = `**TCBot Usage**\n`; | |
for (let key in actions) { | |
msg += `\`${key}\`: ${actions[key]}\n`; | |
} | |
return msg; | |
} | |
// Send error message to console | |
bot.on('disconnect', (errMsg, code) => { | |
console.log(errMsg, code); | |
}); | |
bot.on('ready', evt => { | |
console.info('Connected'); | |
console.info('Logged in as: '); | |
console.info(bot.username + ' - (' + bot.id + ')'); | |
}); | |
bot.on('message', (user, userID, channelID, message, evt) => { | |
// Configure the bot to listen for commands starting with ! | |
if (message.substring(0, 1) == '!') { | |
let args = message.substring(1).split(' '); | |
const cmd = args[0]; | |
args = args.splice(1); | |
switch(cmd) { | |
case 'help': | |
bot.sendMessage({ | |
to: channelID, | |
message: getHelpMessage() | |
}); | |
break; | |
case 'ping': | |
bot.sendMessage({ | |
to: channelID, | |
message: 'Pong!' | |
}); | |
break; | |
case 'buildStatus': | |
let buildId = parseInt(args[0].replace(/[^\d.]/g, '')); | |
tc.builds.get({ id: buildId }).then(res => { | |
bot.sendMessage({ | |
to: channelID, | |
message: '', | |
embed: { | |
color: 6826080, | |
title: `Build Status ${res.status}`, | |
fields: [ | |
{ | |
name: 'Build Status', | |
value: `${res.status}`, | |
inline: true | |
}, | |
{ | |
name: 'Build State', | |
value: `${res.state}`, | |
inline: true | |
}, | |
{ | |
name: 'Status Text', | |
value: `\`\`\`${build.statusText}\`\`\``, | |
inline: false | |
} | |
] | |
} | |
}); | |
}); | |
break; | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"token": "YOUR-BOTS-TOKEN", | |
"tcuser": "a-tc-user", | |
"tcpass": "pass-for-user" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, what does this line:
const teamcity = require('teamcity');
requires?Is it a node module?