Mastodon support
This commit is contained in:
		
							
								
								
									
										25
									
								
								app/bot.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								app/bot.go
									
									
									
									
									
								
							@ -9,13 +9,15 @@ import (
 | 
			
		||||
	"github.com/ilyakaznacheev/cleanenv"
 | 
			
		||||
	"github.com/yeslayla/birdbot/core"
 | 
			
		||||
	"github.com/yeslayla/birdbot/discord"
 | 
			
		||||
	"github.com/yeslayla/birdbot/mastodon"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var Version string
 | 
			
		||||
var Build string
 | 
			
		||||
 | 
			
		||||
type Bot struct {
 | 
			
		||||
	session *discord.Discord
 | 
			
		||||
	session  *discord.Discord
 | 
			
		||||
	mastodon *mastodon.Mastodon
 | 
			
		||||
 | 
			
		||||
	// Discord Objects
 | 
			
		||||
	guildID               string
 | 
			
		||||
@ -52,6 +54,13 @@ func (app *Bot) Initialize(config_path string) error {
 | 
			
		||||
		return fmt.Errorf("discord Guild ID is not set")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if cfg.Mastodon.ClientID != "" && cfg.Mastodon.ClientSecret != "" &&
 | 
			
		||||
		cfg.Mastodon.Username != "" && cfg.Mastodon.Password != "" &&
 | 
			
		||||
		cfg.Mastodon.Server != "" {
 | 
			
		||||
		app.mastodon = mastodon.NewMastodon(cfg.Mastodon.Server, cfg.Mastodon.ClientID, cfg.Mastodon.ClientSecret,
 | 
			
		||||
			cfg.Mastodon.Username, cfg.Mastodon.Password)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	app.session = discord.New(app.guildID, cfg.Discord.Token)
 | 
			
		||||
 | 
			
		||||
	// Register Event Handlers
 | 
			
		||||
@ -116,6 +125,13 @@ func (app *Bot) onEventCreate(d *discord.Discord, event *core.Event) {
 | 
			
		||||
 | 
			
		||||
	eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", app.guildID, event.ID)
 | 
			
		||||
	app.Notify(fmt.Sprintf("%s is organizing an event '%s': %s", event.Organizer.Mention(), event.Name, eventURL))
 | 
			
		||||
 | 
			
		||||
	if app.mastodon != nil {
 | 
			
		||||
		err = app.mastodon.Toot(fmt.Sprintf("A new event has been organized '%s': %s", event.Name, eventURL))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println("Failed to send Mastodon Toot:", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
 | 
			
		||||
@ -126,6 +142,13 @@ func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	app.Notify(fmt.Sprintf("%s cancelled '%s' on %s, %d!", event.Organizer.Mention(), event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
 | 
			
		||||
 | 
			
		||||
	if app.mastodon != nil {
 | 
			
		||||
		err = app.mastodon.Toot(fmt.Sprintf("'%s' cancelled on %s, %d!", event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			fmt.Println("Failed to send Mastodon Toot:", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (app *Bot) onEventUpdate(d *discord.Discord, event *core.Event) {
 | 
			
		||||
 | 
			
		||||
@ -1,7 +1,8 @@
 | 
			
		||||
package core
 | 
			
		||||
 | 
			
		||||
type Config struct {
 | 
			
		||||
	Discord DiscordConfig `yaml:"discord"`
 | 
			
		||||
	Discord  DiscordConfig  `yaml:"discord"`
 | 
			
		||||
	Mastodon MastodonConfig `yaml:"mastodon"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type DiscordConfig struct {
 | 
			
		||||
@ -12,3 +13,11 @@ type DiscordConfig struct {
 | 
			
		||||
	ArchiveCategory     string `yaml:"archive_category" env:"DISCORD_ARCHIVE_CATEGORY"`
 | 
			
		||||
	NotificationChannel string `yaml:"notification_channel" env:"DISCORD_NOTIFICATION_CHANNEL"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type MastodonConfig struct {
 | 
			
		||||
	Server       string `yaml:"server" env:"MASTODON_SERVER"`
 | 
			
		||||
	Username     string `yaml:"user" env:"MASTODON_USER"`
 | 
			
		||||
	Password     string `yaml:"password" env:"MASTODON_PASSWORD"`
 | 
			
		||||
	ClientID     string `yaml:"client_id" env:"MASTODON_CLIENT_ID"`
 | 
			
		||||
	ClientSecret string `yaml:"client_secret" env:"MASTODON_CLIENT_SECRET"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@ -11,10 +11,12 @@ require (
 | 
			
		||||
require (
 | 
			
		||||
	github.com/BurntSushi/toml v1.2.1 // indirect
 | 
			
		||||
	github.com/davecgh/go-spew v1.1.1 // indirect
 | 
			
		||||
	github.com/gorilla/websocket v1.4.2 // indirect
 | 
			
		||||
	github.com/gorilla/websocket v1.5.0 // indirect
 | 
			
		||||
	github.com/joho/godotenv v1.4.0 // indirect
 | 
			
		||||
	github.com/mattn/go-mastodon v0.0.5 // indirect
 | 
			
		||||
	github.com/pmezard/go-difflib v1.0.0 // indirect
 | 
			
		||||
	github.com/stretchr/objx v0.5.0 // indirect
 | 
			
		||||
	github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
 | 
			
		||||
	golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1 // indirect
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										6
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.sum
									
									
									
									
									
								
							@ -8,10 +8,14 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 | 
			
		||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
 | 
			
		||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 | 
			
		||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
 | 
			
		||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 | 
			
		||||
github.com/ilyakaznacheev/cleanenv v1.4.0 h1:Gvwxt6wAPUo9OOxyp5Xz9eqhLsAey4AtbCF5zevDnvs=
 | 
			
		||||
github.com/ilyakaznacheev/cleanenv v1.4.0/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
 | 
			
		||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
 | 
			
		||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 | 
			
		||||
github.com/mattn/go-mastodon v0.0.5 h1:P0e1/R2v3ho6kM7BUW0noQm8gAqHE0p8Gq1TMapIVAc=
 | 
			
		||||
github.com/mattn/go-mastodon v0.0.5/go.mod h1:cg7RFk2pcUfHZw/IvKe1FUzmlq5KnLFqs7eV2PHplV8=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
			
		||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
			
		||||
@ -22,6 +26,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
 | 
			
		||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 | 
			
		||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
 | 
			
		||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 | 
			
		||||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
 | 
			
		||||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
 | 
			
		||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								mastodon/mastodon.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								mastodon/mastodon.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
			
		||||
package mastodon
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"github.com/mattn/go-mastodon"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Mastodon struct {
 | 
			
		||||
	client *mastodon.Client
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewMastodon(server string, clientID string, clientSecret string, username string, password string) *Mastodon {
 | 
			
		||||
	m := &Mastodon{}
 | 
			
		||||
 | 
			
		||||
	m.client = mastodon.NewClient(&mastodon.Config{
 | 
			
		||||
		Server:       server,
 | 
			
		||||
		ClientID:     clientID,
 | 
			
		||||
		ClientSecret: clientSecret,
 | 
			
		||||
	})
 | 
			
		||||
	err := m.client.Authenticate(context.Background(), username, password)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Print("Failed to configure Mastodon: ", err)
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return m
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								mastodon/toot.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								mastodon/toot.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
package mastodon
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
 | 
			
		||||
	"github.com/mattn/go-mastodon"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func (m *Mastodon) Toot(message string) error {
 | 
			
		||||
	_, err := m.client.PostStatus(context.Background(), &mastodon.Toot{
 | 
			
		||||
		Status: message,
 | 
			
		||||
	})
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
@ -9,3 +9,11 @@ discord:
 | 
			
		||||
  event_category: ""
 | 
			
		||||
  archive_category: ""
 | 
			
		||||
  notification_channel: ""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# mastodon:
 | 
			
		||||
#   server: https://mastodon.social
 | 
			
		||||
#   username: my_user
 | 
			
		||||
#   password: secret
 | 
			
		||||
#   client_id: 1234
 | 
			
		||||
#   client_secret: secret2
 | 
			
		||||
		Reference in New Issue
	
	Block a user