Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't truncate text #28

Open
nomis opened this issue Jan 20, 2019 · 2 comments · May be fixed by #149
Open

Don't truncate text #28

nomis opened this issue Jan 20, 2019 · 2 comments · May be fixed by #149
Labels

Comments

@nomis
Copy link

nomis commented Jan 20, 2019

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

@PutoML
Copy link

PutoML commented Jul 16, 2020

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

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

@qaisjp
Copy link
Owner

qaisjp commented Aug 28, 2020

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
+				}()
+			}
 		}
 	}
 }

@codeurimpulsif codeurimpulsif linked a pull request Jun 28, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants