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

Use forward messages #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/elliotwms/pinbot

go 1.23

replace github.com/bwmarrin/discordgo => github.com/elliotwms/discordgo v0.23.3-0.20250129005426-fecefc6e5375

require (
github.com/bwmarrin/discordgo v0.28.1
github.com/bwmarrin/snowflake v0.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotwms/bot v0.3.33 h1:McFeMu0fMssxDkkxTOxWpc97G7zAQKRsX3k9cnM3G7w=
github.com/elliotwms/bot v0.3.33/go.mod h1:2laSFmgz3PLUuSLxbwqAocRRsM02CgPbhOKCgJdHngw=
github.com/elliotwms/discordgo v0.23.3-0.20250129005426-fecefc6e5375 h1:eFyMqRSDLdSzHTzroXkKoPS4lWmHr2R8g0Rr5QLw++k=
github.com/elliotwms/discordgo v0.23.3-0.20250129005426-fecefc6e5375/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/elliotwms/fakediscord v0.18.2 h1:EN1JeyhMMPpZf+UnZMvROjhuzsAM+z4nw9bVseakcjo=
github.com/elliotwms/fakediscord v0.18.2/go.mod h1:diPjfnMTjg73Izwl5pHMvGoGuOL8VSsMVt2ugFhfCos=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
84 changes: 32 additions & 52 deletions internal/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package commands
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/bwmarrin/discordgo"
"log/slog"
)

const (
Expand Down Expand Up @@ -60,16 +58,27 @@ func PinMessageCommandHandler(s *discordgo.Session, i *discordgo.InteractionCrea
log = log.With("target_channel_id", targetChannel.ID)

// build the rich embed pin message
pinMessage := buildPinMessage(sourceChannel, m, i.Member.User)
forward := &discordgo.MessageSend{
Reference: m.Forward(),
}

// send the pin message
log.Debug("Sending pin message")
pin, err := s.ChannelMessageSendComplex(targetChannel.ID, pinMessage, discordgo.WithContext(ctx))
pin, err := s.ChannelMessageSendComplex(targetChannel.ID, forward, discordgo.WithContext(ctx))
if err != nil {
log.Error("Could not send pin message", "error", err)
return respond(ctx, s, i.Interaction, "🙅 Could not send pin message. Please ensure bot has permission to post in "+targetChannel.Mention())
}

// build a secondary info message to accompany the forward
infoMessage := buildPinInfoMessage(sourceChannel, m, pin, i.Member.User)

log.Debug("Sending pin info message")
_, err = s.ChannelMessageSendComplex(targetChannel.ID, infoMessage, discordgo.WithContext(ctx))
if err != nil {
log.Error("Could not send info message", "error", err)
return respond(ctx, s, i.Interaction, "🙅 Could not send pin message. Please ensure bot has permission to post in "+targetChannel.Mention())
}

// mark the message as done
if err := s.MessageReactionAdd(m.ChannelID, m.ID, emojiPinned, discordgo.WithContext(ctx)); err != nil {
log.Error("Could not react to message", "error", err)
Expand Down Expand Up @@ -97,68 +106,39 @@ func url(guildID, channelID, messageID string) string {
)
}

func buildPinMessage(sourceChannel *discordgo.Channel, m *discordgo.Message, pinnedBy *discordgo.User) *discordgo.MessageSend {
fields := []*discordgo.MessageEmbedField{
{
Name: "Channel",
Value: sourceChannel.Mention(),
Inline: true,
},
}

u := url(sourceChannel.GuildID, m.ChannelID, m.ID)
func buildPinInfoMessage(sourceChannel *discordgo.Channel, m *discordgo.Message, pin *discordgo.Message, pinnedBy *discordgo.User) *discordgo.MessageSend {
u := url(sourceChannel.GuildID, pin.ChannelID, pin.ID)
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: m.Author.Username,
IconURL: m.Author.AvatarURL(""),
URL: u,
},
Title: "📌 Pinned",
Color: pinMessageColor,
Description: m.Content,
URL: u,
Timestamp: m.Timestamp.Format(time.RFC3339),
Title: "📌 Pinned",
Color: pinMessageColor,
URL: u,
Fields: []*discordgo.MessageEmbedField{
{
Name: "Channel",
Value: sourceChannel.Mention(),
Inline: true,
},
},
}

if pinnedBy != nil {
fields = append(fields, &discordgo.MessageEmbedField{
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "Pinned by",
Value: pinnedBy.Mention(),
Inline: true,
})
}

embed.Fields = fields

pinMessage := &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{embed},
}

// If there are multiple attachments then add them to separate embeds
for i, a := range m.Attachments {
if a.Width == 0 || a.Height == 0 {
// only embed images
continue
}
e := &discordgo.MessageEmbedImage{URL: a.URL}

if i == 0 {
// add the first image to the existing embed
pinMessage.Embeds[0].Image = e
} else {
// add any other images to their own embed
pinMessage.Embeds = append(pinMessage.Embeds, &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeImage,
Color: pinMessageColor,
Image: e,
})
}
return &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{
embed,
},
}

// preserve the existing embeds
pinMessage.Embeds = append(pinMessage.Embeds, m.Embeds...)

return pinMessage
}

func isAlreadyPinned(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate, m *discordgo.Message) (bool, error) {
Expand Down