Fix linting

This commit is contained in:
Wim 2024-05-24 00:23:50 +02:00
parent 6edd5de3b7
commit 815d8b804f
No known key found for this signature in database
4 changed files with 22 additions and 22 deletions

View File

@ -331,7 +331,7 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
// Edit message // Edit message
if msg.ID != "" { if msg.ID != "" {
// Exploit that a discord message ID is actually just a large number, and we encode a list of IDs by separating them with ";". // Exploit that a discord message ID is actually just a large number, and we encode a list of IDs by separating them with ";".
var msgIds = strings.Split(msg.ID, ";") msgIds := strings.Split(msg.ID, ";")
msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), len(msgIds)) msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), len(msgIds))
for len(msgParts) < len(msgIds) { for len(msgParts) < len(msgIds) {
msgParts = append(msgParts, "((obsoleted by edit))") msgParts = append(msgParts, "((obsoleted by edit))")
@ -349,7 +349,7 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
} }
msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), b.GetInt("MessageSplitMaxCount")) msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), b.GetInt("MessageSplitMaxCount"))
var msgIds = []string{} msgIds := []string{}
for _, msgPart := range msgParts { for _, msgPart := range msgParts {
m := discordgo.MessageSend{ m := discordgo.MessageSend{

View File

@ -45,7 +45,7 @@ func (b *Bdiscord) maybeGetLocalAvatar(msg *config.Message) string {
func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (string, error) { func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (string, error) {
msgParts := helper.ClipOrSplitMessage(msg.Text, MessageLength, b.GetString("MessageClipped"), b.GetInt("MessageSplitMaxCount")) msgParts := helper.ClipOrSplitMessage(msg.Text, MessageLength, b.GetString("MessageClipped"), b.GetInt("MessageSplitMaxCount"))
var msgIds = []string{} msgIds := []string{}
for _, msgPart := range msgParts { for _, msgPart := range msgParts {
res, err := b.transmitter.Send( res, err := b.transmitter.Send(
channelID, channelID,
@ -68,7 +68,7 @@ func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (s
func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) error { func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) error {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo) //nolint:forcetypeassert
file := discordgo.File{ file := discordgo.File{
Name: fi.Name, Name: fi.Name,
ContentType: "", ContentType: "",
@ -101,8 +101,8 @@ func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) e
// Returns messageID and error. // Returns messageID and error.
func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (string, error) { func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (string, error) {
var ( var (
res string res string
err error err error
) )
// If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this) // If avatar is unset, mutate the message to include the local avatar (but only if settings say we should do this)
@ -143,29 +143,29 @@ func (b *Bdiscord) handleEventWebhook(msg *config.Message, channelID string) (st
if msg.ID != "" { if msg.ID != "" {
// Exploit that a discord message ID is actually just a large number, and we encode a list of IDs by separating them with ";". // Exploit that a discord message ID is actually just a large number, and we encode a list of IDs by separating them with ";".
var msgIds = strings.Split(msg.ID, ";") msgIds := strings.Split(msg.ID, ";")
msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), len(msgIds)) msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), len(msgIds))
for len(msgParts) < len(msgIds) { for len(msgParts) < len(msgIds) {
msgParts = append(msgParts, "((obsoleted by edit))") msgParts = append(msgParts, "((obsoleted by edit))")
} }
b.Log.Debugf("Editing webhook message") b.Log.Debugf("Editing webhook message")
var edit_err error = nil var editErr error = nil
for i := range msgParts { for i := range msgParts {
// In case of split-messages where some parts remain the same (i.e. only a typo-fix in a huge message), this causes some noop-updates. // In case of split-messages where some parts remain the same (i.e. only a typo-fix in a huge message), this causes some noop-updates.
// TODO: Optimize away noop-updates of un-edited messages // TODO: Optimize away noop-updates of un-edited messages
edit_err = b.transmitter.Edit(channelID, msgIds[i], &discordgo.WebhookParams{ editErr = b.transmitter.Edit(channelID, msgIds[i], &discordgo.WebhookParams{
Content: msgParts[i], Content: msgParts[i],
Username: msg.Username, Username: msg.Username,
AllowedMentions: b.getAllowedMentions(), AllowedMentions: b.getAllowedMentions(),
}) })
if edit_err != nil { if editErr != nil {
break break
} }
} }
if edit_err == nil { if editErr == nil {
return msg.ID, nil return msg.ID, nil
} }
b.Log.Errorf("Could not edit webhook message(s): %s; sending as new message(s) instead", edit_err) b.Log.Errorf("Could not edit webhook message(s): %s; sending as new message(s) instead", editErr)
} }
b.Log.Debugf("Processing webhook sending for message %#v", msg) b.Log.Debugf("Processing webhook sending for message %#v", msg)

View File

@ -231,17 +231,17 @@ func ClipMessage(text string, length int, clippingMessage string) string {
func ClipOrSplitMessage(text string, length int, clippingMessage string, splitMax int) []string { func ClipOrSplitMessage(text string, length int, clippingMessage string, splitMax int) []string {
var msgParts []string var msgParts []string
var remainingText = text remainingText := text
// Invariant of this splitting loop: No text is lost (msgParts+remainingText is the original text), // Invariant of this splitting loop: No text is lost (msgParts+remainingText is the original text),
// and all parts is guaranteed to satisfy the length requirement. // and all parts is guaranteed to satisfy the length requirement.
for len(msgParts) < splitMax - 1 && len(remainingText) > length { for len(msgParts) < splitMax-1 && len(remainingText) > length {
// Decision: The text needs to be split (again). // Decision: The text needs to be split (again).
var chunk string var chunk string
var wasted = 0 wasted := 0
// The longest UTF-8 encoding of a valid rune is 4 bytes (0xF4 0x8F 0xBF 0xBF, encoding U+10FFFF), // The longest UTF-8 encoding of a valid rune is 4 bytes (0xF4 0x8F 0xBF 0xBF, encoding U+10FFFF),
// so we should never need to waste 4 or more bytes at a time. // so we should never need to waste 4 or more bytes at a time.
for wasted < 4 && wasted < length { for wasted < 4 && wasted < length {
chunk = remainingText[:length - wasted] chunk = remainingText[:length-wasted]
if r, _ := utf8.DecodeLastRuneInString(chunk); r == utf8.RuneError { if r, _ := utf8.DecodeLastRuneInString(chunk); r == utf8.RuneError {
wasted += 1 wasted += 1
} else { } else {

View File

@ -169,7 +169,7 @@ var clippingOrSplittingTestCases = map[string]struct {
clipSplitLength: 50, clipSplitLength: 50,
clippingMessage: "?!?!", clippingMessage: "?!?!",
splitMax: 10, splitMax: 10,
expectedOutput: []string{ expectedOutput: []string{
"Lorem ipsum dolor sit amet, consectetur adipiscing", "Lorem ipsum dolor sit amet, consectetur adipiscing",
" elit, sed do eiusmod tempor incididunt ut labore ", " elit, sed do eiusmod tempor incididunt ut labore ",
"et dolore magna aliqua.", "et dolore magna aliqua.",
@ -180,7 +180,7 @@ var clippingOrSplittingTestCases = map[string]struct {
clipSplitLength: 50, clipSplitLength: 50,
clippingMessage: "?!?!", clippingMessage: "?!?!",
splitMax: 3, splitMax: 3,
expectedOutput: []string{ expectedOutput: []string{
"Lorem ipsum dolor sit amet, consectetur adipiscing", "Lorem ipsum dolor sit amet, consectetur adipiscing",
" elit, sed do eiusmod tempor incididunt ut labore ", " elit, sed do eiusmod tempor incididunt ut labore ",
"et dolore magna aliqua.", "et dolore magna aliqua.",
@ -191,7 +191,7 @@ var clippingOrSplittingTestCases = map[string]struct {
clipSplitLength: 50, clipSplitLength: 50,
clippingMessage: "?!?!", clippingMessage: "?!?!",
splitMax: 2, splitMax: 2,
expectedOutput: []string{ expectedOutput: []string{
"Lorem ipsum dolor sit amet, consectetur adipiscing", "Lorem ipsum dolor sit amet, consectetur adipiscing",
" elit, sed do eiusmod tempor incididunt ut lab?!?!", " elit, sed do eiusmod tempor incididunt ut lab?!?!",
}, },
@ -201,7 +201,7 @@ var clippingOrSplittingTestCases = map[string]struct {
clipSplitLength: 50, clipSplitLength: 50,
clippingMessage: "", clippingMessage: "",
splitMax: 2, splitMax: 2,
expectedOutput: []string{ expectedOutput: []string{
"Lorem ipsum dolor sit amet, consectetur adipiscing", "Lorem ipsum dolor sit amet, consectetur adipiscing",
" elit, sed do eiusmod tempor inc <clipped message>", " elit, sed do eiusmod tempor inc <clipped message>",
}, },
@ -218,8 +218,8 @@ var clippingOrSplittingTestCases = map[string]struct {
clipSplitLength: 50, clipSplitLength: 50,
clippingMessage: "", clippingMessage: "",
splitMax: 10, splitMax: 10,
expectedOutput: []string{ expectedOutput: []string{
"人人生而自由,在尊嚴和權利上一律", // Note: only 48 bytes! "人人生而自由,在尊嚴和權利上一律", // Note: only 48 bytes!
"平等。 他們都具有理性和良知,應該", // Note: only 49 bytes! "平等。 他們都具有理性和良知,應該", // Note: only 49 bytes!
"以兄弟情誼的精神對待彼此。", "以兄弟情誼的精神對待彼此。",
}, },