Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
* Update discordgo in order to be able to access Nitro-State
Browse files Browse the repository at this point in the history
* Nitro users can now send any custom emoji
* Non-nitro users can now send all local custom emojis and all GW ones
  • Loading branch information
Bios-Marcel committed Oct 7, 2019
1 parent 419315b commit 02fa70f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/Bios-Marcel/discordemojimap v0.0.0-20190404160132-506fd0e8d912
github.com/Bios-Marcel/discordgo v0.20.4-0.20190901100625-c0b4a9b40243
github.com/Bios-Marcel/discordgo v0.20.4-0.20191007163810-41c4e4a91fd6
github.com/Bios-Marcel/goclipimg v0.0.0-20190417192721-b58a8831f27d
github.com/Bios-Marcel/shortnotforlong v1.0.0
github.com/Bios-Marcel/tview v0.0.0-20191005105220-888684dabd6b
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Bios-Marcel/discordemojimap v0.0.0-20190404160132-506fd0e8d912 h1:Qxa
github.com/Bios-Marcel/discordemojimap v0.0.0-20190404160132-506fd0e8d912/go.mod h1:D1W9gRQXP1UESEobmNIng2i+E8UvdDF4NJ9EOXn9Ano=
github.com/Bios-Marcel/discordgo v0.20.4-0.20190901100625-c0b4a9b40243 h1:FgFOmhd9id31pa7b4B6fTAqq7IEeNPYMyUw7kZIMl3Y=
github.com/Bios-Marcel/discordgo v0.20.4-0.20190901100625-c0b4a9b40243/go.mod h1:bLnfQU0j/SejmPozgW5GepmKvd8CrbMIml2I0IZENVE=
github.com/Bios-Marcel/discordgo v0.20.4-0.20191007163810-41c4e4a91fd6 h1:SKG4DGHPz6ZIWfdnnHB/bto0SyypPhV6OAR8vrVmGeQ=
github.com/Bios-Marcel/discordgo v0.20.4-0.20191007163810-41c4e4a91fd6/go.mod h1:bLnfQU0j/SejmPozgW5GepmKvd8CrbMIml2I0IZENVE=
github.com/Bios-Marcel/goclipimg v0.0.0-20190417192721-b58a8831f27d h1:vfrX8l3fHuaP7gRgEc7mX/lHVTlfQcQeaIdSPmT6Ej0=
github.com/Bios-Marcel/goclipimg v0.0.0-20190417192721-b58a8831f27d/go.mod h1:u7z9t086HoIbA/uuoA2KcRDhKS47DRYCDZ39aegTR4c=
github.com/Bios-Marcel/shortnotforlong v1.0.0 h1:K4JJ5U3+D8LXoAiH0QbfMujWuqKo+NAEZKuQ2RHPqqE=
Expand Down
132 changes: 106 additions & 26 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,13 @@ func (window *Window) IsCursorInsideCodeBlock() bool {
}

func (window *Window) insertQuoteOfMessage(message *discordgo.Message) {
time, parseError := message.Timestamp.Parse()
messageTime, parseError := message.Timestamp.Parse()
if parseError != nil {
return
}

// All quotes should be UTC.
time = time.UTC()
messageTimeUTC := messageTime.UTC()

currentContent := strings.TrimSpace(window.messageInput.GetText())
username := message.Author.Username
Expand All @@ -928,7 +928,7 @@ func (window *Window) insertQuoteOfMessage(message *discordgo.Message) {
}

quotedMessage := strings.ReplaceAll(message.ContentWithMentionsReplaced(), "\n", "\n> ")
quotedMessage = fmt.Sprintf("> **%s** %s UTC:\n> %s\n", username, times.TimeToString(&time), quotedMessage)
quotedMessage = fmt.Sprintf("> **%s** %s UTC:\n> %s\n", username, times.TimeToString(&messageTimeUTC), quotedMessage)
if currentContent != "" {
quotedMessage = quotedMessage + currentContent
}
Expand Down Expand Up @@ -1129,55 +1129,135 @@ func (window *Window) updateServerReadStatus(guildID string, guildNode *tview.Tr
//
// The input is expected to be a string without sorrounding whitespace.
func (window *Window) prepareMessage(targetChannel *discordgo.Channel, inputText string) string {
output := codeBlockRegex.ReplaceAllStringFunc(inputText, func(input string) string {
message := codeBlockRegex.ReplaceAllStringFunc(inputText, func(input string) string {
return strings.ReplaceAll(input, ":", "\\:")
})

if targetChannel.GuildID != "" {
guild, discordError := window.session.State.Guild(targetChannel.GuildID)
channelGuild, discordError := window.session.State.Guild(targetChannel.GuildID)
if discordError == nil {
//Those could be optimized by searching the string for patterns.
for _, channel := range guild.Channels {
for _, channel := range channelGuild.Channels {
if channel.Type == discordgo.ChannelTypeGuildText {
output = strings.ReplaceAll(output, "#"+channel.Name, "<#"+channel.ID+">")
message = strings.ReplaceAll(message, "#"+channel.Name, "<#"+channel.ID+">")
}
}

//Customemojis
if len(guild.Emojis) > 0 {
output = emojiRegex.ReplaceAllStringFunc(output, func(match string) string {
firstDoubleColon := strings.IndexRune(match, ':')
emjoiSequence := match[firstDoubleColon+1 : len(match)-1]
for _, emoji := range guild.Emojis {
if emoji.Name == emjoiSequence {
return match[:firstDoubleColon] + "<:" + emoji.Name + ":" + emoji.ID + ">"
}
}

return match
})
}
message = window.replaceCustomEmojiSequences(channelGuild, message)
}
} else {
message = window.replaceCustomEmojiSequences(nil, message)
}

//Replace formatter characters and replace emoji codes.
output = discordemojimap.Replace(output)
output = strings.Replace(output, "\\:", ":", -1)
message = discordemojimap.Replace(message)
message = strings.Replace(message, "\\:", ":", -1)

if targetChannel.GuildID == "" {
for _, user := range targetChannel.Recipients {
output = strings.ReplaceAll(output, "@"+user.Username+"#"+user.Discriminator, "<@"+user.ID+">")
message = strings.ReplaceAll(message, "@"+user.Username+"#"+user.Discriminator, "<@"+user.ID+">")
}
} else {
members, discordError := window.session.State.Members(targetChannel.GuildID)
if discordError == nil {
for _, member := range members {
output = strings.ReplaceAll(output, "@"+member.User.Username+"#"+member.User.Discriminator, "<@"+member.User.ID+">")
message = strings.ReplaceAll(message, "@"+member.User.Username+"#"+member.User.Discriminator, "<@"+member.User.ID+">")
}
}
}

return output
return message
}

// replaceCustomEmojiSequences replaces all emoji codes for non-unicode
// emojis, e.g. discord custom emojis. the search is lowercase and doesn't
// differentiate between emojis with the same name. Instead it goes by whatever
// it finds first. For private channels, the channelGuild may be nil.
func (window *Window) replaceCustomEmojiSequences(channelGuild *discordgo.Guild, message string) string {
//Simple handling for nitro, since nitro users can pretty much send anything.
if window.session.State.User.PremiumType == discordgo.UserPremiumTypeNitroClassic ||
window.session.State.User.PremiumType == discordgo.UserPremiumTypeNitro {
return emojiRegex.ReplaceAllStringFunc(message, func(match string) string {
firstDoubleColon := strings.IndexRune(match, ':')
emojiSequence := strings.ToLower(match[firstDoubleColon+1 : len(match)-1])
for _, guild := range window.session.State.Guilds {
for _, emoji := range guild.Emojis {
if strings.ToLower(emoji.Name) == emojiSequence {
if emoji.Animated {
return match[:firstDoubleColon] + "<a:" + emoji.Name + ":" + emoji.ID + ">"
} else {
return match[:firstDoubleColon] + "<:" + emoji.Name + ":" + emoji.ID + ">"
}
}
}
}

return match
})
}

return emojiRegex.ReplaceAllStringFunc(message, func(match string) string {
firstDoubleColon := strings.IndexRune(match, ':')
emojiSequence := strings.ToLower(match[firstDoubleColon+1 : len(match)-1])

//Local guild emojis take priority
if channelGuild != nil {
emojiResult := window.findMatchInGuild(channelGuild, true, emojiSequence)
if emojiResult != "" {
return match[:firstDoubleColon] + emojiResult
}
}

//Check for global emotes
for _, guild := range window.session.State.Guilds {
emojiResult := window.findMatchInGuild(guild, false, emojiSequence)
if emojiResult != "" {
return match[:firstDoubleColon] + emojiResult
}
}

return match
})
}

// findMatchInGuild searches for a fitting emoji. Fitting means the correct name
// (caseinsensitive), not animated and the correct permissions. If the result
// is an empty string, it means no result was found.
func (window *Window) findMatchInGuild(guild *discordgo.Guild, omitGWCheck bool, emojiSequence string) string {
for _, emoji := range guild.Emojis {
if emoji.Animated {
continue
}

if strings.ToLower(emoji.Name) == emojiSequence && (omitGWCheck || strings.HasPrefix(emoji.Name, "GW")) {
if len(emoji.Roles) != 0 {
selfMember, cacheError := window.session.State.Member(guild.ID, window.session.State.User.ID)
if cacheError != nil {
selfMember, discordError := window.session.GuildMember(guild.ID, window.session.State.User.ID)
if discordError != nil {
log.Println(discordError)
continue
}

window.session.State.MemberAdd(selfMember)
}

if selfMember != nil {
for _, emojiRole := range emoji.Roles {
for _, selfRole := range selfMember.Roles {
if selfRole == emojiRole {
return "<:" + emoji.Name + ":" + emoji.ID + ">"
}
}
}
}
}

return "<:" + emoji.Name + ":" + emoji.ID + ">"
}
}

return ""
}

// ShowDialog shows a dialog at the bottom of the window. It doesn't surrender
Expand Down

0 comments on commit 02fa70f

Please sign in to comment.