const config = require('../config');
const Eris = require('eris');
const threadUtils = require('../threadUtils');
const transliterate = require("transliteration");

module.exports = bot => {
  const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args);

  addInboxServerCommand('move', async (msg, args, thread) => {
    if (! config.allowMove) return;

    if (! thread) return;

    const searchStr = args[0];
    if (! searchStr || searchStr.trim() === '') return;

    const normalizedSearchStr = transliterate.slugify(searchStr);

    const categories = msg.channel.guild.channels.filter(c => {
      // Filter to categories that are not the thread's current parent category
      return (c instanceof Eris.CategoryChannel) && (c.id !== msg.channel.parentID);
    });

    if (categories.length === 0) return;

    // See if any category name contains a part of the search string
    const containsRankings = categories.map(cat => {
      const normalizedCatName = transliterate.slugify(cat.name);

      let i;
      for (i = 1; i < normalizedSearchStr.length; i++) {
        if (! normalizedCatName.includes(normalizedSearchStr.slice(0, i))) {
          i--;
          break;
        }
      }

      return [cat, i];
    });

    // Sort by best match
    containsRankings.sort((a, b) => {
      return a[1] > b[1] ? -1 : 1;
    });

    if (containsRankings[0][1] === 0) {
      thread.postSystemMessage('No matching category');
      return;
    }

    const targetCategory = containsRankings[0][0];

    await bot.editChannel(thread.channel_id, {
      parentID: targetCategory.id
    });

    thread.postSystemMessage(`Thread moved to ${targetCategory.name.toUpperCase()}`);
  });
};

@@ -1040,24 +1040,28 @@
    let i
+ = 0
 ;%0A      
 for (i =
@@ -1056,56 +1056,10 @@
     
-for (i = 1; i %3C normalizedSearchStr.length; i++)
+do
  %7B%0A 
@@ -1132,13 +1132,22 @@
 0, i
-))) %7B
+ + 1))) break;
 %0A   
@@ -1155,21 +1155,19 @@
     
-  i--
+i++
 ;%0A      
     
@@ -1166,36 +1166,49 @@
     
-    break;%0A        %7D%0A      %7D
+%7D while (i %3C normalizedSearchStr.length);
 %0A%0A  

fix !move ignoring the last letter of the category name