mastodon-group-bot/bot.go

130 lines
3.3 KiB
Go
Raw Normal View History

2022-08-22 14:46:49 +03:00
package main
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/mattn/go-mastodon"
)
2022-08-28 19:37:43 +03:00
func RunBot(Conf Config) {
logger_init()
2022-08-22 14:46:49 +03: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 {
2022-08-28 19:37:43 +03:00
ErrorLogger.Println("Streaming")
2022-08-22 14:46:49 +03:00
}
2022-08-26 13:56:21 +03:00
my_account, err := c.GetAccountCurrentUser(ctx)
if err != nil {
2022-08-28 19:37:43 +03:00
ErrorLogger.Println("Fetch account info")
2022-08-26 13:56:21 +03:00
}
followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
if err != nil {
2022-08-28 19:37:43 +03:00
ErrorLogger.Println("Fetch followers")
2022-08-26 13:56:21 +03:00
}
2022-08-22 14:46:49 +03: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 22:23:44 +03:00
acct := notif.Account.Acct
2022-08-28 19:37:43 +03:00
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)
2022-08-25 22:23:44 +03:00
var message = fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
2022-08-28 19:37:43 +03:00
err := postToot(message, "public")
if err != nil {
ErrorLogger.Println("Post welcome message")
}
InfoLogger.Printf("%s was welcomed", acct)
2022-08-25 22:23:44 +03:00
}
2022-08-22 14:46:49 +03:00
}
// Read message
if notif.Type == "mention" {
2022-08-25 22:23:44 +03:00
acct := notif.Status.Account.Acct
for i := 0; i < len(followers); i++ {
if acct == string(followers[i].Acct) { // Follow check
2022-08-22 14:46:49 +03:00
if notif.Status.Visibility == "public" { // Reblog toot
2022-08-25 22:23:44 +03:00
if notif.Status.InReplyToID == nil { // Not boost replies
2022-08-28 19:37:43 +03:00
if !followed(acct) { // Add to db if needed
add_to_db(acct, Conf.Max_toots)
InfoLogger.Printf("%s added to database", acct)
2022-08-25 22:23:44 +03:00
}
2022-08-28 19:37:43 +03:00
if check_ticket(acct, Conf.Max_toots, Conf.Toots_interval) > 0 { // Limit
take_ticket(acct)
InfoLogger.Printf("Ticket of %s was taken", acct)
2022-08-25 22:23:44 +03:00
c.Reblog(ctx, notif.Status.ID)
2022-08-28 19:37:43 +03:00
InfoLogger.Printf("Toot %s of %s was rebloged", notif.Status.URL, acct)
} else {
WarnLogger.Printf("%s haven't tickets", acct)
2022-08-25 22:23:44 +03:00
}
2022-08-28 19:37:43 +03:00
} else {
WarnLogger.Printf("%s is reply and not boosted", notif.Status.URL)
2022-08-22 21:05:40 +03:00
}
2022-08-22 14:46:49 +03:00
} else if notif.Status.Visibility == "direct" { // Admin commands
for y := 0; y < len(Conf.Admins); y++ {
2022-08-25 22:23:44 +03:00
if acct == Conf.Admins[y] {
2022-08-22 14:46:49 +03:00
text := notif.Status.Content
recmd := regexp.MustCompile(`<.*?> `)
command := recmd.ReplaceAllString(text, "")
args := strings.Split(command, " ")
2022-08-25 22:23:44 +03:00
mID := mastodon.ID((args[1]))
2022-08-22 14:46:49 +03:00
if len(args) == 2 {
switch args[0] {
case "unboost":
2022-08-25 22:23:44 +03:00
c.Unreblog(ctx, mID)
2022-08-22 14:46:49 +03:00
case "delete":
2022-08-25 22:23:44 +03:00
c.DeleteStatus(ctx, mID)
2022-08-22 14:46:49 +03:00
case "block":
2022-08-25 22:23:44 +03:00
c.AccountBlock(ctx, mID)
2022-08-22 14:46:49 +03:00
case "unblock":
2022-08-25 22:23:44 +03:00
c.AccountUnblock(ctx, mID)
2022-08-22 14:46:49 +03:00
}
}
2022-08-28 19:37:43 +03:00
} else {
break
2022-08-22 14:46:49 +03:00
}
}
2022-08-28 19:37:43 +03:00
} else {
2022-08-30 12:28:44 +03:00
WarnLogger.Printf("%s is not public toot and not boosted", notif.Status.URL)
2022-08-28 19:37:43 +03:00
break
2022-08-22 14:46:49 +03:00
}
}
}
}
}
}