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 * Limit of toots per hour
* Admin commands * Admin commands
### Admin commands
* unboost \<Toot ID>
* delete \<Toot ID>
* block \<User ID>
* unblock \<User ID>
# Configuration # Configuration
The bot is configured in a JSON file that looks like this: The bot is configured in a JSON file that looks like this:
``` ```
@ -30,10 +36,13 @@ go build
``` ```
# Setup services # 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 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 ## Systemd
@ -48,7 +57,5 @@ cp ./services/openrc/mastodon-group-bot /etc/init.d/mastodon-group-bot
# Usage # Usage
``` ```
Usage of mastodon-group-bot: mastodon-group-bot -config <path> -db <path>
-config string
Path to config (default "config.json")
``` ```

24
bot.go
View file

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

View file

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

View file

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

View file

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

View file

@ -1,14 +1,14 @@
#!/sbin/openrc-run #!/sbin/openrc-run
name=$RC_SVCNAME name=$RC_SVCNAME
command="/usr/bin/$name" command="/usr/bin/$name"
command_args="-config" command_arg1="-config /etc/$name/config.json"
cfgfile="/etc/$name.json" command_arg2="-db /var/lib/$name/limits.db"
pidfile="/run/$name.pid" pidfile="/run/$name.pid"
user="nobody:nobody" user="nobody"
description="Mastodon group bot which reposts toots" description="Mastodon group bot which reposts toots"
start() { start() {
ebegin "Starting $name" 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 $? eend $?
} }
stop() { stop() {

View file

@ -6,7 +6,7 @@ Wants=network-online.target
[Service] [Service]
Type=simple Type=simple
User=nobody 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] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target