emote, edit support, attachment support
This commit is contained in:
		
							parent
							
								
									f85ac8138d
								
							
						
					
					
						commit
						3d55bdf31c
					
				
					 1 changed files with 100 additions and 6 deletions
				
			
		
							
								
								
									
										106
									
								
								src/index.js
									
										
									
									
									
								
							
							
						
						
									
										106
									
								
								src/index.js
									
										
									
									
									
								
							| 
						 | 
					@ -12,6 +12,7 @@ stdin.setEncoding("utf8");
 | 
				
			||||||
let currentGuild,
 | 
					let currentGuild,
 | 
				
			||||||
  currentChannel,
 | 
					  currentChannel,
 | 
				
			||||||
  inSendMode = false,
 | 
					  inSendMode = false,
 | 
				
			||||||
 | 
					  inEmoteMode = false,
 | 
				
			||||||
  guildSwitch = false,
 | 
					  guildSwitch = false,
 | 
				
			||||||
  channelSwitch = false,
 | 
					  channelSwitch = false,
 | 
				
			||||||
  nameLength = 2;
 | 
					  nameLength = 2;
 | 
				
			||||||
| 
						 | 
					@ -51,14 +52,16 @@ client.once("ready", function () {
 | 
				
			||||||
  listGuilds();
 | 
					  listGuilds();
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function processMessage({name, content, bot}) {
 | 
					function processMessage({name, content, bot, attachments}) {
 | 
				
			||||||
  if (name.length + 2 > nameLength) nameLength = name.length + 2;
 | 
					  if (name.length + 2 > nameLength) nameLength = name.length + 2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (
 | 
					  if (
 | 
				
			||||||
    (content.startsWith("*") && content.endsWith("*")) ||
 | 
					    (content.startsWith("*") && content.endsWith("*")) ||
 | 
				
			||||||
    (content.startsWith("_") && content.endsWith("_"))
 | 
					    (content.startsWith("_") && content.endsWith("_"))
 | 
				
			||||||
  ) {
 | 
					  ) {
 | 
				
			||||||
    console.log(chalk.bold.green(`<${name} ${content}>`));
 | 
					    console.log(
 | 
				
			||||||
 | 
					      chalk.bold.green(`<${name} ${content.substring(1, content.length - 1)}>`)
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    let nameColor = chalk.bold.cyan;
 | 
					    let nameColor = chalk.bold.cyan;
 | 
				
			||||||
    if (bot) nameColor = chalk.bold.yellow;
 | 
					    if (bot) nameColor = chalk.bold.yellow;
 | 
				
			||||||
| 
						 | 
					@ -69,6 +72,9 @@ function processMessage({name, content, bot}) {
 | 
				
			||||||
        " ".repeat(nameLength - (name.length + 2)) +
 | 
					        " ".repeat(nameLength - (name.length + 2)) +
 | 
				
			||||||
        chalk.reset(" " + content)
 | 
					        chalk.reset(" " + content)
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					    for (const attachment of attachments) {
 | 
				
			||||||
 | 
					      console.log(chalk.bold.yellow(`<attachment: ${attachment.url} >`));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -76,11 +82,13 @@ function processQueue() {
 | 
				
			||||||
  for (const msg of messageQueue) {
 | 
					  for (const msg of messageQueue) {
 | 
				
			||||||
    if (msg.content.indexOf("\n") > -1) {
 | 
					    if (msg.content.indexOf("\n") > -1) {
 | 
				
			||||||
      const lines = msg.content.split("\n");
 | 
					      const lines = msg.content.split("\n");
 | 
				
			||||||
      for (const line of lines) {
 | 
					      for (const index in lines) {
 | 
				
			||||||
 | 
					        const line = lines[index];
 | 
				
			||||||
        processMessage({
 | 
					        processMessage({
 | 
				
			||||||
          name: msg.author.username,
 | 
					          name: msg.author.username,
 | 
				
			||||||
          bot: msg.author.bot,
 | 
					          bot: msg.author.bot,
 | 
				
			||||||
          content: line,
 | 
					          content: line,
 | 
				
			||||||
 | 
					          attachments: index == lines.length - 1 ? msg.attachments : null,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
| 
						 | 
					@ -88,25 +96,30 @@ function processQueue() {
 | 
				
			||||||
        name: msg.author.username,
 | 
					        name: msg.author.username,
 | 
				
			||||||
        bot: msg.author.bot,
 | 
					        bot: msg.author.bot,
 | 
				
			||||||
        content: msg.content,
 | 
					        content: msg.content,
 | 
				
			||||||
 | 
					        attachments: msg.attachments,
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  messageQueue.splice(0, messageQueue.length);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
client.on("messageCreate", function (msg) {
 | 
					client.on("messageCreate", function (msg) {
 | 
				
			||||||
  if (msg.author.id === client.user.id) return;
 | 
					  if (msg.author.id === client.user.id) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (msg.channel.id == currentChannel) {
 | 
					  if (msg.channel.id == currentChannel) {
 | 
				
			||||||
    if (inSendMode) {
 | 
					    if (inSendMode || inEmoteMode) {
 | 
				
			||||||
      messageQueue.push(msg);
 | 
					      messageQueue.push(msg);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (msg.content.indexOf("\n") > -1) {
 | 
					      if (msg.content.indexOf("\n") > -1) {
 | 
				
			||||||
        const lines = msg.content.split("\n");
 | 
					        const lines = msg.content.split("\n");
 | 
				
			||||||
        for (const line of lines) {
 | 
					        for (const index in lines) {
 | 
				
			||||||
 | 
					          const line = lines[index];
 | 
				
			||||||
          processMessage({
 | 
					          processMessage({
 | 
				
			||||||
            name: msg.author.username,
 | 
					            name: msg.author.username,
 | 
				
			||||||
            bot: msg.author.bot,
 | 
					            bot: msg.author.bot,
 | 
				
			||||||
            content: line,
 | 
					            content: line,
 | 
				
			||||||
 | 
					            attachments: index == lines.length - 1 ? msg.attachments : null,
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
| 
						 | 
					@ -114,6 +127,38 @@ client.on("messageCreate", function (msg) {
 | 
				
			||||||
          name: msg.author.username,
 | 
					          name: msg.author.username,
 | 
				
			||||||
          bot: msg.author.bot,
 | 
					          bot: msg.author.bot,
 | 
				
			||||||
          content: msg.content,
 | 
					          content: msg.content,
 | 
				
			||||||
 | 
					          attachments: msg.attachments,
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					client.on("messageUpdate", function (msg, old) {
 | 
				
			||||||
 | 
					  if (msg.author.id === client.user.id) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (msg.channel.id == currentChannel) {
 | 
				
			||||||
 | 
					    if (msg.content == old.content) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (inSendMode || inEmoteMode) {
 | 
				
			||||||
 | 
					      messageQueue.push(msg);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      if (msg.content.indexOf("\n") > -1) {
 | 
				
			||||||
 | 
					        const lines = msg.content.split("\n");
 | 
				
			||||||
 | 
					        for (const index in lines) {
 | 
				
			||||||
 | 
					          const line = lines[index];
 | 
				
			||||||
 | 
					          processMessage({
 | 
				
			||||||
 | 
					            name: msg.author.username,
 | 
				
			||||||
 | 
					            bot: msg.author.bot,
 | 
				
			||||||
 | 
					            content: line + index == lines.length - 1 ? " (edited)" : null,
 | 
				
			||||||
 | 
					            attachments: index == lines.length - 1 ? msg.attachments : null,
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        processMessage({
 | 
				
			||||||
 | 
					          name: msg.author.username,
 | 
				
			||||||
 | 
					          bot: msg.author.bot,
 | 
				
			||||||
 | 
					          content: msg.content + " (edited)",
 | 
				
			||||||
 | 
					          attachments: msg.attachments,
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -144,6 +189,7 @@ async function sendMessage() {
 | 
				
			||||||
    stdout.write("<no message sent>\n");
 | 
					    stdout.write("<no message sent>\n");
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 | 
					      stdout.write("\n");
 | 
				
			||||||
      await client.createMessage(currentChannel, toSend);
 | 
					      await client.createMessage(currentChannel, toSend);
 | 
				
			||||||
    } catch (err) {
 | 
					    } catch (err) {
 | 
				
			||||||
      console.log("<failed to send message: " + err.message + ">");
 | 
					      console.log("<failed to send message: " + err.message + ">");
 | 
				
			||||||
| 
						 | 
					@ -335,6 +381,7 @@ function switchChannel() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const guild = client.guilds.get(currentGuild);
 | 
					  const guild = client.guilds.get(currentGuild);
 | 
				
			||||||
  const channels = [...guild.channels.values()].filter((c) => c.type == 0);
 | 
					  const channels = [...guild.channels.values()].filter((c) => c.type == 0);
 | 
				
			||||||
 | 
					  channels.sort((a, b) => a.position - b.position);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  for (const channel of channels) {
 | 
					  for (const channel of channels) {
 | 
				
			||||||
    if (channel.name.toLowerCase().indexOf(targetChannel.toLowerCase()) > -1) {
 | 
					    if (channel.name.toLowerCase().indexOf(targetChannel.toLowerCase()) > -1) {
 | 
				
			||||||
| 
						 | 
					@ -354,6 +401,29 @@ function switchChannel() {
 | 
				
			||||||
  channelSwitch = false;
 | 
					  channelSwitch = false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function startEmote() {
 | 
				
			||||||
 | 
					  toSend = "";
 | 
				
			||||||
 | 
					  inEmoteMode = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  stdout.write(":emote> ");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function sendEmote() {
 | 
				
			||||||
 | 
					  toSend = toSend.trim();
 | 
				
			||||||
 | 
					  if (toSend === "") {
 | 
				
			||||||
 | 
					    console.log("<no message sent>");
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      await client.createMessage(currentChannel, "*" + toSend + "*");
 | 
				
			||||||
 | 
					      console.log(`<${client.user.username} ${toSend}>`);
 | 
				
			||||||
 | 
					    } catch (err) {
 | 
				
			||||||
 | 
					      console.log("<failed to send message: " + err.message + ">");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  inEmoteMode = false;
 | 
				
			||||||
 | 
					  processQueue();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
stdin.on("data", function (key) {
 | 
					stdin.on("data", function (key) {
 | 
				
			||||||
  if (guildSwitch) {
 | 
					  if (guildSwitch) {
 | 
				
			||||||
    if (key === "\r") {
 | 
					    if (key === "\r") {
 | 
				
			||||||
| 
						 | 
					@ -391,7 +461,6 @@ stdin.on("data", function (key) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else if (inSendMode) {
 | 
					  } else if (inSendMode) {
 | 
				
			||||||
    if (key === "\r") {
 | 
					    if (key === "\r") {
 | 
				
			||||||
      console.log("");
 | 
					 | 
				
			||||||
      sendMessage();
 | 
					      sendMessage();
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      if (key === "\b") {
 | 
					      if (key === "\b") {
 | 
				
			||||||
| 
						 | 
					@ -406,6 +475,23 @@ stdin.on("data", function (key) {
 | 
				
			||||||
        toSend += key;
 | 
					        toSend += key;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  } else if (inEmoteMode) {
 | 
				
			||||||
 | 
					    if (key === "\r") {
 | 
				
			||||||
 | 
					      console.log("");
 | 
				
			||||||
 | 
					      sendEmote();
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      if (key === "\b") {
 | 
				
			||||||
 | 
					        if (toSend.length > 0) {
 | 
				
			||||||
 | 
					          stdout.moveCursor(-1);
 | 
				
			||||||
 | 
					          stdout.write(" ");
 | 
				
			||||||
 | 
					          stdout.moveCursor(-1);
 | 
				
			||||||
 | 
					          toSend = toSend.substring(0, toSend.length - 1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        stdout.write(key);
 | 
				
			||||||
 | 
					        toSend += key;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    switch (key) {
 | 
					    switch (key) {
 | 
				
			||||||
      case "\u0003":
 | 
					      case "\u0003":
 | 
				
			||||||
| 
						 | 
					@ -449,6 +535,14 @@ stdin.on("data", function (key) {
 | 
				
			||||||
        listUsers();
 | 
					        listUsers();
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      case "e": {
 | 
				
			||||||
 | 
					        if (currentChannel == null) {
 | 
				
			||||||
 | 
					          console.log("<not in a channel>");
 | 
				
			||||||
 | 
					          break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        startEmote();
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      case " ":
 | 
					      case " ":
 | 
				
			||||||
      case "\r":
 | 
					      case "\r":
 | 
				
			||||||
      default: {
 | 
					      default: {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue