diff --git a/src/internal/database/counter.go b/src/internal/database/counter.go new file mode 100644 index 0000000..d30530e --- /dev/null +++ b/src/internal/database/counter.go @@ -0,0 +1,72 @@ +package database + +import ( + "fmt" + "time" +) + +// GetLastUsedPic +func (db *Database) GetCounterFor(key string) (time.Time, error) { + var id int64 + var timestamp time.Time + + row, err := db.db.Query("SELECT `id`, `timestamp` FROM `counter` WHERE `key` = ? LIMIT 1", key) + if err != nil { + return timestamp, err + } + + defer row.Close() + for row.Next() { + row.Scan(&id, ×tamp) + } + + if id == 0 { + return timestamp, fmt.Errorf("%s not found", key) + } + + return timestamp, nil +} + +// UpdateCounterFor - Add counter +func (db *Database) UpdateCounterFor(key string, timestamp time.Time) (int64, error) { + exec, err := db.db.Exec( + "REPLACE INTO `counter` (`key`, `timestamp`) VALUES (?,?)", + key, + timestamp, + ) + + if err != nil { + return 0, err + } + + return exec.LastInsertId() +} + +// GetAllCounters +func (db *Database) GetAllCounters() (map[string]time.Time, error) { + counters := make(map[string]time.Time) + row, err := db.db.Query("SELECT key, timestamp FROM counter order by key ASC") + if err != nil { + return counters, err + } + + defer row.Close() + for row.Next() { + var key string + var timestamp time.Time + row.Scan(&key, ×tamp) + counters[key] = timestamp + } + + return counters, nil +} + +// DeleteCounter - Deletes a counter by key +func (db *Database) DeleteCounter(key string) (int64, error) { + res, err := db.db.Exec("DELETE FROM `counter` WHERE `key` = ?", key) + if err != nil { + return 0, err + } + + return res.RowsAffected() +} diff --git a/src/internal/database/main.go b/src/internal/database/main.go index 2c477c3..49ba72f 100644 --- a/src/internal/database/main.go +++ b/src/internal/database/main.go @@ -46,6 +46,12 @@ func (db *Database) runMigrations() (*Database, error) { return db, err } + _, err = db.db.Exec("CREATE TABLE IF NOT EXISTS `counter` " + + "(`id` INTEGER PRIMARY KEY AUTOINCREMENT, `key` TEXT, `timestamp` DATETIME)") + if err != nil { + return db, err + } + return db, nil } diff --git a/src/internal/gomatrixbot/birds.go b/src/internal/gomatrixbot/birds.go index 14fec59..5ea0a43 100644 --- a/src/internal/gomatrixbot/birds.go +++ b/src/internal/gomatrixbot/birds.go @@ -16,7 +16,7 @@ import ( const fontSize = 42 -const BIRB_PATH = "/root/data/reaction_pics" +const BIRB_PATH = BASE_PATH + "/reaction_pics" func (mtrx *MtrxClient) postBirb(ctx context.Context, evt *event.Event) { idd, birb, err := mtrx.db.GetLastUsedPic(true) diff --git a/src/internal/gomatrixbot/commands.go b/src/internal/gomatrixbot/commands.go index 7b4dcca..3486e8d 100644 --- a/src/internal/gomatrixbot/commands.go +++ b/src/internal/gomatrixbot/commands.go @@ -24,6 +24,9 @@ var COMMANDS = []string{ "☝️ react to set room topic", "📷️ react to save picture for future lulz", "", + ".counter - List all counters", + ".counter - Get a counter for a key", + ".counterreset - Reset a counter for a key", ".r - Returns a random quote (can search quotes)", ".rb - Returns a random quote on a random bird pic (with optional search)", ".remindme