Browse Source

Added AutoMention option to parse MessageSend for any <@ID> tags.

Bruce Marriner 9 years ago
parent
commit
dd4aef7263
2 changed files with 32 additions and 9 deletions
  1. 20 2
      restapi.go
  2. 12 7
      structs.go

+ 20 - 2
restapi.go

@@ -16,6 +16,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"regexp"
 	"time"
 )
 
@@ -408,12 +409,29 @@ func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
 // ChannelMessageSend sends a message to the given channel.
 // channelID : The ID of a Channel.
 // content   : The message to send.
+// NOTE, mention and tts parameters may be added in 2.x branch.
 func (s *Session) ChannelMessageSend(channelID string, content string) (st Message, err error) {
 
+	// TOD: nonce string ?
 	data := struct {
-		Content string `json:"content"`
-	}{content}
+		Content  string   `json:"content"`
+		Mentions []string `json:"mentions"`
+		TTS      bool     `json:"tts"`
+	}{content, nil, false}
+
+	// If true, search for <@ID> tags and add those IDs to mention list.
+	if s.AutoMention {
+		re := regexp.MustCompile(`<@(\d+)>`)
+		match := re.FindAllStringSubmatch(content, -1)
+
+		mentions := make([]string, len(match))
+		for i, m := range match {
+			mentions[i] = m[1]
+		}
+		data.Mentions = mentions
+	}
 
+	// Send the message to the given channel
 	response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
 	err = json.Unmarshal(response, &st)
 	return

+ 12 - 7
structs.go

@@ -23,13 +23,11 @@ import (
 // token : The authentication token returned from Discord
 // Debug : If set to ture debug logging will be displayed.
 type Session struct {
-	Token      string // Authentication token for this session
-	Debug      bool   // Debug for printing JSON request/responses
-	Cache      int    // number in X to cache some responses
-	SessionID  string // from websocket READY packet
-	DataReady  bool   // Set to true when Data Websocket is ready
-	VoiceReady bool   // Set to true when Voice Websocket is ready
-	UDPReady   bool   // Set to true when UDP Connection is ready
+	// General configurable settings.
+	Token       string // Authentication token for this session
+	Debug       bool   // Debug for printing JSON request/responses
+	Cache       int    // number in X to cache some responses
+	AutoMention bool   // if set to True, ChannelSendMessage will auto mention <@ID>
 
 	// Settable Callback functions for Websocket Events
 	OnEvent                   func(*Session, Event) // should Event be *Event?
@@ -56,6 +54,13 @@ type Session struct {
 	OnGuildRoleDelete         func(*Session, GuildRoleDelete)
 	OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate)
 
+	// Exposed but should not be modified by User.
+	SessionID  string // from websocket READY packet
+	DataReady  bool   // Set to true when Data Websocket is ready
+	VoiceReady bool   // Set to true when Voice Websocket is ready
+	UDPReady   bool   // Set to true when UDP Connection is ready
+
+	// Other..
 	wsConn *websocket.Conn
 	//TODO, add bools for like.
 	// are we connnected to websocket?