some fixes

This commit is contained in:
fade 2022-08-26 06:56:21 -04:00
parent f013670852
commit d1439f87ac
7 changed files with 49 additions and 35 deletions

View File

@ -7,6 +7,12 @@ This is a bot which implements group functionality in Mastodon.
* Limit of toots per hour
* Admin commands
### Admin commands
* unboost \<Toot ID>
* delete \<Toot ID>
* block \<User ID>
* unblock \<User ID>
# Configuration
The bot is configured in a JSON file that looks like this:
```
@ -30,10 +36,13 @@ go build
```
# Setup services
For first copy config and binary
For first copy config, binary and make dirs
```
mkdir /etc/mastodon-group-bot
mkdir /var/lib/mastodon-group-bot
chown nobody /var/lib/mastodon-group-bot
cp mastodon-group-bot /usr/bin/mastodon-group-bot
cp config.json /etc/mastodon-group-bot.json
cp config.json /etc/mastodon-group-bot/config.json
```
## Systemd
@ -48,7 +57,5 @@ cp ./services/openrc/mastodon-group-bot /etc/init.d/mastodon-group-bot
# Usage
```
Usage of mastodon-group-bot:
-config string
Path to config (default "config.json")
mastodon-group-bot -config <path> -db <path>
```

24
bot.go
View File

@ -10,7 +10,7 @@ import (
"github.com/mattn/go-mastodon"
)
func run_bot(Conf Config) {
func run_bot(Conf Config, DB string) {
c := mastodon.NewClient(&mastodon.Config{
Server: Conf.Server,
ClientID: Conf.ClientID,
@ -24,8 +24,14 @@ func run_bot(Conf Config) {
log.Fatal(err)
}
my_account, _ := c.GetAccountCurrentUser(ctx)
followers, _ := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
my_account, err := c.GetAccountCurrentUser(ctx)
if err != nil {
log.Fatal(err)
}
followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
if err != nil {
log.Fatal(err)
}
// Run bot
for {
@ -49,8 +55,8 @@ func run_bot(Conf Config) {
// New follower
if notif.Type == "follow" {
acct := notif.Account.Acct
if !followed(acct) { // Add to db and post welcome message
add_to_db(acct, Conf.Max_toots)
if !followed(acct, DB) { // Add to db and post welcome message
add_to_db(acct, Conf.Max_toots, DB)
var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
postToot(message, "public")
}
@ -63,11 +69,11 @@ func run_bot(Conf Config) {
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) { // Add to db if needed
add_to_db(acct, Conf.Max_toots)
if !followed(acct, DB) { // Add to db if needed
add_to_db(acct, Conf.Max_toots, DB)
}
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval) > 0 { // Limit
take_ticket(acct)
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval, DB) > 0 { // Limit
take_ticket(acct, DB)
c.Reblog(ctx, notif.Status.ID)
}
}

View File

@ -18,8 +18,9 @@ type Config struct {
Admins []string `json:"Admins"`
}
func read_conf() Config {
func read_conf() (Config, *string) {
ConfPath := flag.String("config", "config.json", "Path to config")
DBPath := flag.String("db", "limits.db", "Path to database")
flag.Parse()
data, err := os.ReadFile(*ConfPath)
@ -30,5 +31,5 @@ func read_conf() Config {
var Conf Config
json.Unmarshal(data, &Conf)
return Conf
return Conf, DBPath
}

View File

@ -10,8 +10,8 @@ import (
)
// Init database
func init_limit_db() *sql.DB {
db, err := sql.Open("sqlite3", "limits.db")
func init_limit_db(DBPath string) *sql.DB {
db, err := sql.Open("sqlite3", DBPath)
if err != nil {
log.Fatal(err)
}
@ -26,8 +26,8 @@ func init_limit_db() *sql.DB {
}
// Add account to database
func add_to_db(acct string, limit uint16) {
db := init_limit_db()
func add_to_db(acct string, limit uint16, DBPath string) {
db := init_limit_db(DBPath)
cmd := `INSERT INTO Limits (acct, ticket) VALUES (?, ?)`
stat, err := db.Prepare(cmd)
if err != nil {
@ -37,8 +37,8 @@ func add_to_db(acct string, limit uint16) {
}
// Take ticket for tooting
func take_ticket(acct string) {
db := init_limit_db()
func take_ticket(acct string, DBPath string) {
db := init_limit_db(DBPath)
cmd1 := `SELECT ticket FROM Limits WHERE acct = ?`
cmd2 := `UPDATE Limits SET ticket = ?, time = ? WHERE acct = ?`
@ -60,8 +60,8 @@ func take_ticket(acct string) {
}
// Check followed once
func followed(acct string) bool {
db := init_limit_db()
func followed(acct string, DBPath string) bool {
db := init_limit_db(DBPath)
cmd := `SELECT acct FROM Limits WHERE acct = ?`
err := db.QueryRow(cmd, acct).Scan(&acct)
if err != nil {
@ -76,8 +76,8 @@ func followed(acct string) bool {
}
// Check ticket availability
func check_ticket(acct string, ticket uint16, toots_interval uint16) uint16 {
db := init_limit_db()
func check_ticket(acct string, ticket uint16, toots_interval uint16, DBPath string) uint16 {
db := init_limit_db(DBPath)
cmd1 := `SELECT ticket FROM Limits WHERE acct = ?`
cmd2 := `SELECT time FROM Limits WHERE acct = ?`

View File

@ -1,7 +1,7 @@
package main
func main() {
config := read_conf()
config, db := read_conf()
run_bot(config)
run_bot(config, *db)
}

View File

@ -1,18 +1,18 @@
#!/sbin/openrc-run
name=$RC_SVCNAME
command="/usr/bin/$name"
command_args="-config"
cfgfile="/etc/$name.json"
command_arg1="-config /etc/$name/config.json"
command_arg2="-db /var/lib/$name/limits.db"
pidfile="/run/$name.pid"
user="nobody:nobody"
user="nobody"
description="Mastodon group bot which reposts toots"
start() {
ebegin "Starting $name"
start-stop-daemon -bm -S -u $user -p $pidfile -x $command -- $command_args $cfgfile
start-stop-daemon -bm -S -u $user -p $pidfile -x $command -- $command_arg1 $command_arg2
eend $?
}
stop() {
ebegin "Stopping $name"
start-stop-daemon -K -p $pidfile
eend $?
}
}

View File

@ -6,7 +6,7 @@ Wants=network-online.target
[Service]
Type=simple
User=nobody
ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot.json
ExecStart=/usr/bin/mastodon-group-bot -config /etc/mastodon-group-bot/config.json -db /var/lib/mastodon-group-bot/limits.db
[Install]
WantedBy=multi-user.target
WantedBy=multi-user.target