<commit_before>const request = require('request-promise-native');
const bot = require('./../bot');
const md = require('to-markdown');
const RichEmbed = require('discord.js').RichEmbed;
const mentionReg = /\[@[a-z]*\]\(\/u\/[a-z]*\)/igm;
const did_from_uname = async function (uname) {
    let res = await request('http://localhost/v0/discord/did_from_uname/' + encodeURIComponent(uname));
    console.log(res);
    res = JSON.parse(res);
    return res.id;
}
module.exports = {
    'ping': function (body) {
        console.log("Recieved a ping event\n" + body);
    },
    'post': async function (body) {
        console.log("Recieved a post event for post no: " + body.post.post_number);
        let toMention =[];
        let embed = new RichEmbed();
        body.post.cooked = md(body.post.cooked)
        embed.setAuthor(body.post.username, body.base_url + body.post.avatar_template.replace('{size}', "100")).setDescription(body.post.cooked)
        if (body.post.reply_to_user && body.post.reply_to_user.username) {
            toMention.push({
                name: body.post.reply_to_user.username,
                message: 'You recieved a reply from ' + body.post.username + ' for post ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
            });
        }
        let mentions = body.post.cooked.match(mentionReg);
        if(mentions && mentions!=null)
        {
            mentions.map(function(m){
                let u = m.match(/@[a-z]*/i)[0].substring(1);
                toMention.push({
                    name: u,
                    message: 'You were mentioned by ' + body.post.username + ' in ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
                });
            });
        }
        // check for private_message
        let topic = await request(body.base_url+ '/t/'+body.post.topic_id+'.json');
        topic = JSON.parse(topic);
        if(topic.archetype && topic.archetype ==='private_message' && topic.details.participants)
        {
            topic.details.participants.map(function(p){
                if(p.username === body.post.username)
                    toMention.push({
                    name: p.username,
                    message: 'You were DMed by ' + body.post.username + ' in ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
                });
            })
        }
        for(let i=0;i<toMention.length;i++)
        {
            let id = await did_from_uname(toMention[i].name);
            bot.users.get(id).send(toMention[i].message, { embed: embed })<commit_msg>add discourse api key<commit_after>const request = require('request-promise-native');
const bot = require('./../bot');
const settings = require('./../settings');
const md = require('to-markdown');
const RichEmbed = require('discord.js').RichEmbed;
const mentionReg = /\[@[a-z]*\]\(\/u\/[a-z]*\)/igm;
const did_from_uname = async function (uname) {
    let res = await request('http://localhost/v0/discord/did_from_uname/' + encodeURIComponent(uname));
    console.log(res);
    res = JSON.parse(res);
    return res.id;
}
module.exports = {
    'ping': function (body) {
        console.log("Recieved a ping event\n" + body);
    },
    'post': async function (body) {
        console.log("Recieved a post event for post no: " + body.post.post_number);
        let toMention =[];
        let embed = new RichEmbed();
        body.post.cooked = md(body.post.cooked)
        embed.setAuthor(body.post.username, body.base_url + body.post.avatar_template.replace('{size}', "100")).setDescription(body.post.cooked)
        if (body.post.reply_to_user && body.post.reply_to_user.username) {
            toMention.push({
                name: body.post.reply_to_user.username,
                message: 'You recieved a reply from ' + body.post.username + ' for post ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
            });
        }
        let mentions = body.post.cooked.match(mentionReg);
        if(mentions && mentions!=null)
        {
            mentions.map(function(m){
                let u = m.match(/@[a-z]*/i)[0].substring(1);
                toMention.push({
                    name: u,
                    message: 'You were mentioned by ' + body.post.username + ' in ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
                });
            });
        }
        // check for private_message
        let topic = await request(body.base_url+ '/t/'+body.post.topic_id+'.json?api_key='+settings.discourse_api+'&api_username=revivebot');
        topic = JSON.parse(topic);
        if(topic.archetype && topic.archetype ==='private_message' && topic.details.participants)
        {
            topic.details.participants.map(function(p){
                if(p.username === body.post.username)
                    toMention.push({
                    name: p.username,
                    message: 'You were DMed by ' + body.post.username + ' in ' + body.base_url + '/t/' + body.post.topic_slug + '/' + body.post.topic_id + '/' + body.post.post_number
                });
            })
        }
        for(let i=0;i<toMention.length;i++)
        {
            let id = await did_from_uname(toMention[i].name);
            bot.users.get(id).send(toMention[i].message, { embed: embed })
{'fraction_non_alphanumeric': 0.16138954224715035, 'fraction_numerical': 0.0025330197213678306, 'mean_word_length': 2.1552511415525113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2822006', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1740, 'n_words': 302}