add blacklist

This commit is contained in:
aynakeya
2024-04-15 17:18:03 -07:00
parent 6ddcb507f5
commit 30a2f9ebea
5 changed files with 145 additions and 4 deletions

View File

@@ -288,6 +288,30 @@
"en": "Admin",
"zh-CN": "管理员"
},
"plugin.diange.blacklist.btn.add": {
"en": "Add",
"zh-CN": "添加"
},
"plugin.diange.blacklist.description": {
"en": "Blacklist Configuration",
"zh-CN": "点歌黑名单设置"
},
"plugin.diange.blacklist.input.placeholder": {
"en": "enter word",
"zh-CN": "输入黑名单词"
},
"plugin.diange.blacklist.option.contains": {
"en": "Contains",
"zh-CN": "包含"
},
"plugin.diange.blacklist.option.exact": {
"en": "Exact Match",
"zh-CN": "相等"
},
"plugin.diange.blacklist.title": {
"en": "Blacklist",
"zh-CN": "点歌黑名单"
},
"plugin.diange.cooldown": {
"en": "Cooldown",
"zh-CN": "点歌冷却"

View File

@@ -45,7 +45,7 @@ func createConfigLayout() fyne.CanvasObject {
}
}
content.Objects = []fyne.CanvasObject{
container.NewVScroll(container.NewVBox(desc, widget.NewSeparator(), ConfigList[id].CreatePanel())),
container.NewVScroll(container.NewBorder(container.NewVBox(desc, widget.NewSeparator()), nil, nil, nil, ConfigList[id].CreatePanel())),
}
content.Refresh()
}

View File

@@ -5,7 +5,7 @@ import (
"AynaLivePlayer/global"
"AynaLivePlayer/pkg/config"
"github.com/AynaLivePlayer/miaosic"
_ "github.com/AynaLivePlayer/miaosic/providers/bilibili"
//_ "github.com/AynaLivePlayer/miaosic/providers/bilibili"
_ "github.com/AynaLivePlayer/miaosic/providers/bilivideo"
_ "github.com/AynaLivePlayer/miaosic/providers/kuwo"
"github.com/AynaLivePlayer/miaosic/providers/local"

View File

@@ -0,0 +1,85 @@
package diange
import (
"AynaLivePlayer/pkg/i18n"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type blacklistItem struct {
Exact bool
Value string
}
type blacklist struct {
panel fyne.CanvasObject
}
func (b *blacklist) Title() string {
return i18n.T("plugin.diange.blacklist.title")
}
func (b *blacklist) Description() string {
return i18n.T("plugin.diange.blacklist.description")
}
func (b *blacklist) CreatePanel() fyne.CanvasObject {
if b.panel != nil {
return b.panel
}
// UI组件
input := widget.NewEntry()
input.SetPlaceHolder(i18n.T("plugin.diange.blacklist.input.placeholder"))
exactText := i18n.T("plugin.diange.blacklist.option.exact")
containsText := i18n.T("plugin.diange.blacklist.option.contains")
options := widget.NewRadioGroup([]string{exactText, containsText}, nil)
options.SetSelected(containsText)
var blackListGui *widget.List
blackListGui = widget.NewList(
func() int {
return len(diange.blacklist)
},
func() fyne.CanvasObject {
return container.NewBorder(nil, nil,
widget.NewLabel(""),
widget.NewButtonWithIcon("", theme.DeleteIcon(), nil),
widget.NewLabel(""))
},
func(lii widget.ListItemID, co fyne.CanvasObject) {
if diange.blacklist[lii].Exact {
co.(*fyne.Container).Objects[1].(*widget.Label).SetText(exactText)
} else {
co.(*fyne.Container).Objects[1].(*widget.Label).SetText(containsText)
}
co.(*fyne.Container).Objects[0].(*widget.Label).SetText(diange.blacklist[lii].Value)
co.(*fyne.Container).Objects[2].(*widget.Button).OnTapped = func() {
diange.blacklist = append(diange.blacklist[:lii], diange.blacklist[lii+1:]...)
blackListGui.Refresh()
}
},
)
addButton := widget.NewButton(i18n.T("plugin.diange.blacklist.btn.add"), func() {
if input.Text != "" {
diange.blacklist = append(diange.blacklist, blacklistItem{
Exact: options.Selected == exactText,
Value: input.Text,
})
input.SetText("")
blackListGui.Refresh()
}
})
form := container.NewBorder(
nil,
nil,
options, addButton, input,
)
b.panel = container.NewBorder(form, nil, nil, nil, blackListGui)
return b.panel
}

View File

@@ -37,18 +37,22 @@ type Diange struct {
UserCoolDown int
CustomCMD string
SourceConfigPath string
BlackListItemPath string
SkipSystemPlaylist bool
currentQueueLength int
isCurrentSystem bool
sourceConfigs map[string]*sourceConfig
blacklist []blacklistItem
cooldowns map[string]int
panel fyne.CanvasObject
log logger.ILogger
}
var diange *Diange
func NewDiange() *Diange {
return &Diange{
diange = &Diange{
UserPermission: true,
PrivilegePermission: true,
AdminPermission: true,
@@ -56,6 +60,7 @@ func NewDiange() *Diange {
UserCoolDown: -1,
CustomCMD: "点歌",
SourceConfigPath: "./config/diange.json",
BlackListItemPath: "./config/diange_blacklist.json",
currentQueueLength: 0,
sourceConfigs: map[string]*sourceConfig{
@@ -83,6 +88,7 @@ func NewDiange() *Diange {
cooldowns: make(map[string]int),
log: global.Logger.WithPrefix("Plugin.Logger"),
}
return diange
}
func (d *Diange) Name() string {
@@ -91,15 +97,18 @@ func (d *Diange) Name() string {
func (c *Diange) OnLoad() {
_ = config.LoadJson(c.SourceConfigPath, &c.sourceConfigs)
_ = config.LoadJson(c.BlackListItemPath, &c.blacklist)
}
func (c *Diange) OnSave() {
_ = config.SaveJson(c.SourceConfigPath, c.sourceConfigs)
_ = config.SaveJson(c.BlackListItemPath, c.blacklist)
}
func (d *Diange) Enable() error {
config.LoadConfig(d)
gui.AddConfigLayout(d)
gui.AddConfigLayout(&blacklist{})
global.EventManager.RegisterA(
events.LiveRoomMessageReceive,
"plugin.diange.message",
@@ -189,14 +198,37 @@ func (d *Diange) handleMessage(event *event.Event) {
if !perm {
return
}
d.cooldowns[message.User.Uid] = ct
keywords := strings.Join(msgs[1:], " ")
// blacklist check
for _, item := range d.blacklist {
if item.Exact && item.Value == keywords {
d.log.Warnf("User %s(%s) diange %s is in blacklist %s, ignore", message.User.Username, message.User.Uid, keywords, item.Value)
return
}
if !item.Exact && strings.Contains(keywords, item.Value) {
d.log.Warnf("User %s(%s) diange %s is in blacklist %s, ignore", message.User.Username, message.User.Uid, keywords, item.Value)
return
}
}
d.cooldowns[message.User.Uid] = ct
for _, source := range sources {
medias, err := miaosic.SearchByProvider(source, keywords, 1, 10)
if len(medias) == 0 || err != nil {
continue
}
// double check blacklist
for _, item := range d.blacklist {
if item.Exact && item.Value == medias[0].Title {
d.log.Warnf("User %s(%s) diange %s is in blacklist %s, ignore", message.User.Username, message.User.Uid, keywords, item.Value)
return
}
if !item.Exact && strings.Contains(medias[0].Title, item.Value) {
d.log.Warnf("User %s(%s) diange %s is in blacklist %s, ignore", message.User.Username, message.User.Uid, keywords, item.Value)
return
}
}
if d.SkipSystemPlaylist && d.isCurrentSystem {
global.EventManager.CallA(
events.PlayerPlayCmd,