Skip to content

Instantly share code, notes, and snippets.

@tonussi
Last active December 10, 2018 19:14
Show Gist options
  • Save tonussi/4bd0740ddfafae816a4b059627e1afba to your computer and use it in GitHub Desktop.
Save tonussi/4bd0740ddfafae816a4b059627e1afba to your computer and use it in GitHub Desktop.
hltv.js
import Discord from 'discord.js';
const HLTV = require('hltv-api');
import moment from 'moment';
import _ from 'lodash';
export const botGetHelp = msg => {
try {
var help = new Discord.RichEmbed();
help.setColor('#fff');
help.addField('Help', "!help Mostra uma ajuda para comandar o bot.");
help.addField('News', "!news Mostra as notícias recentes da HLTV.");
help.addField('Resultados de Partidas', "!results Mostra alguns resultados recentes de partidas.");
help.addField('Statísticas', "!stats <matchId> Mostra as estatísticas total dos mapas de um confronto.");
help.addField('MatchId', "O campo matchId pode ser obtido quando você usar !results.");
msg.channel.send(help);
} catch (err) {
console.log(err);
}
}
export const botGetNews = msg => {
try {
HLTV.getNews(function (news) {
var matchEmbedList = [];
for (let index = 0; index < news.length; index++) {
const element = news[index];
matchEmbedList.push(
new Discord.RichEmbed()
.setColor('#c3073f')
.addField('Date', moment(new Date(element.date)).locale("pt-BR").format('LL'))
.addField('Title', element.title)
.addField('Description', element.description)
.addField('Link', element.link)
);
}
for (let index = 0; index < matchEmbedList.length; index++) {
const element = matchEmbedList[index];
msg.channel.send(element);
if (index === 4) {
break;
}
}
});
} catch (err) {
console.log(err);
}
};
export const botGetResults = msg => {
try {
HLTV.getResults(function (results) {
var matchEmbedList = [];
for (let index = 0; index < results.length; index++) {
const element = results[index];
matchEmbedList.push(
new Discord.RichEmbed()
.setColor('#c3073f')
.addField('Event Name', element.event)
.addField('Maps', element.maps)
.addField('Result', element.team1.name + " " + element.team1.result + ":" + element.team2.result + " " + element.team2.name)
.addField('Link', "https://www.hltv.org" + element.matchId)
.addField('matchId', element.matchId)
);
}
for (let index = 0; index < matchEmbedList.length; index++) {
const element = matchEmbedList[index];
msg.channel.send(element);
if (index === 4) {
break;
}
}
});
} catch (err) {
console.log(err);
}
};
export const botGetMatches = params => {
var msg = params.msg;
var matchId = params.matchId;
try {
HLTV.getMatches(matchId, function (stats) {
var playersFirstTeam = new Discord.RichEmbed();
playersFirstTeam.setColor('#c3073f')
var playersSecondTeam = new Discord.RichEmbed();
playersSecondTeam.setColor('#f12311')
for (let index = 0; index < 5; index++) {
const element = stats[index];
playersFirstTeam.addField(element.playerName, "(KDR: " + element.kills + "/" + element.deaths + ")");
}
for (let index = 5; index < 10; index++) {
const element = stats[index];
playersSecondTeam.addField(element.playerName, "(KDR: " + element.kills + "/" + element.deaths + ")");
}
msg.channel.send(playersFirstTeam);
msg.channel.send(playersSecondTeam);
});
} catch (err) {
console.log(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment