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

This commit is contained in:
Bruce Marriner 2015-12-06 17:04:05 -06:00
parent c50848e6a9
commit dd4aef7263
2 changed files with 32 additions and 9 deletions

View file

@ -16,6 +16,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"regexp"
"time" "time"
) )
@ -408,12 +409,29 @@ func (s *Session) ChannelMessageAck(channelID, messageID string) (err error) {
// ChannelMessageSend sends a message to the given channel. // ChannelMessageSend sends a message to the given channel.
// channelID : The ID of a Channel. // channelID : The ID of a Channel.
// content : The message to send. // 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) { func (s *Session) ChannelMessageSend(channelID string, content string) (st Message, err error) {
// TOD: nonce string ?
data := struct { data := struct {
Content string `json:"content"` Content string `json:"content"`
}{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) response, err := s.Request("POST", CHANNEL_MESSAGES(channelID), data)
err = json.Unmarshal(response, &st) err = json.Unmarshal(response, &st)
return return

View file

@ -23,13 +23,11 @@ import (
// token : The authentication token returned from Discord // token : The authentication token returned from Discord
// Debug : If set to ture debug logging will be displayed. // Debug : If set to ture debug logging will be displayed.
type Session struct { type Session struct {
// General configurable settings.
Token string // Authentication token for this session Token string // Authentication token for this session
Debug bool // Debug for printing JSON request/responses Debug bool // Debug for printing JSON request/responses
Cache int // number in X to cache some responses Cache int // number in X to cache some responses
SessionID string // from websocket READY packet AutoMention bool // if set to True, ChannelSendMessage will auto mention <@ID>
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
// Settable Callback functions for Websocket Events // Settable Callback functions for Websocket Events
OnEvent func(*Session, Event) // should Event be *Event? OnEvent func(*Session, Event) // should Event be *Event?
@ -56,6 +54,13 @@ type Session struct {
OnGuildRoleDelete func(*Session, GuildRoleDelete) OnGuildRoleDelete func(*Session, GuildRoleDelete)
OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate) 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 wsConn *websocket.Conn
//TODO, add bools for like. //TODO, add bools for like.
// are we connnected to websocket? // are we connnected to websocket?