implement reactions from the discord side
This commit is contained in:
@@ -288,7 +288,9 @@ func (p *Portal) handleDiscordMessages(msg portalDiscordMessage) {
|
|||||||
|
|
||||||
switch msg.msg.(type) {
|
switch msg.msg.(type) {
|
||||||
case *discordgo.MessageCreate:
|
case *discordgo.MessageCreate:
|
||||||
p.handleDiscordMessage(msg.msg.(*discordgo.MessageCreate).Message)
|
p.handleDiscordMessage(msg.user, msg.msg.(*discordgo.MessageCreate).Message)
|
||||||
|
case *discordgo.MessageReactionAdd:
|
||||||
|
p.handleDiscordReaction(msg.user, msg.msg.(*discordgo.MessageReactionAdd).MessageReaction, true)
|
||||||
default:
|
default:
|
||||||
p.log.Warnln("unknown message type")
|
p.log.Warnln("unknown message type")
|
||||||
}
|
}
|
||||||
@@ -314,7 +316,11 @@ func (p *Portal) markMessageHandled(msg *database.Message, discordID string, mxi
|
|||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Portal) handleDiscordMessage(msg *discordgo.Message) {
|
func (p *Portal) handleDiscordMessage(user *User, msg *discordgo.Message) {
|
||||||
|
if user.ID == msg.Author.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if p.MXID == "" {
|
if p.MXID == "" {
|
||||||
p.log.Warnln("handle message called without a valid portal")
|
p.log.Warnln("handle message called without a valid portal")
|
||||||
|
|
||||||
@@ -537,3 +543,39 @@ func (p *Portal) handleMatrixReaction(evt *event.Event) {
|
|||||||
user.Session.MessageReactionAdd(p.Key.ChannelID, msg.DiscordID, reaction.RelatesTo.Key)
|
user.Session.MessageReactionAdd(p.Key.ChannelID, msg.DiscordID, reaction.RelatesTo.Key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Portal) handleDiscordReaction(user *User, reaction *discordgo.MessageReaction, add bool) {
|
||||||
|
if user.ID == reaction.UserID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if reaction.Emoji.ID != "" {
|
||||||
|
p.log.Debugln("ignoring non-unicode reaction")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
message := p.bridge.db.Message.GetByDiscordID(p.Key, reaction.MessageID)
|
||||||
|
if message == nil {
|
||||||
|
p.log.Debugfln("failed to add reaction to message %s: message not found", reaction.MessageID)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
intent := p.bridge.GetPuppetByID(reaction.UserID).IntentFor(p)
|
||||||
|
|
||||||
|
content := event.Content{Parsed: &event.ReactionEventContent{
|
||||||
|
RelatesTo: event.RelatesTo{
|
||||||
|
EventID: message.MatrixID,
|
||||||
|
Type: event.RelAnnotation,
|
||||||
|
Key: reaction.Emoji.Name,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
_, err := intent.Client.SendMessageEvent(p.MXID, event.EventReaction, &content)
|
||||||
|
if err != nil {
|
||||||
|
p.log.Errorfln("failed to send reaction from %s: %v", reaction.MessageID, err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -213,6 +213,7 @@ func (u *User) Connect() error {
|
|||||||
u.User.Session.AddHandler(u.channelUpdateHandler)
|
u.User.Session.AddHandler(u.channelUpdateHandler)
|
||||||
|
|
||||||
u.User.Session.AddHandler(u.messageHandler)
|
u.User.Session.AddHandler(u.messageHandler)
|
||||||
|
u.User.Session.AddHandler(u.reactionHandler)
|
||||||
|
|
||||||
// u.User.Session.Identify.Capabilities = 125
|
// u.User.Session.Identify.Capabilities = 125
|
||||||
// // Setup our properties
|
// // Setup our properties
|
||||||
@@ -279,7 +280,25 @@ func (u *User) channelUpdateHandler(s *discordgo.Session, c *discordgo.ChannelUp
|
|||||||
|
|
||||||
func (u *User) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func (u *User) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
if m.GuildID != "" {
|
if m.GuildID != "" {
|
||||||
u.log.Debugln("ignoring guild build messaged")
|
u.log.Debugln("ignoring message for guild")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
key := database.NewPortalKey(m.ChannelID, u.User.ID)
|
||||||
|
portal := u.bridge.GetPortalByID(key)
|
||||||
|
|
||||||
|
msg := portalDiscordMessage{
|
||||||
|
msg: m,
|
||||||
|
user: u,
|
||||||
|
}
|
||||||
|
|
||||||
|
portal.discordMessages <- msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) reactionHandler(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
||||||
|
if m.GuildID != "" {
|
||||||
|
u.log.Debugln("ignoring reaction for guild message")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user