Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
228c293b70 | |||
97c9006fef | |||
b542fcf901 | |||
454b42c2d7 | |||
423de55daa | |||
4b7c9a6ee3 | |||
696ab7201c | |||
49aa4fdedb | |||
7c8e662a2e | |||
5e1d6a916e | |||
d8ca637c7d | |||
810d971567 |
11
.github/workflows/publish_release.yaml
vendored
11
.github/workflows/publish_release.yaml
vendored
@ -20,7 +20,7 @@ jobs:
|
|||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: '>=1.18.0'
|
go-version: '>=1.19.0'
|
||||||
- name: Version
|
- name: Version
|
||||||
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
|
||||||
- name: Test
|
- name: Test
|
||||||
@ -28,7 +28,14 @@ jobs:
|
|||||||
make test
|
make test
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
make build
|
make build VERSION="${{ env.RELEASE_VERSION }}"
|
||||||
|
- name: Upload Release Asset
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
if: startsWith(github.ref, 'refs/tags/')
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
build/birdbot
|
||||||
|
sample_config.yaml
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
|
4
.github/workflows/validate.yaml
vendored
4
.github/workflows/validate.yaml
vendored
@ -14,10 +14,10 @@ jobs:
|
|||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: actions/setup-go@v3
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: '>=1.18.0'
|
go-version: '>=1.19.0'
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
make test
|
make test
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
make build
|
make build VERSION="test"
|
4
Makefile
4
Makefile
@ -1,5 +1,7 @@
|
|||||||
PROJECTNAME="Bird Bot"
|
PROJECTNAME="Bird Bot"
|
||||||
PROJECT_BIN="birdbot"
|
PROJECT_BIN="birdbot"
|
||||||
|
VERSION="DEV"
|
||||||
|
BUILD_NUMBER:=$(shell git rev-parse --short HEAD)
|
||||||
|
|
||||||
# Go related variables.
|
# Go related variables.
|
||||||
GOBASE=$(shell pwd)
|
GOBASE=$(shell pwd)
|
||||||
@ -14,7 +16,7 @@ go-full-build: go-clean go-get go-build
|
|||||||
go-build:
|
go-build:
|
||||||
@echo " > Building binary..."
|
@echo " > Building binary..."
|
||||||
@mkdir -p $(GOBIN)
|
@mkdir -p $(GOBIN)
|
||||||
@GOOS=linux CGO_ENABLED=0 go build -o $(GOBIN)/$(PROJECT_BIN) $(GOFILES)
|
@GOOS=linux CGO_ENABLED=0 go build -ldflags "-X github.com/yeslayla/birdbot/app.Version=$(VERSION) -X github.com/yeslayla/birdbot/app.Build=$(BUILD_NUMBER)" -o $(GOBIN)/$(PROJECT_BIN) $(GOFILES)
|
||||||
@chmod 755 $(GOBIN)/$(PROJECT_BIN)
|
@chmod 755 $(GOBIN)/$(PROJECT_BIN)
|
||||||
|
|
||||||
go-generate:
|
go-generate:
|
||||||
|
@ -8,6 +8,7 @@ Bird Bot is a discord bot for managing and organizing events for a small discord
|
|||||||
- Notifying when events are created & cancelled
|
- Notifying when events are created & cancelled
|
||||||
- Delete text channels after events
|
- Delete text channels after events
|
||||||
- Archive text channels after events
|
- Archive text channels after events
|
||||||
|
- Create recurring weekly events
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
177
app/bot.go
177
app/bot.go
@ -5,28 +5,32 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
"github.com/ilyakaznacheev/cleanenv"
|
"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 {
|
type Bot struct {
|
||||||
discord *discordgo.Session
|
session *discord.Discord
|
||||||
|
mastodon *mastodon.Mastodon
|
||||||
|
|
||||||
// Discord Objects
|
// Discord Objects
|
||||||
guildID string
|
guildID string
|
||||||
eventCategoryID string
|
eventCategoryID string
|
||||||
archiveCategoryID string
|
archiveCategoryID string
|
||||||
notificationChannelID string
|
notificationChannelID string
|
||||||
|
|
||||||
// Signal for shutdown
|
|
||||||
stop chan os.Signal
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initalize creates the discord session and registers handlers
|
// Initalize creates the discord session and registers handlers
|
||||||
func (app *Bot) Initialize(config_path string) error {
|
func (app *Bot) Initialize(config_path string) error {
|
||||||
log.Printf("Using config: %s", config_path)
|
log.Printf("Using config: %s", config_path)
|
||||||
cfg := &Config{}
|
cfg := &core.Config{}
|
||||||
|
|
||||||
_, err := os.Stat(config_path)
|
_, err := os.Stat(config_path)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@ -51,160 +55,147 @@ func (app *Bot) Initialize(config_path string) error {
|
|||||||
return fmt.Errorf("discord Guild ID is not set")
|
return fmt.Errorf("discord Guild ID is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Discord Session
|
if cfg.Mastodon.ClientID != "" && cfg.Mastodon.ClientSecret != "" &&
|
||||||
app.discord, err = discordgo.New(fmt.Sprint("Bot ", cfg.Discord.Token))
|
cfg.Mastodon.Username != "" && cfg.Mastodon.Password != "" &&
|
||||||
if err != nil {
|
cfg.Mastodon.Server != "" {
|
||||||
return fmt.Errorf("failed to create Discord session: %v", err)
|
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
|
// Register Event Handlers
|
||||||
app.discord.AddHandler(app.onReady)
|
app.session.OnReady(app.onReady)
|
||||||
app.discord.AddHandler(app.onEventCreate)
|
app.session.OnEventCreate(app.onEventCreate)
|
||||||
app.discord.AddHandler(app.onEventDelete)
|
app.session.OnEventDelete(app.onEventDelete)
|
||||||
app.discord.AddHandler(app.onEventUpdate)
|
app.session.OnEventUpdate(app.onEventUpdate)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run opens the session with Discord until exit
|
// Run opens the session with Discord until exit
|
||||||
func (app *Bot) Run() error {
|
func (app *Bot) Run() error {
|
||||||
|
return app.session.Run()
|
||||||
if err := app.discord.Open(); err != nil {
|
|
||||||
return fmt.Errorf("failed to open Discord session: %v", err)
|
|
||||||
}
|
|
||||||
defer app.discord.Close()
|
|
||||||
|
|
||||||
// Keep alive
|
|
||||||
app.stop = make(chan os.Signal, 1)
|
|
||||||
<-app.stop
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop triggers a graceful shutdown of the app
|
// Stop triggers a graceful shutdown of the app
|
||||||
func (app *Bot) Stop() {
|
func (app *Bot) Stop() {
|
||||||
log.Print("Shuting down...")
|
log.Print("Shuting down...")
|
||||||
app.stop <- os.Kill
|
app.session.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify sends a message to the notification channe;
|
// Notify sends a message to the notification channe;
|
||||||
func (app *Bot) Notify(message string) {
|
func (app *Bot) Notify(message string) {
|
||||||
if app.notificationChannelID == "" {
|
if app.notificationChannelID == "" {
|
||||||
|
log.Println(message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := app.discord.ChannelMessageSend(app.notificationChannelID, message)
|
log.Print("Notification: ", message)
|
||||||
|
|
||||||
log.Println("Notification: ", message)
|
channel := app.session.NewChannelFromID(app.notificationChannelID)
|
||||||
|
if channel == nil {
|
||||||
|
log.Printf("Failed notification: channel was not found with ID '%v'", app.notificationChannelID)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.session.SendMessage(channel, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed notification: ", err)
|
log.Print("Failed notification: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bot) onReady(s *discordgo.Session, r *discordgo.Ready) {
|
func (app *Bot) onReady(d *discord.Discord) {
|
||||||
app.Notify("BirdBot is ready!")
|
app.session.SetStatus(fmt.Sprintf("with fire! (%s)", Version))
|
||||||
log.Print("BirdBot is ready!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bot) onEventCreate(s *discordgo.Session, r *discordgo.GuildScheduledEventCreate) {
|
func (app *Bot) onEventCreate(d *discord.Discord, event *core.Event) {
|
||||||
event := &Event{}
|
|
||||||
event.Name = r.Name
|
|
||||||
event.OrganizerID = r.CreatorID
|
|
||||||
event.DateTime = r.ScheduledStartTime
|
|
||||||
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
|
|
||||||
event.Location = REMOTE_LOCATION
|
|
||||||
} else {
|
|
||||||
event.Location = r.EntityMetadata.Location
|
|
||||||
}
|
|
||||||
log.Print("Event Created: '", event.Name, "':'", event.Location, "'")
|
log.Print("Event Created: '", event.Name, "':'", event.Location, "'")
|
||||||
|
|
||||||
channel, err := CreateChannelIfNotExists(s, app.guildID, event.GetChannelName())
|
channel, err := app.session.NewChannelFromName(event.Channel().Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Failed to create channel for event: ", err)
|
log.Print("Failed to create channel for event: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if app.eventCategoryID != "" {
|
if app.eventCategoryID != "" {
|
||||||
if _, err = s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{
|
err = app.session.MoveChannelToCategory(channel, app.eventCategoryID)
|
||||||
ParentID: app.eventCategoryID,
|
if err != nil {
|
||||||
}); err != nil {
|
|
||||||
log.Printf("Failed to move channel to events category '%s': %v", channel.Name, err)
|
log.Printf("Failed to move channel to events category '%s': %v", channel.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", app.guildID, r.ID)
|
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.OrganizerID, event.Name, eventURL))
|
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(s *discordgo.Session, r *discordgo.GuildScheduledEventDelete) {
|
func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
|
||||||
|
|
||||||
// Create Event Object
|
_, err := app.session.DeleteChannel(event.Channel())
|
||||||
event := &Event{}
|
|
||||||
event.Name = r.Name
|
|
||||||
event.OrganizerID = r.CreatorID
|
|
||||||
event.DateTime = r.ScheduledStartTime
|
|
||||||
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
|
|
||||||
event.Location = REMOTE_LOCATION
|
|
||||||
} else {
|
|
||||||
event.Location = r.EntityMetadata.Location
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := DeleteChannel(app.discord, app.guildID, event.GetChannelName())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Failed to create channel for event: ", err)
|
log.Print("Failed to create channel for event: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Notify(fmt.Sprintf("<@%s> cancelled '%s' on %s, %d!", event.OrganizerID, event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
|
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(s *discordgo.Session, r *discordgo.GuildScheduledEventUpdate) {
|
func (app *Bot) onEventUpdate(d *discord.Discord, event *core.Event) {
|
||||||
|
|
||||||
// Create Event Object
|
|
||||||
event := &Event{}
|
|
||||||
event.Name = r.Name
|
|
||||||
event.OrganizerID = r.CreatorID
|
|
||||||
event.DateTime = r.ScheduledStartTime
|
|
||||||
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
|
|
||||||
event.Location = REMOTE_LOCATION
|
|
||||||
} else {
|
|
||||||
event.Location = r.EntityMetadata.Location
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pass event onwards
|
// Pass event onwards
|
||||||
switch r.Status {
|
if event.Completed {
|
||||||
case discordgo.GuildScheduledEventStatusCompleted:
|
app.onEventComplete(d, event)
|
||||||
app.onEventComplete(s, event)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *Bot) onEventComplete(s *discordgo.Session, event *Event) {
|
func (app *Bot) onEventComplete(d *discord.Discord, event *core.Event) {
|
||||||
channel_name := event.GetChannelName()
|
|
||||||
|
channel := event.Channel()
|
||||||
|
|
||||||
if app.archiveCategoryID != "" {
|
if app.archiveCategoryID != "" {
|
||||||
|
|
||||||
// Get Channel ID
|
if err := app.session.MoveChannelToCategory(channel, app.archiveCategoryID); err != nil {
|
||||||
id, err := GetChannelID(s, app.guildID, channel_name)
|
log.Print("Failed to move channel to archive category: ", err)
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to archive channel: %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move to archive category
|
if err := app.session.ArchiveChannel(channel); err != nil {
|
||||||
if _, err := s.ChannelEdit(id, &discordgo.ChannelEdit{
|
log.Print("Failed to archive channel: ", err)
|
||||||
ParentID: app.archiveCategoryID,
|
|
||||||
}); err != nil {
|
|
||||||
log.Printf("Failed to move channel to archive category: %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Archived channel: '%s'", channel_name)
|
log.Printf("Archived channel: '%s'", channel.Name)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Delete Channel
|
// Delete Channel
|
||||||
_, err := DeleteChannel(s, app.guildID, channel_name)
|
_, err := app.session.DeleteChannel(channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("Failed to delete channel: ", err)
|
log.Print("Failed to delete channel: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Deleted channel: '%s'", channel_name)
|
log.Printf("Deleted channel: '%s'", channel.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(strings.ToLower(event.Description), "recurring weekly") {
|
||||||
|
startTime := event.DateTime.AddDate(0, 0, 7)
|
||||||
|
finishTime := event.CompleteTime.AddDate(0, 0, 7)
|
||||||
|
nextEvent := event
|
||||||
|
nextEvent.DateTime = startTime
|
||||||
|
nextEvent.CompleteTime = finishTime
|
||||||
|
|
||||||
|
if err := app.session.CreateEvent(nextEvent); err != nil {
|
||||||
|
log.Print("Failed to create recurring event: ", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,68 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateChannelIfNotExists(discord *discordgo.Session, guildID string, channel_name string) (*discordgo.Channel, error) {
|
|
||||||
|
|
||||||
// Grab channels to query
|
|
||||||
channels, err := discord.GuildChannels(guildID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to list channels when creating new channel: '%s': %v", channel_name, err)
|
|
||||||
}
|
|
||||||
for _, channel := range channels {
|
|
||||||
|
|
||||||
// Found channel!
|
|
||||||
if channel.Name == channel_name {
|
|
||||||
log.Printf("Tried to create channel, but it already exists '%s'", channel_name)
|
|
||||||
return channel, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since a channel was not found, create one
|
|
||||||
channel, err := discord.GuildChannelCreate(guildID, channel_name, discordgo.ChannelTypeGuildText)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to created channel '%s': %v", channel_name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Created channel: '%s'", channel_name)
|
|
||||||
return channel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteChannel(discord *discordgo.Session, guildID string, channel_name string) (bool, error) {
|
|
||||||
|
|
||||||
channels, err := discord.GuildChannels(guildID)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("failed to list channels when deleting channel: '%s': %v", channel_name, err)
|
|
||||||
}
|
|
||||||
for _, channel := range channels {
|
|
||||||
if channel.Name == channel_name {
|
|
||||||
_, err = discord.ChannelDelete(channel.ID)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("failed to delete channel: %v", err)
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Tried to delete channel, but it didn't exist '%s'", channel_name)
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetChannelID(discord *discordgo.Session, guildID string, channel_name string) (string, error) {
|
|
||||||
channels, err := discord.GuildChannels(guildID)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to list channels when getting channel id: '%s': %v", channel_name, err)
|
|
||||||
}
|
|
||||||
for _, channel := range channels {
|
|
||||||
if channel.Name == channel_name {
|
|
||||||
return channel.ID, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("failed to get channel id for '%s': channel not found", channel_name)
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Discord DiscordConfig `yaml:"discord"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DiscordConfig struct {
|
|
||||||
Token string `yaml:"token" env:"DISCORD_TOKEN"`
|
|
||||||
GuildID string `yaml:"guild_id" env:"DISCORD_GUILD_ID"`
|
|
||||||
|
|
||||||
EventCategory string `yaml:"event_category" env:"DISCORD_EVENT_CATEGORY"`
|
|
||||||
ArchiveCategory string `yaml:"archive_category" env:"DISCORD_ARCHIVE_CATEGORY"`
|
|
||||||
NotificationChannel string `yaml:"notification_channel" env:"DISCORD_NOTIFICATION_CHANNEL"`
|
|
||||||
}
|
|
7
core/channel.go
Normal file
7
core/channel.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
type Channel struct {
|
||||||
|
Name string
|
||||||
|
ID string
|
||||||
|
Verified bool
|
||||||
|
}
|
23
core/configuration.go
Normal file
23
core/configuration.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Discord DiscordConfig `yaml:"discord"`
|
||||||
|
Mastodon MastodonConfig `yaml:"mastodon"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DiscordConfig struct {
|
||||||
|
Token string `yaml:"token" env:"DISCORD_TOKEN"`
|
||||||
|
GuildID string `yaml:"guild_id" env:"DISCORD_GUILD_ID"`
|
||||||
|
|
||||||
|
EventCategory string `yaml:"event_category" env:"DISCORD_EVENT_CATEGORY"`
|
||||||
|
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"`
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package app
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -11,31 +11,43 @@ const REMOTE_LOCATION string = "online"
|
|||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Name string
|
Name string
|
||||||
|
ID string
|
||||||
Location string
|
Location string
|
||||||
|
Completed bool
|
||||||
DateTime time.Time
|
DateTime time.Time
|
||||||
|
CompleteTime time.Time
|
||||||
|
Description string
|
||||||
|
Image string
|
||||||
|
|
||||||
OrganizerID string
|
Organizer *User
|
||||||
}
|
}
|
||||||
|
|
||||||
func (event *Event) GetChannelName() string {
|
// Channel returns a channel object associated with an event
|
||||||
|
func (event *Event) Channel() *Channel {
|
||||||
|
|
||||||
month := event.GetMonthPrefix()
|
month := event.GetMonthPrefix()
|
||||||
day := event.DateTime.Day()
|
day := event.DateTime.Day()
|
||||||
city := event.GetCityFromLocation()
|
city := event.GetCityFromLocation()
|
||||||
|
year := event.DateTime.Year()
|
||||||
|
|
||||||
channel := fmt.Sprint(month, "-", day, city, "-", event.Name)
|
channel := fmt.Sprint(month, "-", day, city, "-", event.Name, "-", year)
|
||||||
channel = strings.ReplaceAll(channel, " ", "-")
|
channel = strings.ReplaceAll(channel, " ", "-")
|
||||||
channel = strings.ToLower(channel)
|
channel = strings.ToLower(channel)
|
||||||
|
|
||||||
re, _ := regexp.Compile(`[^\w\-]`)
|
re, _ := regexp.Compile(`[^\w\-]`)
|
||||||
channel = re.ReplaceAllString(channel, "")
|
channel = re.ReplaceAllString(channel, "")
|
||||||
|
|
||||||
return channel
|
return &Channel{
|
||||||
|
Name: channel,
|
||||||
|
Verified: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetCityFromLocation returns the city name of an event's location
|
||||||
func (event *Event) GetCityFromLocation() string {
|
func (event *Event) GetCityFromLocation() string {
|
||||||
|
|
||||||
if event.Location == REMOTE_LOCATION {
|
if event.Location == REMOTE_LOCATION {
|
||||||
return REMOTE_LOCATION
|
return fmt.Sprint("-", REMOTE_LOCATION)
|
||||||
}
|
}
|
||||||
parts := strings.Split(event.Location, " ")
|
parts := strings.Split(event.Location, " ")
|
||||||
index := -1
|
index := -1
|
||||||
@ -65,6 +77,7 @@ func (event *Event) GetCityFromLocation() string {
|
|||||||
return fmt.Sprint("-", loc)
|
return fmt.Sprint("-", loc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMonthPrefix returns a month in short form
|
||||||
func (event *Event) GetMonthPrefix() string {
|
func (event *Event) GetMonthPrefix() string {
|
||||||
month := event.DateTime.Month()
|
month := event.DateTime.Month()
|
||||||
data := map[time.Month]string{
|
data := map[time.Month]string{
|
62
core/event_test.go
Normal file
62
core/event_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetChannelName(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// Test Valid Address
|
||||||
|
event := Event{
|
||||||
|
Name: "Hello World",
|
||||||
|
Location: "1234 Place Rd, Ann Arbor, MI 00000",
|
||||||
|
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan-5-ann-arbor-hello-world-2022", event.Channel().Name)
|
||||||
|
|
||||||
|
// Test Unparsable Location
|
||||||
|
// lmanley: Note it'd be nice to expand support for this
|
||||||
|
event = Event{
|
||||||
|
Name: "Hello World",
|
||||||
|
Location: "Michigan Theater, Ann Arbor",
|
||||||
|
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan-5-hello-world-2022", event.Channel().Name)
|
||||||
|
|
||||||
|
// Test Short Location
|
||||||
|
event = Event{
|
||||||
|
Name: "Hello World",
|
||||||
|
Location: "Monroe, MI",
|
||||||
|
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan-5-monroe-hello-world-2022", event.Channel().Name)
|
||||||
|
|
||||||
|
// Test Short Location
|
||||||
|
event = Event{
|
||||||
|
Name: "Hello World",
|
||||||
|
Location: "Monroe St, Monroe , MI",
|
||||||
|
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan-5-monroe-hello-world-2022", event.Channel().Name)
|
||||||
|
|
||||||
|
// Test Remote Event
|
||||||
|
event = Event{
|
||||||
|
Name: "Hello World",
|
||||||
|
Location: REMOTE_LOCATION,
|
||||||
|
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan-5-online-hello-world-2022", event.Channel().Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMonthPrefix(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
event := Event{
|
||||||
|
DateTime: time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
}
|
||||||
|
assert.Equal("jan", event.GetMonthPrefix())
|
||||||
|
}
|
6
core/pointers.go
Normal file
6
core/pointers.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
// Bool returns a pointer to a bool
|
||||||
|
func Bool(v bool) *bool {
|
||||||
|
return &v
|
||||||
|
}
|
19
core/pointers_test.go
Normal file
19
core/pointers_test.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBool(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// Test const
|
||||||
|
assert.True(*Bool(true))
|
||||||
|
|
||||||
|
// Test var
|
||||||
|
sample := true
|
||||||
|
assert.True(*Bool(sample))
|
||||||
|
|
||||||
|
}
|
16
core/user.go
Normal file
16
core/user.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mention generated a Discord mention string for the user
|
||||||
|
func (user *User) Mention() string {
|
||||||
|
if user == nil {
|
||||||
|
return "<NULL>"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("<@%s>", user.ID)
|
||||||
|
}
|
23
core/user_test.go
Normal file
23
core/user_test.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUserMention(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// Create user object
|
||||||
|
user := &User{
|
||||||
|
ID: "sample_id",
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal("<@sample_id>", user.Mention())
|
||||||
|
|
||||||
|
// Test null user
|
||||||
|
var nullUser *User = nil
|
||||||
|
assert.NotEmpty(nullUser.Mention())
|
||||||
|
|
||||||
|
}
|
146
discord/channel.go
Normal file
146
discord/channel.go
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/yeslayla/birdbot/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewChannelFromID creates a channel from a given ID
|
||||||
|
func (discord *Discord) NewChannelFromID(ID string) *core.Channel {
|
||||||
|
channel, err := discord.session.Channel(ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &core.Channel{
|
||||||
|
ID: ID,
|
||||||
|
Name: channel.Name,
|
||||||
|
Verified: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewChannelFromName creates a channel object with its name
|
||||||
|
func (discord *Discord) NewChannelFromName(channel_name string) (*core.Channel, error) {
|
||||||
|
|
||||||
|
// Grab channels to query
|
||||||
|
channels, err := discord.session.GuildChannels(discord.guildID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list channels when creating new channel: '%s': %v", channel_name, err)
|
||||||
|
}
|
||||||
|
for _, channel := range channels {
|
||||||
|
|
||||||
|
// Found channel!
|
||||||
|
if channel.Name == channel_name {
|
||||||
|
|
||||||
|
// Channel already exists!
|
||||||
|
return &core.Channel{
|
||||||
|
Name: channel.Name,
|
||||||
|
ID: channel.ID,
|
||||||
|
Verified: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since a channel was not found, create one
|
||||||
|
channel, err := discord.session.GuildChannelCreate(discord.guildID, channel_name, discordgo.ChannelTypeGuildText)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to created channel '%s': %v", channel_name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Created channel: '%s'", channel_name)
|
||||||
|
return &core.Channel{
|
||||||
|
Name: channel.Name,
|
||||||
|
ID: channel.ID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVerifiedChannel looks up channel data and returns a verified objec
|
||||||
|
func (discord *Discord) GetVerifiedChannel(channel *core.Channel) *core.Channel {
|
||||||
|
if channel.ID != "" {
|
||||||
|
return discord.NewChannelFromID(channel.ID)
|
||||||
|
}
|
||||||
|
if channel.Name != "" {
|
||||||
|
channel, err := discord.NewChannelFromName(channel.Name)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to verify channel by name: ", err)
|
||||||
|
}
|
||||||
|
return channel
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteChannel deletes a channel
|
||||||
|
func (discord *Discord) DeleteChannel(channel *core.Channel) (bool, error) {
|
||||||
|
if channel.Verified == false {
|
||||||
|
channel = discord.GetVerifiedChannel(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := discord.session.ChannelDelete(channel.ID)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to delete channel: %v", err)
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getChannelID returns a channel ID from its name
|
||||||
|
func (discord *Discord) getChannelID(channel_name string) (string, error) {
|
||||||
|
|
||||||
|
// Get list of all channels
|
||||||
|
channels, err := discord.session.GuildChannels(discord.guildID)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to list channels when getting channel id: '%s': %v", channel_name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through to find channel
|
||||||
|
for _, ch := range channels {
|
||||||
|
|
||||||
|
// Find and return ID!
|
||||||
|
if ch.Name == channel_name {
|
||||||
|
return ch.ID, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("failed to get channel id for '%s': channel not found", channel_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage sends a message to a given channel
|
||||||
|
func (discord *Discord) SendMessage(channel *core.Channel, message string) error {
|
||||||
|
if channel.Verified == false {
|
||||||
|
channel = discord.GetVerifiedChannel(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := discord.session.ChannelMessageSend(channel.ID, message)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveChannelToCategory places a channel in a given category
|
||||||
|
func (discord *Discord) MoveChannelToCategory(channel *core.Channel, categoryID string) error {
|
||||||
|
if channel.Verified == false {
|
||||||
|
channel = discord.GetVerifiedChannel(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move to archive category
|
||||||
|
if _, err := discord.session.ChannelEdit(channel.ID, &discordgo.ChannelEdit{
|
||||||
|
ParentID: categoryID,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("failed to move channel to archive category: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArchiveChannel archives a channel
|
||||||
|
func (discord *Discord) ArchiveChannel(channel *core.Channel) error {
|
||||||
|
if channel.Verified == false {
|
||||||
|
channel = discord.GetVerifiedChannel(channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := discord.session.ChannelEdit(channel.ID, &discordgo.ChannelEdit{
|
||||||
|
Archived: core.Bool(true),
|
||||||
|
})
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
103
discord/discord.go
Normal file
103
discord/discord.go
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/yeslayla/birdbot/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Discord struct {
|
||||||
|
mock.Mock
|
||||||
|
|
||||||
|
guildID string
|
||||||
|
session *discordgo.Session
|
||||||
|
|
||||||
|
// Signal for shutdown
|
||||||
|
stop chan os.Signal
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new Discord session
|
||||||
|
func New(guildID string, token string) *Discord {
|
||||||
|
|
||||||
|
// Create Discord Session
|
||||||
|
session, err := discordgo.New(fmt.Sprint("Bot ", token))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create Discord session: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Discord{
|
||||||
|
session: session,
|
||||||
|
guildID: guildID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run opens the Discod session until exit
|
||||||
|
func (discord *Discord) Run() error {
|
||||||
|
|
||||||
|
if err := discord.session.Open(); err != nil {
|
||||||
|
return fmt.Errorf("failed to open Discord session: %v", err)
|
||||||
|
}
|
||||||
|
defer discord.session.Close()
|
||||||
|
|
||||||
|
// Keep alive
|
||||||
|
discord.stop = make(chan os.Signal, 1)
|
||||||
|
signal.Notify(discord.stop, os.Interrupt)
|
||||||
|
<-discord.stop
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop tells the Discord session to exit
|
||||||
|
func (discord *Discord) Stop() {
|
||||||
|
discord.stop <- os.Kill
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnReady registers a handler for when the Discord session is ready
|
||||||
|
func (discord *Discord) OnReady(handler func(*Discord)) {
|
||||||
|
discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
|
||||||
|
handler(discord)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnEventCreate registers a handler when a guild scheduled event is created
|
||||||
|
func (discord *Discord) OnEventCreate(handler func(*Discord, *core.Event)) {
|
||||||
|
discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.GuildScheduledEventCreate) {
|
||||||
|
if r.GuildID != discord.guildID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
event := NewEvent(r.GuildScheduledEvent)
|
||||||
|
handler(discord, event)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnEventDelete registers a handler when a guild scheduled event is deleted
|
||||||
|
func (discord *Discord) OnEventDelete(handler func(*Discord, *core.Event)) {
|
||||||
|
discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.GuildScheduledEventDelete) {
|
||||||
|
if r.GuildID != discord.guildID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
event := NewEvent(r.GuildScheduledEvent)
|
||||||
|
handler(discord, event)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnEventUpdate registers a handler when a guild scheduled event is updated
|
||||||
|
func (discord *Discord) OnEventUpdate(handler func(*Discord, *core.Event)) {
|
||||||
|
discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.GuildScheduledEventUpdate) {
|
||||||
|
if r.GuildID != discord.guildID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
event := NewEvent(r.GuildScheduledEvent)
|
||||||
|
handler(discord, event)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (discord *Discord) SetStatus(status string) {
|
||||||
|
if err := discord.session.UpdateGameStatus(0, status); err != nil {
|
||||||
|
log.Fatal("Failed to update status: ", err)
|
||||||
|
}
|
||||||
|
}
|
62
discord/event.go
Normal file
62
discord/event.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/yeslayla/birdbot/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewEvent converts a discordgo.GuildScheduledEvent to birdbot event
|
||||||
|
func NewEvent(guildEvent *discordgo.GuildScheduledEvent) *core.Event {
|
||||||
|
event := &core.Event{
|
||||||
|
Name: guildEvent.Name,
|
||||||
|
Description: guildEvent.Description,
|
||||||
|
ID: guildEvent.ID,
|
||||||
|
Organizer: &core.User{
|
||||||
|
ID: guildEvent.CreatorID,
|
||||||
|
},
|
||||||
|
DateTime: guildEvent.ScheduledStartTime,
|
||||||
|
Image: guildEvent.Image,
|
||||||
|
}
|
||||||
|
|
||||||
|
if guildEvent.ScheduledEndTime != nil {
|
||||||
|
event.CompleteTime = *guildEvent.ScheduledEndTime
|
||||||
|
} else {
|
||||||
|
year, month, day := guildEvent.ScheduledStartTime.Date()
|
||||||
|
event.CompleteTime = time.Date(year, month, day, 0, 0, 0, 0, guildEvent.ScheduledStartTime.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
event.Completed = guildEvent.Status == discordgo.GuildScheduledEventStatusCompleted
|
||||||
|
|
||||||
|
if guildEvent.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
|
||||||
|
event.Location = core.REMOTE_LOCATION
|
||||||
|
} else {
|
||||||
|
event.Location = guildEvent.EntityMetadata.Location
|
||||||
|
}
|
||||||
|
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
func (discord *Discord) CreateEvent(event *core.Event) error {
|
||||||
|
|
||||||
|
params := &discordgo.GuildScheduledEventParams{
|
||||||
|
Name: event.Name,
|
||||||
|
Description: event.Description,
|
||||||
|
ScheduledStartTime: &event.DateTime,
|
||||||
|
ScheduledEndTime: &event.CompleteTime,
|
||||||
|
Image: event.Image,
|
||||||
|
EntityType: discordgo.GuildScheduledEventEntityTypeExternal,
|
||||||
|
PrivacyLevel: discordgo.GuildScheduledEventPrivacyLevelGuildOnly,
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Location != "" {
|
||||||
|
params.EntityMetadata = &discordgo.GuildScheduledEventEntityMetadata{
|
||||||
|
Location: event.Location,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := discord.session.GuildScheduledEventCreate(discord.guildID, params)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
20
discord/user.go
Normal file
20
discord/user.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/yeslayla/birdbot/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewUser creates a new user object from a discordgo.User object
|
||||||
|
func NewUser(user *discordgo.User) *core.User {
|
||||||
|
if user == nil {
|
||||||
|
log.Print("Cannot user object, user is nil!")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &core.User{
|
||||||
|
ID: user.ID,
|
||||||
|
}
|
||||||
|
}
|
17
go.mod
17
go.mod
@ -1,13 +1,22 @@
|
|||||||
module github.com/manleydev/bird-bot
|
module github.com/yeslayla/birdbot
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bwmarrin/discordgo v0.26.1
|
||||||
|
github.com/ilyakaznacheev/cleanenv v1.4.0
|
||||||
|
github.com/stretchr/testify v1.8.1
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.2.1 // indirect
|
github.com/BurntSushi/toml v1.2.1 // indirect
|
||||||
github.com/bwmarrin/discordgo v0.26.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/ilyakaznacheev/cleanenv v1.4.0 // indirect
|
|
||||||
github.com/joho/godotenv v1.4.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/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
21
go.sum
21
go.sum
@ -3,12 +3,31 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak
|
|||||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
|
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
|
||||||
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
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 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
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 h1:Gvwxt6wAPUo9OOxyp5Xz9eqhLsAey4AtbCF5zevDnvs=
|
||||||
github.com/ilyakaznacheev/cleanenv v1.4.0/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
|
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 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
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=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
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 h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
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=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
@ -17,7 +36,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
|
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
|
||||||
|
20
main.go
20
main.go
@ -2,17 +2,33 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
"github.com/manleydev/bird-bot/app"
|
"github.com/yeslayla/birdbot/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
configDir, _ := os.UserConfigDir()
|
||||||
|
|
||||||
|
defaultConfigPath := path.Join(configDir, "birdbot", "config.yaml")
|
||||||
|
|
||||||
var config_file string
|
var config_file string
|
||||||
flag.StringVar(&config_file, "c", "birdbot.yaml", "Path to config file")
|
var version bool
|
||||||
|
flag.StringVar(&config_file, "c", defaultConfigPath, "Path to config file")
|
||||||
|
flag.BoolVar(&version, "v", false, "List version")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if version {
|
||||||
|
fmt.Printf("BirdBot %s (%s)\n", app.Version, app.Build)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bot := app.NewBot()
|
bot := app.NewBot()
|
||||||
|
|
||||||
if err := bot.Initialize(config_file); err != nil {
|
if err := bot.Initialize(config_file); err != nil {
|
||||||
log.Fatal("Failed to initialize: ", err)
|
log.Fatal("Failed to initialize: ", err)
|
||||||
}
|
}
|
||||||
|
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: ""
|
event_category: ""
|
||||||
archive_category: ""
|
archive_category: ""
|
||||||
notification_channel: ""
|
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