go-discord-irc icon indicating copy to clipboard operation
go-discord-irc copied to clipboard

Don't truncate text

Open nomis opened this issue 6 years ago • 2 comments

If text is too long for one line on IRC, split it into multiple lines

nomis avatar Jan 20 '19 14:01 nomis

Here’s a patch to do this if anybody’s interested

https://frieza.hoshinet.org/godiscordirc-splitlines.diff

PutoML avatar Jul 16 '20 13:07 PutoML

Thank you very much!

--- go-discord-irc/bridge/irc_manager.go	Thu Jul 16 14:03:07 2020
+++ /home/puto/go-discord-irc/bridge/irc_manager.go	Thu Jul 16 13:56:36 2020
@@ -5,6 +5,7 @@
 	"regexp"
 	"strings"
 	"time"
+	"math"
 
 	"github.com/mozillazg/go-unidecode"
 	"github.com/pkg/errors"
@@ -298,6 +291,25 @@
 	return newNick
 }
 
+
+func splitByWidthMake(str string, size int) []string {
+    strLength := len(str)
+    splitedLength := int(math.Ceil(float64(strLength) / float64(size)))
+    splited := make([]string, splitedLength)
+    var start, stop int
+    for i := 0; i < splitedLength; i += 1 {
+        start = i * size
+        stop = start + size
+        if stop > strLength {
+            stop = strLength
+        }
+        splited[i] = str[start : stop]
+    }
+    return splited
+}
+
+
 // SendMessage sends a broken down Discord Message to a particular IRC channel.
 func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {
 	con, ok := m.ircConnections[msg.Author.ID]
@@ -310,12 +322,15 @@
 	if !ok {
 		length := len(msg.Author.Username)
 		for _, line := range strings.Split(content, "\n") {
-			m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
-				"<%s#%s> %s",
-				msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
-				msg.Author.Discriminator,
-				line,
-			))
+			chunks := splitByWidthMake(line, 400)
+			for _, s := range chunks {
+				m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
+					"<%s#%s> %s",
+					msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
+					msg.Author.Discriminator,
+					s,
+				))
+			}
 		}
 		return
 	}
@@ -326,20 +341,23 @@
 	}
 
 	for _, line := range strings.Split(content, "\n") {
-		ircMessage := IRCMessage{
-			IRCChannel: channel,
-			Message:    line,
-			IsAction:   msg.IsAction,
-		}
+               chunks := splitByWidthMake(line, 400)
+               for _, s := range chunks {
+			ircMessage := IRCMessage{
+				IRCChannel: channel,
+				Message:    s,
+				IsAction:   msg.IsAction,
+			}
 
-		select {
-		// Try to send the message immediately
-		case con.messages <- ircMessage:
-		// If it can't after 5ms, do it in a separate goroutine
-		case <-time.After(time.Millisecond * 5):
-			go func() {
-				con.messages <- ircMessage
-			}()
+			select {
+			// Try to send the message immediately
+			case con.messages <- ircMessage:
+			// If it can't after 5ms, do it in a separate goroutine
+			case <-time.After(time.Millisecond * 5):
+				go func() {
+					con.messages <- ircMessage
+				}()
+			}
 		}
 	}
 }

qaisjp avatar Aug 28 '20 22:08 qaisjp