Content-Length: 490186 | pFad | http://github.com/pajbot/pajbot2/commit/36d974329fa7f28b1e810fe0c436bdafc8d56384

0B Experimental message height stuff · pajbot/pajbot2@36d9743 · GitHub
Skip to content

Commit

Permalink
Experimental message height stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Oct 14, 2018
1 parent f7b7008 commit 36d9743
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "3rdParty/MessageHeightTwitch"]
path = 3rdParty/MessageHeightTwitch
url = https://github.com/TETYYS/MessageHeightTwitch.git
1 change: 1 addition & 0 deletions 3rdParty/MessageHeightTwitch
Submodule MessageHeightTwitch added at c71502
1 change: 1 addition & 0 deletions cmd/bot/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ func (a *Application) LoadBots() error {
bot.AddModule(modules.NewEmoteFilter(bot))
bot.AddModule(modules.NewBannedNames())
bot.AddModule(modules.NewLinkFilter())
bot.AddModule(modules.NewMessageHeightLimit())

bot.AddModule(modules.NewMessageLengthLimit())

Expand Down
2 changes: 0 additions & 2 deletions cmd/bot/twitchusercontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ func (c *UserContext) AddContext(channelID, userID, message string) {
c.context[channelID] = make(map[string][]string)
}

fmt.Println("Add context", channelID, userID, message)

c.context[channelID][userID] = append(c.context[channelID][userID], message)
newLen := len(c.context[channelID][userID]) - 5
if newLen > 5 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/modules/emote_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (m EmoteFilter) OnMessage(bot pkg.Sender, source pkg.Channel, user pkg.User
return nil
}

if source.GetChannel() == "forsen" {
return nil
}

// BTTV Emotes
reader := message.GetBTTVReader()
timeoutDuration := 0
Expand Down
185 changes: 185 additions & 0 deletions pkg/modules/message_height_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// +build csharp

package modules

// To enable the message height limit module, you need .NET Core on your server

// #cgo LDFLAGS: -L../../3rdParty/MessageHeightTwitch/c-interop -lcoreruncommon -ldl -lstdc++
// #include "../../3rdParty/MessageHeightTwitch/c-interop/exports.h"
// #include <stdlib.h>
import "C"

import (
"errors"
"fmt"
"os"
"path/filepath"
"unsafe"

"github.com/pajlada/pajbot2/pkg"
)

var _ pkg.Module = &MessageHeightLimit{}

type MessageHeightLimit struct {
server *server
}

func NewMessageHeightLimit() *MessageHeightLimit {
return &MessageHeightLimit{
server: &_server,
}
}

var clrInitialized = false
var messageHeightLimitLibraryInitialized = false
var charMapPath string

func initCLR() error {
executableDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}

clrLibraryFolder, clrLibraryFolderSet := os.LookupEnv("LIBCOREFOLDER")
if !clrLibraryFolderSet {
clrLibraryFolder = "/opt/dotnet/shared/Microsoft.NETCore.App/2.1.5/"
}

// Path to our own executable
clr1 := C.CString(executableDir + "/bot")

// Folder where libcoreclr.so is located
clr2 := C.CString(clrLibraryFolder)

// Path to library we want to use
clr3 := C.CString(executableDir + "/MessageHeightTwitch.dll")

var res C.int

res = C.LoadCLRRuntime(
clr1,
clr2,
clr3)

C.free(unsafe.Pointer(clr1))
C.free(unsafe.Pointer(clr2))
C.free(unsafe.Pointer(clr3))

if res != 0 {
return errors.New("Failed to load CLR Runtime")
}

charMapPath = executableDir + "/charmap.bin.gz"

clrInitialized = true

return nil
}

func initMessageHeightLimitLibrary() error {
charMap := C.CString(charMapPath)
channel := C.CString("forsen")

fmt.Println(charMapPath)

res := C.InitMessageHeightTwitch(charMap, channel)

C.free(unsafe.Pointer(charMap))
C.free(unsafe.Pointer(channel))

if res != 0 {
return errors.New("Failed to init MessageHeightTwitch")
}

messageHeightLimitLibraryInitialized = true

return nil
}

func (m *MessageHeightLimit) Register() (err error) {
if !clrInitialized {
err = initCLR()
if err != nil {
return
}

err = initMessageHeightLimitLibrary()
}

return
}

func (m *MessageHeightLimit) Name() string {
return "MessageHeightLimit"
}

func (m *MessageHeightLimit) OnWhisper(bot pkg.Sender, user pkg.User, message pkg.Message) error {
return nil
}

func (m *MessageHeightLimit) getHeight(user pkg.User, message pkg.Message) float32 {
input := C.CString(message.GetText())
loginName := C.CString(user.GetName())
displayName := C.CString(user.GetDisplayName())

var emoteStrings []*C.char

var te []C.TwitchEmote

reader := message.GetTwitchReader()
for reader.Next() {
emote := reader.Get()
emoteCode := C.CString(emote.GetName())
emoteURL := C.CString(fmt.Sprintf("https://static-cdn.jtvnw.net/emoticons/v1/%s/1.0", emote.GetID()))

te = append(te, C.TwitchEmote{emoteCode, emoteURL})

emoteStrings = append(emoteStrings, emoteCode)
emoteStrings = append(emoteStrings, emoteURL)
}

var pArray unsafe.Pointer

if len(te) > 0 {
pArray = unsafe.Pointer(&te[0])
}

badgeCount := C.int(len(user.GetBadges()))

height := C.CalculateMessageHeightDirect(
input, // Message text
loginName, // Login name
displayName, // Display name
badgeCount, // Badge count
((*C.TwitchEmote)(pArray)), // Array of emotes
C.int(len(te)), // Emote array size
)

C.free(unsafe.Pointer(input))
C.free(unsafe.Pointer(loginName))
C.free(unsafe.Pointer(displayName))

for _, str := range emoteStrings {
C.free(unsafe.Pointer(str))
}

return float32(height)
}

func (m *MessageHeightLimit) OnMessage(bot pkg.Sender, channel pkg.Channel, user pkg.User, message pkg.Message, action pkg.Action) error {
if !messageHeightLimitLibraryInitialized {
return nil
}

if channel.GetChannel() != "forsen" {
return nil
}

const heightLimit = 160

height := m.getHeight(user, message)
bot.Mention(channel, user, fmt.Sprintf("Message height: %f\n", height))

return nil
}
2 changes: 2 additions & 0 deletions pkg/modules/message_length_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (m MessageLengthLimit) OnMessage(bot pkg.Sender, channel pkg.Channel, user
return nil
}

return nil

if user.GetName() == "gazatu2" {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type User interface {
GetID() string
IsModerator() bool
IsBroadcaster(Channel) bool
GetBadges() map[string]int
}
4 changes: 4 additions & 0 deletions pkg/users/twitchuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (u TwitchUser) IsBroadcaster(channel pkg.Channel) bool {
return u.GetName() == channel.GetChannel()
}

func (u TwitchUser) GetBadges() map[string]int {
return u.Badges
}

func GetUserPermissions(userID, channelID string) (pkg.Permission, error) {
switch channelID {
case "global":
Expand Down

0 comments on commit 36d9743

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/pajbot/pajbot2/commit/36d974329fa7f28b1e810fe0c436bdafc8d56384

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy