add order limit

This commit is contained in:
fade 2022-09-06 05:31:05 -04:00
parent 8b7e62f070
commit 30b7fddf18
5 changed files with 113 additions and 63 deletions

View file

@ -25,6 +25,7 @@ The bot is configured in a JSON file that looks like this:
"Max_toots": 2, "Max_toots": 2,
"Toots_interval": 12, "Toots_interval": 12,
"Duplicate_buf": 10, "Duplicate_buf": 10,
"Order_limit": 1,
"Admins": ["admin@example.com"] "Admins": ["admin@example.com"]
} }
``` ```

14
bot.go
View file

@ -63,7 +63,7 @@ func RunBot() {
add_to_db(acct) add_to_db(acct)
InfoLogger.Printf("%s added to database", acct) InfoLogger.Printf("%s added to database", acct)
var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct) message := fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
err := postToot(message, "public") err := postToot(message, "public")
if err != nil { if err != nil {
ErrorLogger.Println("Post welcome message") ErrorLogger.Println("Post welcome message")
@ -101,15 +101,23 @@ func RunBot() {
InfoLogger.Printf("%s added to database", acct) InfoLogger.Printf("%s added to database", acct)
} }
// Message limit // Message order
if check_ticket(acct) > 0 { if check_order(acct) < Conf.Order_limit {
if check_ticket(acct) > 0 { // Message limit
take_ticket(acct) take_ticket(acct)
InfoLogger.Printf("Ticket of %s was taken", acct) InfoLogger.Printf("Ticket of %s was taken", acct)
count_order(acct)
InfoLogger.Printf("Order of %s was counted", acct)
c.Reblog(ctx, notif.Status.ID) c.Reblog(ctx, notif.Status.ID)
InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct) InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct)
} else { } else {
WarnLogger.Printf("%s haven't tickets", acct) WarnLogger.Printf("%s haven't tickets", acct)
} }
} else {
WarnLogger.Printf("%s order limit", acct)
}
} else { } else {
WarnLogger.Printf("%s is reply and not boosted", tooturl) WarnLogger.Printf("%s is reply and not boosted", tooturl)
} }

View file

@ -23,7 +23,8 @@ type Config struct {
WelcomeMessage string `json:"WelcomeMessage"` WelcomeMessage string `json:"WelcomeMessage"`
Max_toots uint16 `json:"Max_toots"` Max_toots uint16 `json:"Max_toots"`
Toots_interval uint16 `json:"Toots_interval"` Toots_interval uint16 `json:"Toots_interval"`
Duplicate_buf int `json:"Duplicate_buf"` Duplicate_buf uint16 `json:"Duplicate_buf"`
Order_limit uint16 `json:"Order_limit"`
Admins []string `json:"Admins"` Admins []string `json:"Admins"`
} }

View file

@ -7,5 +7,6 @@
"Max_toots": 2, "Max_toots": 2,
"Toots_interval": 12, "Toots_interval": 12,
"Duplicate_buf": 10, "Duplicate_buf": 10,
"Order_limit": 1,
"Admins": ["admin@example.com"] "Admins": ["admin@example.com"]
} }

143
limits.go
View file

@ -16,18 +16,18 @@ func init_limit_db() *sql.DB {
ErrorLogger.Println("Open database") ErrorLogger.Println("Open database")
} }
cmd1 := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, time TEXT)` cmd1 := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, order_msg INTEGER, time TEXT)`
cmd2 := `CREATE TABLE IF NOT EXISTS MsgHashs (message_hash TEXT)` cmd2 := `CREATE TABLE IF NOT EXISTS MsgHashs (message_hash TEXT)`
stat1, err := db.Prepare(cmd1) stat1, err := db.Prepare(cmd1)
if err != nil { if err != nil {
ErrorLogger.Println("Create database") ErrorLogger.Println("Create database and table Limits")
} }
stat1.Exec() stat1.Exec()
stat2, err := db.Prepare(cmd2) stat2, err := db.Prepare(cmd2)
if err != nil { if err != nil {
ErrorLogger.Println("Create database") ErrorLogger.Println("Create database and table MsgHashs")
} }
stat2.Exec() stat2.Exec()
@ -37,43 +37,12 @@ func init_limit_db() *sql.DB {
// Add account to database // Add account to database
func add_to_db(acct string) { func add_to_db(acct string) {
db := init_limit_db() db := init_limit_db()
cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)` cmd := `INSERT INTO Limits (acct, ticket, order_msg) VALUES (?, ?, ?)`
stat, err := db.Prepare(cmd) stat, err := db.Prepare(cmd)
if err != nil { if err != nil {
ErrorLogger.Println("Add account to databse") ErrorLogger.Println("Add account to databse")
} }
stat.Exec(acct, Conf.Max_toots) stat.Exec(acct, Conf.Max_toots, 0)
}
// Save message hash
func save_msg_hash(hash string) {
db := init_limit_db()
cmd1 := `SELECT COUNT(*) FROM MsgHashs`
cmd2 := `DELETE FROM MsgHashs WHERE ROWID IN (SELECT ROWID FROM MsgHashs LIMIT 1)`
cmd3 := `INSERT INTO MsgHashs (message_hash) VALUES (?)`
var rows int
db.QueryRow(cmd1).Scan(&rows)
if rows >= Conf.Duplicate_buf {
superfluous := rows - Conf.Duplicate_buf
for i := 0; i <= superfluous; i++ {
stat2, err := db.Prepare(cmd2)
if err != nil {
ErrorLogger.Println("Delete message hash from database")
}
stat2.Exec()
}
}
stat1, err := db.Prepare(cmd3)
if err != nil {
ErrorLogger.Println("Add message hash to database")
}
stat1.Exec(hash)
} }
// Check followed once // Check followed once
@ -92,22 +61,6 @@ func followed(acct string) bool {
return true return true
} }
// Check message hash
func check_msg_hash(hash string) bool {
db := init_limit_db()
cmd := `SELECT message_hash FROM MsgHashs WHERE message_hash = ?`
err := db.QueryRow(cmd, hash).Scan(&hash)
if err != nil {
if err != sql.ErrNoRows {
InfoLogger.Println("Check message hash in database")
}
return false
}
return true
}
// Take ticket for tooting // Take ticket for tooting
func take_ticket(acct string) { func take_ticket(acct string) {
db := init_limit_db() db := init_limit_db()
@ -162,3 +115,89 @@ func check_ticket(acct string) uint16 {
return tickets return tickets
} }
// Save message hash
func save_msg_hash(hash string) {
db := init_limit_db()
cmd1 := `SELECT COUNT(*) FROM MsgHashs`
cmd2 := `DELETE FROM MsgHashs WHERE ROWID IN (SELECT ROWID FROM MsgHashs LIMIT 1)`
cmd3 := `INSERT INTO MsgHashs (message_hash) VALUES (?)`
var rows uint16
db.QueryRow(cmd1).Scan(&rows)
if rows >= Conf.Duplicate_buf {
superfluous := rows - Conf.Duplicate_buf
for i := uint16(0); i <= superfluous; i++ {
stat2, err := db.Prepare(cmd2)
if err != nil {
ErrorLogger.Println("Delete message hash from database")
}
stat2.Exec()
}
}
stat1, err := db.Prepare(cmd3)
if err != nil {
ErrorLogger.Println("Add message hash to database")
}
stat1.Exec(hash)
}
// Check message hash
func check_msg_hash(hash string) bool {
db := init_limit_db()
cmd := `SELECT message_hash FROM MsgHashs WHERE message_hash = ?`
err := db.QueryRow(cmd, hash).Scan(&hash)
if err != nil {
if err != sql.ErrNoRows {
InfoLogger.Println("Check message hash in database")
}
return false
}
return true
}
// Count order
func count_order(acct string) {
db := init_limit_db()
cmd1 := `UPDATE Limits SET order_msg = ? WHERE acct != ?`
cmd2 := `SELECT order_msg FROM Limits WHERE acct = ?`
cmd3 := `UPDATE Limits SET order_msg = ? WHERE acct = ?`
stat1, err := db.Prepare(cmd1)
if err != nil {
ErrorLogger.Println("Count order to zero")
}
stat1.Exec(0, acct)
var order uint16
db.QueryRow(cmd2, acct).Scan(&order)
if order < Conf.Order_limit {
order = order + 1
}
stat2, err := db.Prepare(cmd3)
if err != nil {
ErrorLogger.Println("Count order")
}
stat2.Exec(order, acct)
}
// Check order
func check_order(acct string) uint16 {
db := init_limit_db()
cmd := `SELECT order_msg FROM Limits WHERE acct = ?`
var order uint16
db.QueryRow(cmd, acct).Scan(&order)
return order
}