add reminder feature
This commit is contained in:
parent
81f0970e36
commit
cfc48bd2cf
5 changed files with 233 additions and 1 deletions
101
commands/notify/sql.go
Normal file
101
commands/notify/sql.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package notify
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var notifySetReminder *sql.Stmt
|
||||
var notifyGetReminders *sql.Stmt
|
||||
var notifyGetReminderDetails *sql.Stmt
|
||||
var notifyDeleteReminder *sql.Stmt
|
||||
|
||||
func InitDB() {
|
||||
const dbPath string = "./bloater.db"
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to open sqlite database: %v\n", err)
|
||||
}
|
||||
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS notify (updateID INTEGER,notifyTime INTEGER,chatID INTEGER, userID INTEGER,messageToReplyToID INTEGER,notifyText TEXT)")
|
||||
if err != nil {
|
||||
log.Panicf("Failed to create table: %v\n", err)
|
||||
}
|
||||
log.Println("[notify.sql] Created sqlite table in database at " + dbPath)
|
||||
|
||||
notifySetReminder, err = db.Prepare("INSERT OR REPLACE INTO notify (updateID,notifyTime,chatID, userID,messageToReplyToID,notifyText) values (?,?,?,?,?,?)")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql insert: %v\n", err)
|
||||
}
|
||||
|
||||
notifyGetReminders, err = db.Prepare("SELECT updateID,notifyTime FROM notify")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
}
|
||||
|
||||
notifyGetReminderDetails, err = db.Prepare("SELECT chatID, userID,messageToReplyToID,notifyText FROM notify WHERE updateID=?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
}
|
||||
|
||||
notifyDeleteReminder, err = db.Prepare("DELETE FROM notify WHERE updateID=?")
|
||||
|
||||
if err != nil {
|
||||
log.Panicf("Failed to prepare sql select: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func sqlDeleteReminder(updateID int) {
|
||||
_, err := notifyDeleteReminder.Exec(updateID)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to delete reminder: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func sqlAddReminder(reminder reminderConfig) {
|
||||
_, err := notifySetReminder.Exec(reminder.updateID, reminder.notifyTime, reminder.chatID, reminder.userID, reminder.messageToReplyToID, reminder.reminderText)
|
||||
if err != nil {
|
||||
log.Panicf("Failed to set add reminder: %v\n", err)
|
||||
}
|
||||
log.Println("[notify.sql] Set new reminder for " + strconv.FormatInt(reminder.userID, 10) + " in chat " + strconv.FormatInt(reminder.chatID, 10))
|
||||
}
|
||||
|
||||
func sqlGetReminders() (reminders map[int]int64) {
|
||||
rows, err := notifyGetReminders.Query()
|
||||
if err != nil {
|
||||
log.Panicf("Failed to query reminders: %v\n", err)
|
||||
}
|
||||
|
||||
reminders = make(map[int]int64)
|
||||
var c int
|
||||
var b int64
|
||||
for rows.Next() {
|
||||
switch err := rows.Scan(&c, &b); err {
|
||||
case nil:
|
||||
reminders[c] = b
|
||||
default:
|
||||
log.Panicf("Failed to query gaypoints: %v\n", err)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return reminders
|
||||
}
|
||||
|
||||
func sqlGetReminderDetails(updateID int) (reminder reminderConfig) {
|
||||
row := notifyGetReminderDetails.QueryRow(updateID)
|
||||
|
||||
switch err := row.Scan(&reminder.chatID, &reminder.userID, &reminder.messageToReplyToID, &reminder.reminderText); err {
|
||||
case nil:
|
||||
break
|
||||
default:
|
||||
log.Panicf("Failed to get reminder details: %v\n", err)
|
||||
|
||||
}
|
||||
log.Println("[notify.sql] Got reminder details for update ID" + string(rune(updateID)))
|
||||
return reminder
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue