mirror of
https://gitea.phreedom.club/localhost_frssoft/mastodon-group-bot
synced 2024-11-24 04:11:28 +00:00
add logging
This commit is contained in:
parent
d3f2bcbdcd
commit
7e00624577
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
go.mod
|
||||
go.sum
|
||||
*.db
|
||||
*.log
|
||||
/mastodon-group-bot
|
||||
|
|
|
@ -36,13 +36,15 @@ go build
|
|||
```
|
||||
|
||||
# Setup services
|
||||
For first copy config, binary and make dirs
|
||||
For first make dirs, copy config and binary
|
||||
```
|
||||
mkdir /etc/mastodon-group-bot
|
||||
mkdir /var/lib/mastodon-group-bot
|
||||
mkdir /var/log/mastodon-group-bot
|
||||
chown nobody /var/lib/mastodon-group-bot
|
||||
cp mastodon-group-bot /usr/bin/mastodon-group-bot
|
||||
chown nobody /var/log/mastodon-group-bot
|
||||
cp config.json /etc/mastodon-group-bot/config.json
|
||||
cp mastodon-group-bot /usr/bin/mastodon-group-bot
|
||||
```
|
||||
|
||||
## Systemd
|
||||
|
@ -57,5 +59,5 @@ cp ./services/openrc/mastodon-group-bot /etc/init.d/mastodon-group-bot
|
|||
|
||||
# Usage
|
||||
```
|
||||
mastodon-group-bot -config <path> -db <path>
|
||||
mastodon-group-bot -config <path> -db <path> -log <path>
|
||||
```
|
48
bot.go
48
bot.go
|
@ -3,14 +3,15 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/mattn/go-mastodon"
|
||||
)
|
||||
|
||||
func run_bot(Conf Config, DB string) {
|
||||
func RunBot(Conf Config) {
|
||||
logger_init()
|
||||
|
||||
c := mastodon.NewClient(&mastodon.Config{
|
||||
Server: Conf.Server,
|
||||
ClientID: Conf.ClientID,
|
||||
|
@ -21,16 +22,16 @@ func run_bot(Conf Config, DB string) {
|
|||
ctx := context.Background()
|
||||
events, err := c.StreamingUser(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Streaming")
|
||||
}
|
||||
|
||||
my_account, err := c.GetAccountCurrentUser(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Fetch account info")
|
||||
}
|
||||
followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Fetch followers")
|
||||
}
|
||||
|
||||
// Run bot
|
||||
|
@ -55,10 +56,18 @@ func run_bot(Conf Config, DB string) {
|
|||
// New follower
|
||||
if notif.Type == "follow" {
|
||||
acct := notif.Account.Acct
|
||||
if !followed(acct, DB) { // Add to db and post welcome message
|
||||
add_to_db(acct, Conf.Max_toots, DB)
|
||||
if !followed(acct) { // Add to db and post welcome message
|
||||
InfoLogger.Printf("%s followed", acct)
|
||||
|
||||
add_to_db(acct, Conf.Max_toots)
|
||||
InfoLogger.Printf("%s added to database", acct)
|
||||
|
||||
var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
|
||||
postToot(message, "public")
|
||||
err := postToot(message, "public")
|
||||
if err != nil {
|
||||
ErrorLogger.Println("Post welcome message")
|
||||
}
|
||||
InfoLogger.Printf("%s was welcomed", acct)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,13 +78,20 @@ func run_bot(Conf Config, DB string) {
|
|||
if acct == string(followers[i].Acct) { // Follow check
|
||||
if notif.Status.Visibility == "public" { // Reblog toot
|
||||
if notif.Status.InReplyToID == nil { // Not boost replies
|
||||
if !followed(acct, DB) { // Add to db if needed
|
||||
add_to_db(acct, Conf.Max_toots, DB)
|
||||
if !followed(acct) { // Add to db if needed
|
||||
add_to_db(acct, Conf.Max_toots)
|
||||
InfoLogger.Printf("%s added to database", acct)
|
||||
}
|
||||
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval, DB) > 0 { // Limit
|
||||
take_ticket(acct, DB)
|
||||
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval) > 0 { // Limit
|
||||
take_ticket(acct)
|
||||
InfoLogger.Printf("Ticket of %s was taken", acct)
|
||||
c.Reblog(ctx, notif.Status.ID)
|
||||
InfoLogger.Printf("Toot %s of %s was rebloged", notif.Status.URL, acct)
|
||||
} else {
|
||||
WarnLogger.Printf("%s haven't tickets", acct)
|
||||
}
|
||||
} else {
|
||||
WarnLogger.Printf("%s is reply and not boosted", notif.Status.URL)
|
||||
}
|
||||
} else if notif.Status.Visibility == "direct" { // Admin commands
|
||||
for y := 0; y < len(Conf.Admins); y++ {
|
||||
|
@ -98,8 +114,12 @@ func run_bot(Conf Config, DB string) {
|
|||
c.AccountUnblock(ctx, mID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
config.go
14
config.go
|
@ -7,6 +7,12 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
ConfPath = flag.String("config", "config.json", "Path to config")
|
||||
DBPath = flag.String("db", "limits.db", "Path to database")
|
||||
LogPath = flag.String("log", "mastodon-group-bot.log", "Path to log")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server string `json:"Server"`
|
||||
ClientID string `json:"ClientID"`
|
||||
|
@ -18,18 +24,16 @@ type Config struct {
|
|||
Admins []string `json:"Admins"`
|
||||
}
|
||||
|
||||
func read_conf() (Config, *string) {
|
||||
ConfPath := flag.String("config", "config.json", "Path to config")
|
||||
DBPath := flag.String("db", "limits.db", "Path to database")
|
||||
func ReadConf() Config {
|
||||
flag.Parse()
|
||||
|
||||
data, err := os.ReadFile(*ConfPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal("Failed to read config")
|
||||
}
|
||||
|
||||
var Conf Config
|
||||
json.Unmarshal(data, &Conf)
|
||||
|
||||
return Conf, DBPath
|
||||
return Conf
|
||||
}
|
||||
|
|
34
limits.go
34
limits.go
|
@ -3,22 +3,22 @@ package main
|
|||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// Init database
|
||||
func init_limit_db(DBPath string) *sql.DB {
|
||||
db, err := sql.Open("sqlite3", DBPath)
|
||||
func init_limit_db() *sql.DB {
|
||||
db, err := sql.Open("sqlite3", *DBPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Open database")
|
||||
}
|
||||
cmd := `CREATE TABLE IF NOT EXISTS Limits (id INTEGER PRIMARY KEY AUTOINCREMENT, acct TEXT, ticket INTEGER, time TEXT)`
|
||||
stat, err := db.Prepare(cmd)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Create database")
|
||||
}
|
||||
stat.Exec()
|
||||
|
||||
|
@ -26,19 +26,19 @@ func init_limit_db(DBPath string) *sql.DB {
|
|||
}
|
||||
|
||||
// Add account to database
|
||||
func add_to_db(acct string, limit uint16, DBPath string) {
|
||||
db := init_limit_db(DBPath)
|
||||
func add_to_db(acct string, limit uint16) {
|
||||
db := init_limit_db()
|
||||
cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)`
|
||||
stat, err := db.Prepare(cmd)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Add account to databse")
|
||||
}
|
||||
stat.Exec(acct, limit)
|
||||
}
|
||||
|
||||
// Take ticket for tooting
|
||||
func take_ticket(acct string, DBPath string) {
|
||||
db := init_limit_db(DBPath)
|
||||
func take_ticket(acct string) {
|
||||
db := init_limit_db()
|
||||
cmd1 := `SELECT ticket FROM Limits WHERE acct = ?`
|
||||
cmd2 := `UPDATE Limits SET ticket = ?, time = ? WHERE acct = ?`
|
||||
|
||||
|
@ -50,7 +50,7 @@ func take_ticket(acct string, DBPath string) {
|
|||
|
||||
stat, err := db.Prepare(cmd2)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Take ticket")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
@ -60,13 +60,13 @@ func take_ticket(acct string, DBPath string) {
|
|||
}
|
||||
|
||||
// Check followed once
|
||||
func followed(acct string, DBPath string) bool {
|
||||
db := init_limit_db(DBPath)
|
||||
func followed(acct string) bool {
|
||||
db := init_limit_db()
|
||||
cmd := `SELECT acct FROM Limits WHERE acct = ?`
|
||||
err := db.QueryRow(cmd, acct).Scan(&acct)
|
||||
if err != nil {
|
||||
if err != sql.ErrNoRows {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Check followed")
|
||||
}
|
||||
|
||||
return false
|
||||
|
@ -76,8 +76,8 @@ func followed(acct string, DBPath string) bool {
|
|||
}
|
||||
|
||||
// Check ticket availability
|
||||
func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath string) uint16 {
|
||||
db := init_limit_db(DBPath)
|
||||
func check_ticket(acct string, ticket uint16, toots_interval uint16) uint16 {
|
||||
db := init_limit_db()
|
||||
cmd1 := `SELECT ticket FROM Limits WHERE acct = ?`
|
||||
cmd2 := `SELECT time FROM Limits WHERE acct = ?`
|
||||
|
||||
|
@ -97,7 +97,7 @@ func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath stri
|
|||
cmd := `UPDATE Limits SET ticket = ? WHERE acct = ?`
|
||||
stat, err := db.Prepare(cmd)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
ErrorLogger.Println("Check ticket availability")
|
||||
}
|
||||
stat.Exec(ticket, acct)
|
||||
|
||||
|
|
22
logger.go
Normal file
22
logger.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
InfoLogger *log.Logger
|
||||
WarnLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
func logger_init() {
|
||||
file, err := os.OpenFile(*LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to read log file")
|
||||
}
|
||||
InfoLogger = log.New(file, "[INFO] ", log.LstdFlags|log.Lshortfile)
|
||||
WarnLogger = log.New(file, "[WARNING] ", log.LstdFlags|log.Lshortfile)
|
||||
ErrorLogger = log.New(file, "[ERROR] ", log.LstdFlags|log.Lshortfile)
|
||||
}
|
4
main.go
4
main.go
|
@ -1,7 +1,7 @@
|
|||
package main
|
||||
|
||||
func main() {
|
||||
config, db := read_conf()
|
||||
config := ReadConf()
|
||||
|
||||
run_bot(config, *db)
|
||||
RunBot(config)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ name=$RC_SVCNAME
|
|||
command="/usr/bin/$name"
|
||||
command_arg1="-config /etc/$name/config.json"
|
||||
command_arg2="-db /var/lib/$name/limits.db"
|
||||
command_arg3="-log /var/log/$name/$name.log"
|
||||
pidfile="/run/$name.pid"
|
||||
user="nobody"
|
||||
description="Mastodon group bot which reposts toots"
|
||||
|
|
|
@ -6,7 +6,7 @@ Wants=network-online.target
|
|||
[Service]
|
||||
Type=simple
|
||||
User=nobody
|
||||
ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot/config.json -db /var/lib/mastodon-group-bot/limits.db
|
||||
ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot/config.json -db /var/lib/mastodon-group-bot/limits.db -log /var/log/mastodon-group-bot/mastodon-group-bot.log
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
Loading…
Reference in a new issue