mirror of
https://github.com/idanoo/gomatrixbot
synced 2025-07-02 00:22:16 +00:00
Fixed?
This commit is contained in:
parent
be94d805cf
commit
b492111b82
4 changed files with 77 additions and 59 deletions
|
@ -26,14 +26,19 @@ func (db *Database) DeleteAllPics() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// StorePic
|
// StorePic
|
||||||
func (db *Database) StorePic(filename string) error {
|
func (db *Database) StorePic(filename string) (int64, error) {
|
||||||
min := db.getPicMinViewCount()
|
min := db.getPicMinViewCount()
|
||||||
_, err := db.db.Exec(
|
exec, err := db.db.Exec(
|
||||||
"INSERT INTO `pics` (`filename`, `viewed`) VALUES (?,?)",
|
"INSERT INTO `pics` (`filename`, `viewed`) VALUES (?,?)",
|
||||||
filename,
|
filename,
|
||||||
min,
|
min,
|
||||||
)
|
)
|
||||||
return err
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return exec.LastInsertId()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePic
|
// DeletePic
|
||||||
|
@ -48,9 +53,9 @@ func (db *Database) ResetPics() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllPics
|
// GetAllPics
|
||||||
func (db *Database) GetAllPics() ([]string, error) {
|
func (db *Database) GetAllPics() (map[int64]string, error) {
|
||||||
var pics []string
|
pics := make(map[int64]string)
|
||||||
row, err := db.db.Query("SELECT filename FROM pics")
|
row, err := db.db.Query("SELECT id, filename FROM pics")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pics, err
|
return pics, err
|
||||||
}
|
}
|
||||||
|
@ -58,8 +63,9 @@ func (db *Database) GetAllPics() ([]string, error) {
|
||||||
defer row.Close()
|
defer row.Close()
|
||||||
for row.Next() {
|
for row.Next() {
|
||||||
var pic string
|
var pic string
|
||||||
row.Scan(&pic)
|
var id int64
|
||||||
pics = append(pics, pic)
|
row.Scan(&id, &pic)
|
||||||
|
pics[id] = pic
|
||||||
}
|
}
|
||||||
|
|
||||||
return pics, nil
|
return pics, nil
|
||||||
|
|
|
@ -58,51 +58,50 @@ func (mtrx *MtrxClient) postBirb(ctx context.Context, evt *event.Event) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mtrx *MtrxClient) postBirbWithText(ctx context.Context, evt *event.Event, text string, attempt int, filename string) {
|
func (mtrx *MtrxClient) postBirbWithText(ctx context.Context, evt *event.Event, text string, filename string) {
|
||||||
idd, birb, err := mtrx.db.GetLastUsedPic()
|
var reader *os.File
|
||||||
|
var err error
|
||||||
|
var birb string
|
||||||
|
|
||||||
|
if filename == "" {
|
||||||
|
// Do a lookup
|
||||||
|
dbId, birb, err := mtrx.db.GetLastUsedPic()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mtrx.c.Log.Err(err).Msg("Failed to get birb")
|
mtrx.sendMessage(ctx, evt.RoomID, "Failed to get an image "+err.Error()+". "+birb)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
reader, err = os.Open(BIRB_PATH + "/" + birb)
|
||||||
if filename != "" {
|
|
||||||
birb = filename
|
|
||||||
}
|
|
||||||
|
|
||||||
reader, err := os.Open(BIRB_PATH + "/" + birb)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mtrx.sendMessage(ctx, evt.RoomID, "Failed to load image "+err.Error()+". "+birb)
|
mtrx.sendMessage(ctx, evt.RoomID, "Failed to load image "+err.Error()+". "+birb)
|
||||||
mtrx.db.DeletePic(idd)
|
mtrx.db.DeletePic(dbId)
|
||||||
if attempt > 3 {
|
delete(mtrx.pics, dbId)
|
||||||
mtrx.db.DeletePic(idd)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Retry
|
birb = filename
|
||||||
mtrx.postBirbWithText(ctx, evt, text, attempt+1, "")
|
reader, err = os.Open(BIRB_PATH + "/" + birb)
|
||||||
|
if err != nil {
|
||||||
|
mtrx.sendMessage(ctx, evt.RoomID, "Failed to load image "+err.Error()+". "+birb)
|
||||||
|
var id int64
|
||||||
|
for k, v := range mtrx.pics {
|
||||||
|
if v == birb {
|
||||||
|
id = k
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mtrx.db.DeletePic(id)
|
||||||
|
delete(mtrx.pics, id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// do the swaps
|
// do the swaps
|
||||||
img, _, err := image.Decode(reader)
|
img, _, err := image.Decode(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mtrx.db.DeletePic(idd)
|
|
||||||
if attempt > 3 {
|
|
||||||
mtrx.db.DeletePic(idd)
|
|
||||||
mtrx.sendMessage(ctx, evt.RoomID, "I can't read this image. I give up. "+err.Error()+". "+birb)
|
mtrx.sendMessage(ctx, evt.RoomID, "I can't read this image. I give up. "+err.Error()+". "+birb)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry
|
|
||||||
mtrx.postBirbWithText(ctx, evt, text, attempt+1, "")
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// r := img.Bounds()
|
|
||||||
// w := r.Dx() // w
|
|
||||||
// h := r.Dy() // h
|
|
||||||
|
|
||||||
imgNew := resize.Resize(1024, 1024, img, resize.Lanczos2)
|
imgNew := resize.Resize(1024, 1024, img, resize.Lanczos2)
|
||||||
m := gg.NewContextForImage(imgNew)
|
m := gg.NewContextForImage(imgNew)
|
||||||
|
|
||||||
|
@ -192,20 +191,31 @@ func (mtrx *MtrxClient) UpdatePics() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
found := false
|
||||||
|
|
||||||
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
|
if file.IsDir() || strings.HasPrefix(file.Name(), ".") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if contains(mtrx.pics, file.Name()) {
|
// check if in our map
|
||||||
|
for _, v := range mtrx.pics {
|
||||||
|
if v == file.Name() {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mtrx.db.StorePic(file.Name())
|
idd, err := mtrx.db.StorePic(file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mtrx.c.Log.Err(err).Msg("Failed to store pic")
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
mtrx.sendMessage(context.Background(), mtrx.adminRoom, "New pic added: "+file.Name())
|
mtrx.sendMessage(context.Background(), mtrx.adminRoom, "New pic added: "+file.Name())
|
||||||
mtrx.pics = append(mtrx.pics, file.Name())
|
mtrx.pics[idd] = file.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -162,9 +162,9 @@ func (mtrx *MtrxClient) handleCommand(ctx context.Context, evt *event.Event) {
|
||||||
return
|
return
|
||||||
case ".rb":
|
case ".rb":
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuote(true), 0, "")
|
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuote(true), "")
|
||||||
} else {
|
} else {
|
||||||
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuoteSearch(true, strings.Join(args[1:], " ")), 0, "")
|
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuoteSearch(true, strings.Join(args[1:], " ")), "")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
case ".delquote":
|
case ".delquote":
|
||||||
|
@ -262,17 +262,19 @@ func (mtrx *MtrxClient) handleCommand(ctx context.Context, evt *event.Event) {
|
||||||
filesearch := strings.ReplaceAll(args[0], ".", "")
|
filesearch := strings.ReplaceAll(args[0], ".", "")
|
||||||
|
|
||||||
// lookup filename from quote, shuffle each time
|
// lookup filename from quote, shuffle each time
|
||||||
tmpPics := make([]string, len(mtrx.pics))
|
matches := make(map[int64]string)
|
||||||
perm := rand.Perm(len(mtrx.pics))
|
keys := make([]int64, 0)
|
||||||
for i, v := range perm {
|
for k, v := range mtrx.pics {
|
||||||
tmpPics[v] = mtrx.pics[i]
|
if strings.Contains(strings.ToLower(strings.Split(v, ".")[0]), strings.ToLower(filesearch)) {
|
||||||
|
matches[k] = v
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range tmpPics {
|
// Shuffle key
|
||||||
if strings.Contains(strings.ToLower(strings.Split(v, ".")[0]), strings.ToLower(filesearch)) {
|
if len(matches) > 0 {
|
||||||
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuoteSearch(true, strings.Join(args[1:], " ")), 0, v)
|
filename := matches[keys[rand.Intn(len(keys)-1)]]
|
||||||
return
|
mtrx.postBirbWithText(ctx, evt, mtrx.getAnyRandomQuoteSearch(true, strings.Join(args[1:], " ")), filename)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ type MtrxClient struct {
|
||||||
savedFiles map[id.EventID]string
|
savedFiles map[id.EventID]string
|
||||||
events map[id.EventID]string
|
events map[id.EventID]string
|
||||||
recentMessages map[id.RoomID]map[int64]string
|
recentMessages map[id.RoomID]map[int64]string
|
||||||
pics []string
|
pics map[int64]string
|
||||||
|
|
||||||
// This is gonna be a mem leak... To fix I guess
|
// This is gonna be a mem leak... To fix I guess
|
||||||
quoteCache map[id.EventID]*Quote
|
quoteCache map[id.EventID]*Quote
|
||||||
|
@ -64,7 +64,7 @@ func Run(db *database.Database) {
|
||||||
pics, err := mtrx.db.GetAllPics()
|
pics, err := mtrx.db.GetAllPics()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
mtrx.pics = make([]string, 0)
|
mtrx.pics = make(map[int64]string, 0)
|
||||||
} else {
|
} else {
|
||||||
mtrx.pics = pics
|
mtrx.pics = pics
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue