Skip to content

Commit

Permalink
boost: Internal utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
XangelMusic committed Jun 11, 2024
1 parent 08fb2cb commit dfc6879
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 72 deletions.
10 changes: 2 additions & 8 deletions artworks/artworks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package artworks

import (
"github.com/VTGare/boe-tea-go/internal/arrays"
"strings"

"github.com/VTGare/boe-tea-go/store"
Expand Down Expand Up @@ -32,12 +33,5 @@ func IsAIGenerated(content ...string) bool {
"stablediffusion",
}

for _, tag := range content {
for _, test := range aiTags {
if strings.EqualFold(tag, test) {
return true
}
}
}
return false
return arrays.CheckArraysFunc(strings.EqualFold, content, aiTags)
}
2 changes: 1 addition & 1 deletion artworks/deviant/deviant.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type deviantEmbed struct {

func New() artworks.Provider {
return &DeviantArt{
regex: regexp.MustCompile(`(?i)https://(?:www\.)?deviantart\.com/[\w]+/art/([\w\-]+)`),
regex: regexp.MustCompile(`(?i)https://(?:www\.)?deviantart\.com/\w+/art/([\w\-]+)`),
}
}

Expand Down
56 changes: 22 additions & 34 deletions artworks/pixiv/pixiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package pixiv

import (
"fmt"
"github.com/VTGare/boe-tea-go/artworks/embed"
"regexp"
"strconv"
"strings"
"time"

"github.com/VTGare/boe-tea-go/artworks"
"github.com/VTGare/boe-tea-go/artworks/embed"
"github.com/VTGare/boe-tea-go/internal/arrays"
"github.com/VTGare/boe-tea-go/internal/dgoutils"
"github.com/VTGare/boe-tea-go/messages"
"github.com/VTGare/boe-tea-go/store"
"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -89,25 +90,17 @@ func (p *Pixiv) Find(id string) (artworks.Artwork, error) {
return nil, artworks.ErrArtworkNotFound
}

author := ""
if illust.User != nil {
author = illust.User.Name
} else {
author = "Unknown"
}

tags := make([]string, 0)
nsfw := false
for _, tag := range illust.Tags {
if tag.Name == "R-18" {
nsfw = true
}

if tag.TranslatedName != "" {
tags = append(tags, tag.TranslatedName)
} else {
tags = append(tags, tag.Name)
}
tags = dgoutils.Ternary(tag.TranslatedName != "",
append(tags, tag.TranslatedName),
append(tags, tag.Name),
)
}

images := make([]*Image, 0, illust.PageCount)
Expand All @@ -131,38 +124,33 @@ func (p *Pixiv) Find(id string) (artworks.Artwork, error) {
images = append(images, img)
}

artwork := &Artwork{
ID: id,
url: "https://www.pixiv.net/en/artworks/" + id,
Title: illust.Title,
Author: author,
Tags: tags,
Images: images,
NSFW: nsfw,
Type: illust.Type,
Pages: illust.PageCount,
Likes: illust.TotalBookmarks,
CreatedAt: illust.CreateDate,

proxy: p.proxyHost,
}

errImages := []string{
"limit_sanity_level_360.png",
"limit_unknown_360.png",
}

for _, img := range errImages {
if artwork.Images[0].Original == fmt.Sprintf("https://s.pximg.net/common/images/%s", img) {
if images[0].Original == fmt.Sprintf("https://s.pximg.net/common/images/%s", img) {
return nil, artworks.ErrRateLimited
}
}

if illust.IllustAIType == pixiv.IllustAITypeAIGenerated {
artwork.AIGenerated = true
}
return &Artwork{
ID: id,
url: "https://www.pixiv.net/en/artworks/" + id,
Title: illust.Title,
Author: dgoutils.Ternary(illust.User != nil, illust.User.Name, "Unknown"),
Tags: tags,
Images: images,
NSFW: nsfw,
Type: illust.Type,
Pages: illust.PageCount,
Likes: illust.TotalBookmarks,
CreatedAt: illust.CreateDate,
AIGenerated: illust.IllustAIType == pixiv.IllustAITypeAIGenerated,

return artwork, nil
proxy: p.proxyHost,
}, nil
})
}

Expand Down
5 changes: 2 additions & 3 deletions commands/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,8 @@ func feedback(*bot.Bot) func(*gumi.Ctx) error {

if len(gctx.Event.Attachments) > 0 {
att := gctx.Event.Attachments[0]
if strings.HasSuffix(att.Filename, "png") ||
strings.HasSuffix(att.Filename, "jpg") ||
strings.HasSuffix(att.Filename, "gif") {
if arrays.CheckFuncCompareArgs(
strings.HasSuffix, att.Filename, "png", "jpg", "gif") {
eb.Image(att.URL)
}
}
Expand Down
10 changes: 4 additions & 6 deletions commands/memes.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ func gamba(*bot.Bot) func(*gumi.Ctx) error {
return func(gctx *gumi.Ctx) error {
getItTwisted := rand.Intn(10) != 0

var text string
if getItTwisted {
text = `🦍 🗣 GET IT TWISTED 🌪 , GAMBLE ✅ . PLEASE START GAMBLING 👍 . GAMBLING IS AN INVESTMENT 🎰 AND AN INVESTMENT ONLY 👍 . YOU WILL PROFIT 💰 , YOU WILL WIN ❗ ️. YOU WILL DO ALL OF THAT 💯 , YOU UNDERSTAND ⁉ ️ YOU WILL BECOME A BILLIONAIRE 💵 📈 AND REBUILD YOUR FUCKING LIFE 🤯`
} else {
text = `🦍 🗣️ DO NOT GET IT TWISTED 🌪️ , DO NOT GAMBLE 🚫 . DO NOT START GAMBLING ❌ . GAMBLING IS ENTERTAINMENT 🎰 AND ENTERTAINMENT ONLY 👍 . YOU WONT BREAK EVEN 🛑 , YOU WONT WIN ⚠️ ️. YOU WONT DO ANY OF THAT 💯 , YOU UNDERSTAND ⁉️ ️ YOU WILL ONLY GO INTO DEBT 💵 📉 AND RUIN YOUR FUCKING LIFE 😵`
}
text := dgoutils.Ternary(getItTwisted,
`🦍 🗣 GET IT TWISTED 🌪 , GAMBLE ✅ . PLEASE START GAMBLING 👍 . GAMBLING IS AN INVESTMENT 🎰 AND AN INVESTMENT ONLY 👍 . YOU WILL PROFIT 💰 , YOU WILL WIN ❗ ️. YOU WILL DO ALL OF THAT 💯 , YOU UNDERSTAND ⁉ ️ YOU WILL BECOME A BILLIONAIRE 💵 📈 AND REBUILD YOUR FUCKING LIFE 🤯`,
`🦍 🗣️ DO NOT GET IT TWISTED 🌪️ , DO NOT GAMBLE 🚫 . DO NOT START GAMBLING ❌ . GAMBLING IS ENTERTAINMENT 🎰 AND ENTERTAINMENT ONLY 👍 . YOU WONT BREAK EVEN 🛑 , YOU WONT WIN ⚠️ ️. YOU WONT DO ANY OF THAT 💯 , YOU UNDERSTAND ⁉️ ️ YOU WILL ONLY GO INTO DEBT 💵 📉 AND RUIN YOUR FUCKING LIFE 😵`,
)

return gctx.Reply(text)
}
Expand Down
14 changes: 6 additions & 8 deletions commands/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,16 @@ func set(b *bot.Bot) func(*gumi.Ctx) error {
),
)

var artChannels []string
if len(guild.ArtChannels) > 5 {
artChannels = []string{"There are more than 5 art channels, use `bt!artchannels` command to see them."}
} else {
artChannels = arrays.Map(guild.ArtChannels, func(s string) string {
channels := dgoutils.Ternary(len(guild.ArtChannels) > 5,
[]string{"There are more than 5 art channels, use `bt!artchannels` command to see them."},
arrays.Map(guild.ArtChannels, func(s string) string {
return fmt.Sprintf("<#%v> | `%v`", s, s)
})
}
}),
)

eb.AddField(
"Art channels",
"Use `bt!artchannels` command to list or manage art channels!\n\n"+strings.Join(artChannels, "\n"),
"Use `bt!artchannels` command to list or manage art channels!\n\n"+strings.Join(channels, "\n"),
)

return gctx.ReplyEmbed(eb.Finalize())
Expand Down
6 changes: 3 additions & 3 deletions commands/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func push(b *bot.Bot) func(*gumi.Ctx) error {
continue
}

if arrays.Any(group.Children, channelID) {
if arrays.Check(channelID, group.Children) {
continue
}

Expand Down Expand Up @@ -452,7 +452,7 @@ func remove(b *bot.Bot) func(*gumi.Ctx) error {
for arg := range gctx.Args.Arguments {
channelID := dgoutils.Trimmer(gctx, arg)

if !arrays.Any(group.Children, channelID) {
if !arrays.Check(channelID, group.Children) {
continue
}

Expand Down Expand Up @@ -506,7 +506,7 @@ func editParent(b *bot.Bot) func(*gumi.Ctx) error {
return messages.ErrGroupAlreadyExists(dest)
}

if arrays.Any(group.Children, dest) {
if arrays.Check(dest, group.Children) {
return messages.ErrUserEditParentFail(group.Parent, dest)
}

Expand Down
2 changes: 1 addition & 1 deletion handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func OnMessage(b *bot.Bot) func(*gumi.Ctx) error {
return err
}

if !(len(guild.ArtChannels) == 0 || arrays.Any(guild.ArtChannels, gctx.Event.ChannelID)) {
if !(len(guild.ArtChannels) == 0 || arrays.Check(gctx.Event.ChannelID, guild.ArtChannels)) {
return nil
}

Expand Down
58 changes: 55 additions & 3 deletions internal/arrays/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ import (
"time"
)

func Any[T comparable](slice []T, cmp T) bool {
func collate[T comparable](a, b T, c ...T) []T {
slice := make([]T, 0)
slice = append(slice, a, b)
return append(slice, c...)
}

func Check[T comparable](compare T, slice []T) bool {
for _, val := range slice {
if val == cmp {
if val == compare {
return true
}
}

return false
}

func AnyFunc[T comparable](slice []T, f func(T) bool) bool {
func CheckArgs[T comparable](compare, a, b T, c ...T) bool {
return Check(compare, collate(a, b, c...))
}

func CheckFunc[T comparable](f func(T) bool, slice []T) bool {
for _, val := range slice {
if f(val) {
return true
Expand All @@ -25,6 +35,48 @@ func AnyFunc[T comparable](slice []T, f func(T) bool) bool {
return false
}

func CheckFuncArgs[T comparable](f func(T) bool, a, b T, c ...T) bool {
return CheckFunc(f, collate(a, b, c...))
}

func CheckFuncCompare[T comparable](f func(T, T) bool, compare T, slice []T) bool {
for _, val := range slice {
if f(compare, val) {
return true
}
}

return false
}

func CheckFuncCompareArgs[T comparable](f func(T, T) bool, compare, a, b T, c ...T) bool {
return CheckFuncCompare(f, compare, collate(a, b, c...))
}

func CheckArrays[T comparable](a, b []T) bool {
for _, val := range a {
for _, test := range b {
if val == test {
return true
}
}
}

return false
}

func CheckArraysFunc[T comparable](f func(T, T) bool, a, b []T) bool {
for _, val := range a {
for _, test := range b {
if f(val, test) {
return true
}
}
}

return false
}

func Filter[T any](slice []T, f func(T) bool) []T {
filtered := make([]T, 0)

Expand Down
10 changes: 9 additions & 1 deletion internal/dgoutils/dgoutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func Ternary[T any](condition bool, a T, b T) T {
return b
}

func TernaryFunc[T any](f func(T), condition bool, a T, b T) {
if condition {
f(a)
}

f(b)
}

func ValidateArgs(gctx *gumi.Ctx, argsLen int) error {
if gctx.Args.Len() < argsLen {
return messages.ErrIncorrectCmd(gctx.Command)
Expand Down Expand Up @@ -132,7 +140,7 @@ func (r *Range) Array() []int {
}

func (r *Range) Map() map[int]struct{} {
m := make(map[int]struct{}, 0)
m := make(map[int]struct{})
for i := r.Low; i <= r.High; i++ {
m[i] = struct{}{}
}
Expand Down
6 changes: 3 additions & 3 deletions post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (p *Post) Crosspost(ctx context.Context, userID string, group *store.Group)
"group", group,
"channel_id", channelID,
)

go func(channelID string) {
defer wg.Done()
ch, err := p.Ctx.Session.Channel(channelID)
Expand All @@ -194,7 +194,7 @@ func (p *Post) Crosspost(ctx context.Context, userID string, group *store.Group)
}

if guild.Crosspost {
if len(guild.ArtChannels) == 0 || arrays.Any(guild.ArtChannels, ch.ID) {
if len(guild.ArtChannels) == 0 || arrays.Check(ch.ID, guild.ArtChannels) {
p.CrosspostMode = true
res, err := p.fetch(ctx, guild, channelID)
if err != nil {
Expand All @@ -207,7 +207,7 @@ func (p *Post) Crosspost(ctx context.Context, userID string, group *store.Group)
log.With("error", err).Error("failed to send messages")
return
}

msgChan <- sent
}
}
Expand Down
2 changes: 1 addition & 1 deletion store/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func DefaultUser(id string) *User {
func (u *User) FindGroup(channelID string) (*Group, bool) {
for _, group := range u.Groups {
if group.IsPair {
if arrays.Any(group.Children, channelID) {
if arrays.Check(channelID, group.Children) {
return group, true
}

Expand Down

0 comments on commit dfc6879

Please sign in to comment.