Skip to content

Commit

Permalink
server now converts discord mentions to usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamiell committed Aug 8, 2018
1 parent dd1a8c1 commit 116abf9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
71 changes: 52 additions & 19 deletions src/chat.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package main

import (
"regexp"
"strconv"
"strings"
"time"
)

type ChatMessage struct {
Msg string `json:"msg"`
Who string `json:"who"`
Discord bool `json:"discord"`
Server bool `json:"server"`
Datetime time.Time `json:"datetime"`
Room string `json:"room"`
}

func chatMakeMessage(msg string, who string, discord bool, server bool, datetime time.Time, room string) *ChatMessage {
return &ChatMessage{
Msg: msg,
Who: who,
Discord: discord,
Server: server,
Datetime: datetime,
Room: room,
}
}
/*
Chat command functions
*/

func chatHere(s *Session) {
// Check to see if enough time has passed from the last @here
Expand Down Expand Up @@ -94,3 +79,51 @@ func chatRandom(s *Session, d *CommandData) {
}
commandChat(nil, d)
}

/*
Subroutines
*/

type ChatMessage struct {
Msg string `json:"msg"`
Who string `json:"who"`
Discord bool `json:"discord"`
Server bool `json:"server"`
Datetime time.Time `json:"datetime"`
Room string `json:"room"`
}

func chatMakeMessage(msg string, who string, discord bool, server bool, datetime time.Time, room string) *ChatMessage {
return &ChatMessage{
Msg: msg,
Who: who,
Discord: discord,
Server: server,
Datetime: datetime,
Room: room,
}
}

func chatFillMentions(msg string) string {
// Discord mentions are in the form of "<@71242588694249472>"
// (by the time the message gets here, it will be sanitized to "&lt;@71242588694249472&gt;")
// We want to convert this to the username, so that the lobby displays messages in a manner similar to the Discord client
var mentionRegExp *regexp.Regexp
if v, err := regexp.Compile(`&lt;@(\d+)?&gt;`); err != nil {
log.Error("Failed to create the Discord mention regular expression:", err)
return msg
} else {
mentionRegExp = v
}

for {
match := mentionRegExp.FindStringSubmatch(msg)
if match == nil || len(match) < 2 {
break
}
discordID := match[1]
username := discordGetNickname(discordID)
msg = strings.Replace(msg, "&lt;@"+discordID+"&gt;", "@"+username, -1)
}
return msg
}
5 changes: 4 additions & 1 deletion src/commandChat.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ func commandChat(s *Session, d *CommandData) {
return
}

// Transform Discord mentions from number to username
msg := chatFillMentions(d.Msg)

// Lobby messages go to everyone
for _, s2 := range sessions {
s2.NotifyChat(d.Msg, username, d.Discord, d.Server, time.Now(), d.Room)
s2.NotifyChat(msg, username, d.Discord, d.Server, time.Now(), d.Room)
}

// Send the chat message to the Discord "#general" channel if we are replicating a message
Expand Down
6 changes: 3 additions & 3 deletions src/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func discordMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
commandMutex.Lock()
defer commandMutex.Unlock()
d := &CommandData{
Username: discordGetNickname(m) + "#" + m.Author.Discriminator,
Username: discordGetNickname(m.Author.ID) + "#" + m.Author.Discriminator,
Msg: m.Content,
Discord: true,
Room: "lobby",
Expand Down Expand Up @@ -182,7 +182,7 @@ func discordSend(to string, username string, msg string) {
}
}

func discordGetNickname(m *discordgo.MessageCreate) string {
func discordGetNickname(discordID string) string {
// Get the Discord guild object
var guild *discordgo.Guild
if v, err := discord.Guild(discordListenChannels[0]); err != nil { // Assume that the first channel ID is the same as the server ID
Expand All @@ -193,7 +193,7 @@ func discordGetNickname(m *discordgo.MessageCreate) string {

// Get their custom nickname for the Discord server, if any
for _, member := range guild.Members {
if member.User.ID != m.Author.ID {
if member.User.ID != discordID {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ <h2>Settings</h2>
<input id="send-chat-notification" type="checkbox">
<label for="send-chat-notification">
<span class="label-text">
Receive notifications when your name is mentioned in chat
Show desktop notifications when your name is mentioned in chat
</span>
</label>

Expand Down
4 changes: 2 additions & 2 deletions src/waitingList.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func waitingListInit() {

func waitingListAdd(m *discordgo.MessageCreate) {
waitingListPurgeOld()
username := discordGetNickname(m)
username := discordGetNickname(m.Author.ID)

// Search through the waiting list to see if they are already on it
for _, waiter := range waitingList {
Expand Down Expand Up @@ -55,7 +55,7 @@ func waitingListAdd(m *discordgo.MessageCreate) {

func waitingListRemove(m *discordgo.MessageCreate) {
waitingListPurgeOld()
username := discordGetNickname(m)
username := discordGetNickname(m.Author.ID)

// Search through the waiting list to see if they are already on it
for i, waiter := range waitingList {
Expand Down
3 changes: 3 additions & 0 deletions view_log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

vim logs/hanabi-live.log

0 comments on commit 116abf9

Please sign in to comment.