mastodon-group-bot/bot.go

155 lines
3.9 KiB
Go
Raw Normal View History

2022-08-22 11:46:49 +00:00
package main
import (
"context"
2022-09-01 11:31:27 +00:00
"crypto/sha512"
2022-08-22 11:46:49 +00:00
"fmt"
"regexp"
"strings"
"github.com/mattn/go-mastodon"
)
2022-09-01 11:31:27 +00:00
func RunBot() {
2022-08-28 16:37:43 +00:00
logger_init()
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 {
2022-08-28 16:37:43 +00:00
ErrorLogger.Println("Streaming")
2022-08-22 11:46:49 +00:00
}
2022-08-26 10:56:21 +00:00
my_account, err := c.GetAccountCurrentUser(ctx)
if err != nil {
2022-08-28 16:37:43 +00:00
ErrorLogger.Println("Fetch account info")
2022-08-26 10:56:21 +00:00
}
followers, err := c.GetAccountFollowers(ctx, my_account.ID, &mastodon.Pagination{Limit: 60})
if err != nil {
2022-08-28 16:37:43 +00:00
ErrorLogger.Println("Fetch followers")
2022-08-26 10:56:21 +00:00
}
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-28 16:37:43 +00:00
if !followed(acct) { // Add to db and post welcome message
InfoLogger.Printf("%s followed", acct)
2022-09-01 11:31:27 +00:00
add_to_db(acct)
2022-08-28 16:37:43 +00:00
InfoLogger.Printf("%s added to database", acct)
2022-09-06 09:31:05 +00:00
message := fmt.Sprintf("%s @%s", Conf.WelcomeMessage, acct)
2022-08-28 16:37:43 +00:00
err := postToot(message, "public")
if err != nil {
ErrorLogger.Println("Post welcome message")
}
InfoLogger.Printf("%s was welcomed", acct)
2022-08-25 19:23:44 +00:00
}
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
2022-09-01 11:31:27 +00:00
content := notif.Status.Content
tooturl := notif.Status.URL
2022-08-25 19:23:44 +00:00
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-09-01 11:31:27 +00:00
// Duplicate protection
content_hash := sha512.New()
content_hash.Write([]byte(content))
hash := fmt.Sprintf("%x", content_hash.Sum(nil))
if !check_msg_hash(hash) {
save_msg_hash(hash)
InfoLogger.Printf("Hash of %s added to database", tooturl)
} else {
WarnLogger.Printf("%s is a duplicate and not boosted", tooturl)
break
}
// Add to db if needed
if !followed(acct) {
add_to_db(acct)
2022-08-28 16:37:43 +00:00
InfoLogger.Printf("%s added to database", acct)
2022-08-25 19:23:44 +00:00
}
2022-09-01 11:31:27 +00:00
2022-09-06 09:31:05 +00:00
// Message order
if check_order(acct) < Conf.Order_limit {
if check_ticket(acct) > 0 { // Message limit
take_ticket(acct)
InfoLogger.Printf("Ticket of %s was taken", acct)
count_order(acct)
InfoLogger.Printf("Order of %s was counted", acct)
c.Reblog(ctx, notif.Status.ID)
InfoLogger.Printf("Toot %s of %s was rebloged", tooturl, acct)
} else {
WarnLogger.Printf("%s haven't tickets", acct)
}
2022-08-28 16:37:43 +00:00
} else {
2022-09-06 09:31:05 +00:00
WarnLogger.Printf("%s order limit", acct)
2022-08-25 19:23:44 +00:00
}
2022-08-28 16:37:43 +00:00
} else {
2022-09-01 11:31:27 +00:00
WarnLogger.Printf("%s is reply and not boosted", tooturl)
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-09-04 11:17:01 +00:00
recmd := regexp.MustCompile(`<[^>]+>`)
2022-09-01 11:31:27 +00:00
command := recmd.ReplaceAllString(content, "")
2022-08-22 11:46:49 +00:00
args := strings.Split(command, " ")
2022-09-04 11:17:01 +00:00
mID := mastodon.ID((args[2]))
2022-08-22 11:46:49 +00:00
2022-09-04 11:17:01 +00:00
if len(args) == 3 {
switch args[1] {
2022-08-22 11:46:49 +00:00
case "unboost":
2022-08-25 19:23:44 +00:00
c.Unreblog(ctx, mID)
2022-09-06 09:31:05 +00:00
WarnLogger.Printf("%s was unrebloged", mID)
2022-08-22 11:46:49 +00:00
case "delete":
2022-08-25 19:23:44 +00:00
c.DeleteStatus(ctx, mID)
2022-09-06 09:31:05 +00:00
WarnLogger.Printf("%s was deleted", mID)
2022-08-22 11:46:49 +00:00
}
}
2022-08-28 16:37:43 +00:00
} else {
2022-09-03 19:16:17 +00:00
continue
2022-08-22 11:46:49 +00:00
}
}
2022-08-28 16:37:43 +00:00
} else {
2022-09-01 11:31:27 +00:00
WarnLogger.Printf("%s is not public toot and not boosted", tooturl)
2022-08-28 16:37:43 +00:00
break
2022-08-22 11:46:49 +00:00
}
}
}
}
}
}