Advertisement
revelcw

Convert Minecraft color codes to discord ANSI escaping

Jan 23rd, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. const formattingTable = {
  2. l: '1',
  3. n: '4',
  4. k: '0',
  5. m: '0',
  6. o: '0',
  7. r: '0',
  8. };
  9.  
  10. const colorTable = {
  11. 0: '30',
  12. 1: '34',
  13. 2: '32',
  14. 3: '36',
  15. 4: '31',
  16. 5: '35',
  17. 6: '33',
  18. 7: '0',
  19. 8: '30',
  20. 9: '34',
  21. a: '32',
  22. b: '36',
  23. c: '31',
  24. d: '35',
  25. e: '33',
  26. f: '37',
  27. };
  28.  
  29. const motdToAnsi = (rawMotd) => {
  30. let currentColor = '37';
  31. let currentFormat = '0';
  32. const prefixedMotd = `§r${rawMotd}`; // Change "§" to "&" or whatever prefix the Minecraft color code is.
  33. const convertedMOTD = prefixedMotd.replaceAll(/§(.)/g, (arg) => { // Change this squiggly too.
  34. const [, code] = [...arg];
  35. // console.log(code)
  36. const newColor = colorTable[code];
  37. if (newColor) {
  38. currentColor = newColor;
  39. }
  40. const newFormat = formattingTable[code];
  41. if (newFormat) {
  42. currentFormat = newFormat;
  43. }
  44.  
  45. return `\u001b[${currentFormat};0;${currentColor}m`;
  46. });
  47. return convertedMOTD;
  48. };
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement