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

Add initial message block support #5

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

*.pb.go
.env
.direnv
.envrc
71 changes: 46 additions & 25 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (b *Backend) handleMessageCreate(s *discordgo.Session, m *discordgo.Message
}

func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.MessageCreate) {
ok, err := ComesFromDM(s, m)
fromDM, err := ComesFromDM(s, m)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to determine if message is private")
return
Expand All @@ -227,22 +227,28 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
return
}

if ok {
if text, ok := ActionText(rawText); ok {
if fromDM {
rootBlock, isAction, err := TextToBlock(rawText)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

if isAction {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_PrivateAction{PrivateAction: &pb.PrivateActionEvent{
Source: &pb.User{
Id: m.ChannelID,
DisplayName: m.Author.Username,
},
Text: text,
RootBlock: rootBlock,
}}})
} else {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_PrivateMessage{PrivateMessage: &pb.PrivateMessageEvent{
Source: &pb.User{
Id: m.ChannelID,
DisplayName: m.Author.Username,
},
Text: rawText,
RootBlock: rootBlock,
}}})
}
return
Expand All @@ -256,14 +262,8 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
},
}

if text, ok := ActionText(rawText); ok {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Action{Action: &pb.ActionEvent{
Source: source,
Text: text,
}}})
return
}

// Special case - if the message started with the command prefix, we do much
// less parsing and processing on it.
if strings.HasPrefix(rawText, b.cmdPrefix) {
msgParts := strings.SplitN(rawText, " ", 2)
if len(msgParts) < 2 {
Expand All @@ -281,22 +281,43 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
return
}

// Special case - if the original message started with the bot's user ID,
// make sure we trim that off before processing as a mention event.
mentionPrefix := fmt.Sprintf("<@%s>", s.State.User.ID)
if strings.HasPrefix(m.Content, mentionPrefix) {
msg := *m.Message
msg.Content = strings.TrimSpace(strings.TrimPrefix(m.Content, mentionPrefix))

rootBlock, _, err := TextToBlock(ReplaceMentions(b.logger, s, &msg))
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Mention{Mention: &pb.MentionEvent{
Source: source,
Text: ReplaceMentions(b.logger, s, &msg),
Source: source,
RootBlock: rootBlock,
}}})
return
}

b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Message{Message: &pb.MessageEvent{
Source: source,
Text: rawText,
}}})
rootBlock, isAction, err := TextToBlock(rawText)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

if isAction {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Action{Action: &pb.ActionEvent{
Source: source,
RootBlock: rootBlock,
}}})
} else {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Message{Message: &pb.MessageEvent{
Source: source,
RootBlock: rootBlock,
}}})
}
}

func (b *Backend) sendJoinNotification(s *discordgo.Session, guildID, userID, channelID string, count int) {
Expand Down Expand Up @@ -430,13 +451,13 @@ func (b *Backend) handleIngest(ctx context.Context) {
}
_, err = b.discord.ChannelMessageSendComplex(v.SendMessage.ChannelId, &discordgo.MessageSend{
Content: msgText,
Flags: discordgo.MessageFlagsSuppressEmbeds,
Flags: discordgo.MessageFlagsSuppressEmbeds,
})
case *pb.ChatRequest_SendPrivateMessage:
// TODO: this might not work
_, err = b.discord.ChannelMessageSendComplex(v.SendPrivateMessage.UserId, &discordgo.MessageSend{
Content: v.SendPrivateMessage.Text,
Flags: discordgo.MessageFlagsSuppressEmbeds,
Flags: discordgo.MessageFlagsSuppressEmbeds,
})
case *pb.ChatRequest_PerformAction:
msgText := v.PerformAction.Text
Expand All @@ -446,14 +467,14 @@ func (b *Backend) handleIngest(ctx context.Context) {
b.logger.Warn().Err(err).Msg("Tried to send message to unknown channel")
}
_, err = b.discord.ChannelMessageSendComplex(v.PerformAction.ChannelId, &discordgo.MessageSend{
Content: "_"+msgText+"_",
Flags: discordgo.MessageFlagsSuppressEmbeds,
Content: "_" + msgText + "_",
Flags: discordgo.MessageFlagsSuppressEmbeds,
})
case *pb.ChatRequest_PerformPrivateAction:
// TODO: this might not work
_, err = b.discord.ChannelMessageSendComplex(v.PerformPrivateAction.UserId, &discordgo.MessageSend{
Content: "_"+v.PerformPrivateAction.Text+"_",
Flags: discordgo.MessageFlagsSuppressEmbeds,
Content: "_" + v.PerformPrivateAction.Text + "_",
Flags: discordgo.MessageFlagsSuppressEmbeds,
})
case *pb.ChatRequest_JoinChannel:
err = errors.New("unimplemented for discord")
Expand Down
130 changes: 130 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devshell.url = "github:numtide/devshell";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, flake-utils, devshell, nixpkgs, ... }:
flake-utils.lib.eachDefaultSystem (system: {
devShell =
let
pkgs = import nixpkgs {
inherit system;

overlays = [
devshell.overlays.default
];
};
in
pkgs.devshell.mkShell {
devshell.motd = "";

devshell.packages = [
pkgs.go
pkgs.protobuf
pkgs.protoc-gen-go
pkgs.protoc-gen-go-grpc
];
};
});
}
35 changes: 29 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
module github.com/seabird-chat/seabird-discord-backend

go 1.14
go 1.23

toolchain go1.23.2

require (
github.com/bwmarrin/discordgo v0.27.2-0.20240202235938-7f80bc797881
github.com/gorilla/websocket v1.5.1 // indirect
github.com/joho/godotenv v1.5.1
github.com/mattn/go-isatty v0.0.20
github.com/rs/zerolog v1.32.0
github.com/seabird-chat/seabird-go v0.4.0
golang.org/x/net v0.21.0 // indirect
github.com/seabird-chat/seabird-go v0.4.1-0.20240221063203-d8e69692c30b
github.com/stretchr/testify v1.8.4
github.com/yuin/goldmark v1.7.8
golang.org/x/sync v0.6.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240205150955-31a09d347014 // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
google.golang.org/grpc v1.61.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/seabird-chat/seabird-go => ../seabird-go

// This fork is needed because CommonMark allows H4-H6, but Discord doesn't
replace github.com/yuin/goldmark => github.com/belak-forks/goldmark v0.0.0-20250104065338-f2faabf722aa
Loading
Loading