mastodon-group-bot/bot.go

109 lines
2.5 KiB
Go
Raw Normal View History

2022-08-22 11:46:49 +00:00
package main
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"github.com/mattn/go-mastodon"
)
2022-08-26 10:56:21 +00:00
func run_bot(Conf Config, DB string) {
2022-08-22 11:46:49 +00:00
c := mastodon.NewClient(&mastodon.Config{
Server: Conf.Server,
ClientID: Conf.ClientID,
ClientSecret: Conf.ClientSecret,
AccessToken: Conf.AccessToken,
})
ctx := context.Background()
events, err := c.StreamingUser(ctx)
if err != nil {
log.Fatal(err)
}
2022-08-26 10:56:21 +00:00
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)
}
2022-08-22 11:46:49 +00:00
// Run bot
for {
notifEvent, ok := (<-events).(*mastodon.NotificationEvent)
if !ok {
continue
}
notif := notifEvent.Notification
// Posting function
postToot := func(toot string, vis string) error {
conToot := mastodon.Toot{
Status: toot,
Visibility: vis,
}
_, err := c.PostStatus(ctx, &conToot)
return err
}
// New follower
if notif.Type == "follow" {
2022-08-25 19:23:44 +00:00
acct := notif.Account.Acct
2022-08-26 10:56:21 +00:00
if !followed(acct, DB) { // Add to db and post welcome message
add_to_db(acct, Conf.Max_toots, DB)
2022-08-25 19:23:44 +00:00
var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
postToot(message, "public")
}
2022-08-22 11:46:49 +00:00
}
// Read message
if notif.Type == "mention" {
2022-08-25 19:23:44 +00:00
acct := notif.Status.Account.Acct
for i := 0; i < len(followers); i++ {
if acct == string(followers[i].Acct) { // Follow check
2022-08-22 11:46:49 +00:00
if notif.Status.Visibility == "public" { // Reblog toot
2022-08-25 19:23:44 +00:00
if notif.Status.InReplyToID == nil { // Not boost replies
2022-08-26 10:56:21 +00:00
if !followed(acct, DB) { // Add to db if needed
add_to_db(acct, Conf.Max_toots, DB)
2022-08-25 19:23:44 +00:00
}
2022-08-26 10:56:21 +00:00
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval, DB) > 0 { // Limit
take_ticket(acct, DB)
2022-08-25 19:23:44 +00:00
c.Reblog(ctx, notif.Status.ID)
}
2022-08-22 18:05:40 +00:00
}
2022-08-22 11:46:49 +00:00
} else if notif.Status.Visibility == "direct" { // Admin commands
for y := 0; y < len(Conf.Admins); y++ {
2022-08-25 19:23:44 +00:00
if acct == Conf.Admins[y] {
2022-08-22 11:46:49 +00:00
text := notif.Status.Content
recmd := regexp.MustCompile(`<.*?> `)
command := recmd.ReplaceAllString(text, "")
args := strings.Split(command, " ")
2022-08-25 19:23:44 +00:00
mID := mastodon.ID((args[1]))
2022-08-22 11:46:49 +00:00
if len(args) == 2 {
switch args[0] {
case "unboost":
2022-08-25 19:23:44 +00:00
c.Unreblog(ctx, mID)
2022-08-22 11:46:49 +00:00
case "delete":
2022-08-25 19:23:44 +00:00
c.DeleteStatus(ctx, mID)
2022-08-22 11:46:49 +00:00
case "block":
2022-08-25 19:23:44 +00:00
c.AccountBlock(ctx, mID)
2022-08-22 11:46:49 +00:00
case "unblock":
2022-08-25 19:23:44 +00:00
c.AccountUnblock(ctx, mID)
2022-08-22 11:46:49 +00:00
}
}
}
}
}
}
}
}
}
}