<commit_before>var fs = require('fs');
var http = require('http');
var https = require('https');
var uuid = require('node-uuid');
var request = require('request');
var im = require('imagemagick');
var config = require('config');
var urban = require('urban');
var wolfram = null;

if (config.get('wolfram.enabled') == true) {
  wolfram = require('wolfram-alpha').createClient(config.get('wolfram.key'));
}

var squanches = fs.readdirSync('res');

squanches = squanches.map((item) => {
  var filename = item;
  var extension = filename.substring(filename.lastIndexOf('.'));
  var command = '!' + filename.replace(extension, '').toLowerCase();

  return {filename: filename, extension: extension, command: command};
}).filter((item) => {
  if (item.extension == '.wav' || item.extension == '.ogg' || item.extension == '.mp3') {
    return true;
  } else {
    return false;
  }
});

console.log(squanches);

squanches.forEach(function(item, index, array) {
  var json_file = item.filename.replace(item.extension, '.json');
  try {
    fs.accessSync('res/' + json_file, fs.constants.R_OK);
    var data = JSON.parse(fs.readFileSync('res/' + json_file, 'utf8'));
    item = Object.assign(item, data);
    array[index] = item;
  } catch (error) {
    //do nothing, this is fine. This is just a very anti-synchronous implementation of checking file access. 
    //We are using synchronous here because this is loading initial config state
  }
});

console.log(squanches);


var global_audio_connection = null;

var Discord = require('discord.js');

var bot = new Discord.Client();

bot.on('message', function(message) {

  if (message.channel.name === undefined) {
    if (message.attachments.array().length > 0 && message.author.username != bot.user.username) {
      var filename = message.attachments.array()[0].filename;
      filename = filename.replace(/ /g, '_');
      var url = message.attachments.array()[0].url;
      var extension = filename.substring(filename.lastIndexOf('.'));
      var command = '!' + filename.replace(extension, '').toLowerCase();
      
      var tags = message.content;
      if (tags) {
        if (tags.indexOf(',') != -1) {
          tags = tags.split(',');
          tags = tags.map((item) => {
            return item.trim();
          });
        } else {
          tags = [tags];
        }
      } else {
        tags = [];
      }

      if (extension === '.ogg' || extension === '.wav' || extension === '.mp3') {
        if (squanches.map((item) => { return item.filename; }).indexOf(filename) == -1) {
          var file = fs.createWriteStream('res/' + filename);
          https.get(url, (res) => {
            res.pipe(file);
            fs.writeFile('res/' + filename.replace(extension, '.json'), JSON.stringify({tags: tags}), 'utf8', (error) => {
              if (error) {
                message.reply('Had some kind of error saving your tags: ' + JSON.stringify(error));
              }
            });
            squanches.push({filename: filename, extension: extension, command: command, tags: tags});
            console.log(squanches);
            message.reply('Added ' + command);
          }).on('error', (e) => {
            message.reply(e);
          });
        } else {
          message.reply('Okay, well, I\'ve already got a file with that name so go squanch yourself!');
        }
      } else {
        message.reply('Come on dude, that\'s not even an audio squanch. I can only do .ogg, .mp3 or .wav');
      }
    } else if (message.content === '!commands' || message.content == 'commands' || message.content === '!help' || message.content === 'help') {
      // message.reply(getCommands());
      var commands = getCommands();
      while (commands.length > 100) {
        commands_chunk = commands.splice(0, (commands.length >= 100) ? 100 : commands.length);
        message.reply(commands_chunk);
      }
    } else if (message.content.startsWith('!delete ') || message.content.startsWith('delete ')) {
      var command_to_delete = '!' + message.content.replace(/!delete /, '').replace(/delete /, '');
      var commands = squanches.map((item) => {
        return item.command.toLowerCase();
      });

      if (commands.indexOf(command_to_delete) != -1) {
        var audio_file = squanches[commands.indexOf(command_to_delete)].filename;
        var extension = squanches[commands.indexOf(command_to_delete)].extension;
        var json_file = audio_file.replace(extension, '.json');

        var archive_time = (new Date()).getTime();

        fs.rename('res/' + audio_file, 'archive/' + audio_file + '_' + archive_time, (error) => {
          if (error) {
            message.reply('Okay mothersquancher, I tried to do that but there was an error: ' + error);
          } else {
            squanches.splice(commands.indexOf(command_to_delete), 1);
            message.reply('I deleted ' + audio_file + ' for you, you old squanchbag.');

            fs.rename('res/' + json_file, 'archive/' + json_file + '_' + archive_time, (error) => {<commit_msg>Further fixes on PM commands<commit_after>var fs = require('fs');
var http = require('http');
var https = require('https');
var uuid = require('node-uuid');
var request = require('request');
var im = require('imagemagick');
var config = require('config');
var urban = require('urban');
var wolfram = null;

if (config.get('wolfram.enabled') == true) {
  wolfram = require('wolfram-alpha').createClient(config.get('wolfram.key'));
}

var squanches = fs.readdirSync('res');

squanches = squanches.map((item) => {
  var filename = item;
  var extension = filename.substring(filename.lastIndexOf('.'));
  var command = '!' + filename.replace(extension, '').toLowerCase();

  return {filename: filename, extension: extension, command: command};
}).filter((item) => {
  if (item.extension == '.wav' || item.extension == '.ogg' || item.extension == '.mp3') {
    return true;
  } else {
    return false;
  }
});

console.log(squanches);

squanches.forEach(function(item, index, array) {
  var json_file = item.filename.replace(item.extension, '.json');
  try {
    fs.accessSync('res/' + json_file, fs.constants.R_OK);
    var data = JSON.parse(fs.readFileSync('res/' + json_file, 'utf8'));
    item = Object.assign(item, data);
    array[index] = item;
  } catch (error) {
    //do nothing, this is fine. This is just a very anti-synchronous implementation of checking file access. 
    //We are using synchronous here because this is loading initial config state
  }
});

console.log(squanches);


var global_audio_connection = null;

var Discord = require('discord.js');

var bot = new Discord.Client();

bot.on('message', function(message) {

  if (message.channel.name === undefined) {
    if (message.attachments.array().length > 0 && message.author.username != bot.user.username) {
      var filename = message.attachments.array()[0].filename;
      filename = filename.replace(/ /g, '_');
      var url = message.attachments.array()[0].url;
      var extension = filename.substring(filename.lastIndexOf('.'));
      var command = '!' + filename.replace(extension, '').toLowerCase();
      
      var tags = message.content;
      if (tags) {
        if (tags.indexOf(',') != -1) {
          tags = tags.split(',');
          tags = tags.map((item) => {
            return item.trim();
          });
        } else {
          tags = [tags];
        }
      } else {
        tags = [];
      }

      if (extension === '.ogg' || extension === '.wav' || extension === '.mp3') {
        if (squanches.map((item) => { return item.filename; }).indexOf(filename) == -1) {
          var file = fs.createWriteStream('res/' + filename);
          https.get(url, (res) => {
            res.pipe(file);
            fs.writeFile('res/' + filename.replace(extension, '.json'), JSON.stringify({tags: tags}), 'utf8', (error) => {
              if (error) {
                message.reply('Had some kind of error saving your tags: ' + JSON.stringify(error));
              }
            });
            squanches.push({filename: filename, extension: extension, command: command, tags: tags});
            console.log(squanches);
            message.reply('Added ' + command);
          }).on('error', (e) => {
            message.reply(e);
          });
        } else {
          message.reply('Okay, well, I\'ve already got a file with that name so go squanch yourself!');
        }
      } else {
        message.reply('Come on dude, that\'s not even an audio squanch. I can only do .ogg, .mp3 or .wav');
      }
    } else if (message.content === '!commands' || message.content == 'commands' || message.content === '!help' || message.content === 'help') {
      // message.reply(getCommands());
      var commands = getCommands();
      while (commands.length > 100) {
        commands_chunk = commands.splice(0, (commands.length >= 100) ? 100 : commands.length);
        message.reply(commands_chunk);
      }
      message.reply(commands);
    } else if (message.content.startsWith('!delete ') || message.content.startsWith('delete ')) {
      var command_to_delete = '!' + message.content.replace(/!delete /, '').replace(/delete /, '');
      var commands = squanches.map((item) => {
        return item.command.toLowerCase();
      });

      if (commands.indexOf(command_to_delete) != -1) {
        var audio_file = squanches[commands.indexOf(command_to_delete)].filename;
        var extension = squanches[commands.indexOf(command_to_delete)].extension;
        var json_file = audio_file.replace(extension, '.json');

        var archive_time = (new Date()).getTime();

        fs.rename('res/' + audio_file, 'archive/' + audio_file + '_' + archive_time, (error) => {
          if (error) {
            message.reply('Okay mothersquancher, I tried to do that but there was an error: ' + error);
          } else {
            squanches.splice(commands.indexOf(command_to_delete), 1);
            message.reply('I deleted ' + audio_file + ' for you, you old squanchbag.');

            fs.rename('res/' + json_file, 'archive/' + json_file + '_' + archive_time, (error) => {
{'fraction_non_alphanumeric': 0.16808721506442023, 'fraction_numerical': 0.004360753221010902, 'mean_word_length': 2.7978923598042904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '28470', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2970, 'n_words': 709}