Initial commit
This commit is contained in:
commit
9fa4eb31dc
20
.devcontainer/Dockerfile
Normal file
20
.devcontainer/Dockerfile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/go/.devcontainer/base.Dockerfile
|
||||||
|
|
||||||
|
# [Choice] Go version (use -bullseye variants on local arm64/Apple Silicon): 1, 1.19, 1.18, 1-bullseye, 1.19-bullseye, 1.18-bullseye, 1-buster, 1.19-buster, 1.18-buster
|
||||||
|
ARG VARIANT="1.19-bullseye"
|
||||||
|
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
|
||||||
|
|
||||||
|
# [Choice] Node.js version: none, lts/*, 18, 16, 14
|
||||||
|
ARG NODE_VERSION="none"
|
||||||
|
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
|
||||||
|
|
||||||
|
# [Optional] Uncomment this section to install additional OS packages.
|
||||||
|
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||||
|
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
||||||
|
|
||||||
|
# [Optional] Uncomment the next lines to use go get to install anything else you need
|
||||||
|
# USER vscode
|
||||||
|
# RUN go get -x <your-dependency-or-tool>
|
||||||
|
|
||||||
|
# [Optional] Uncomment this line to install global node packages.
|
||||||
|
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
|
47
.devcontainer/devcontainer.json
Normal file
47
.devcontainer/devcontainer.json
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
|
||||||
|
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/go
|
||||||
|
{
|
||||||
|
"name": "Go",
|
||||||
|
"build": {
|
||||||
|
"dockerfile": "Dockerfile",
|
||||||
|
"args": {
|
||||||
|
// Update the VARIANT arg to pick a version of Go: 1, 1.19, 1.18
|
||||||
|
// Append -bullseye or -buster to pin to an OS version.
|
||||||
|
// Use -bullseye variants on local arm64/Apple Silicon.
|
||||||
|
"VARIANT": "1.19-bullseye",
|
||||||
|
// Options
|
||||||
|
"NODE_VERSION": "none"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
|
||||||
|
|
||||||
|
// Configure tool-specific properties.
|
||||||
|
"customizations": {
|
||||||
|
// Configure properties specific to VS Code.
|
||||||
|
"vscode": {
|
||||||
|
// Set *default* container specific settings.json values on container create.
|
||||||
|
"settings": {
|
||||||
|
"go.toolsManagement.checkForUpdates": "local",
|
||||||
|
"go.useLanguageServer": true,
|
||||||
|
"go.gopath": "/go"
|
||||||
|
},
|
||||||
|
|
||||||
|
// Add the IDs of extensions you want installed when the container is created.
|
||||||
|
"extensions": [
|
||||||
|
"golang.Go"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
// "forwardPorts": [],
|
||||||
|
|
||||||
|
// Use 'postCreateCommand' to run commands after the container is created.
|
||||||
|
// "postCreateCommand": "go version",
|
||||||
|
|
||||||
|
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
|
||||||
|
"remoteUser": "vscode",
|
||||||
|
"features": {
|
||||||
|
"git": "os-provided"
|
||||||
|
}
|
||||||
|
}
|
25
.gitignore
vendored
Normal file
25
.gitignore
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
# Bird Bot Specific
|
||||||
|
birdbot.yaml
|
||||||
|
build/
|
7
Dockerfile
Normal file
7
Dockerfile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
FROM alpine:3
|
||||||
|
|
||||||
|
COPY build/birdbot /usr/bin/birdbot
|
||||||
|
|
||||||
|
VOLUME /etc/birdbot
|
||||||
|
|
||||||
|
ENTRYPOINT ["/usr/bin/birdbot", "-c=/etc/birdbot/birdbot.yaml"]
|
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
Copyright 2022
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
83
Makefile
Normal file
83
Makefile
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
PROJECTNAME="Bird Bot"
|
||||||
|
PROJECT_BIN="birdbot"
|
||||||
|
|
||||||
|
# Go related variables.
|
||||||
|
GOBASE=$(shell pwd)
|
||||||
|
GOBIN=$(GOBASE)/build
|
||||||
|
GOFILES=$(wildcard *.go)
|
||||||
|
|
||||||
|
# Make is verbose in Linux. Make it silent.
|
||||||
|
MAKEFLAGS += --silent
|
||||||
|
|
||||||
|
go-full-build: go-clean go-get go-build
|
||||||
|
|
||||||
|
go-build:
|
||||||
|
@echo " > Building binary..."
|
||||||
|
@mkdir -p $(GOBIN)
|
||||||
|
@GOOS=linux CGO_ENABLED=0 go build -o $(GOBIN)/$(PROJECT_BIN) $(GOFILES)
|
||||||
|
@chmod 755 $(GOBIN)/$(PROJECT_BIN)
|
||||||
|
|
||||||
|
go-generate:
|
||||||
|
@echo " > Generating dependency files..."
|
||||||
|
@go generate $(generate)
|
||||||
|
|
||||||
|
go-get:
|
||||||
|
@go env -w GOPRIVATE=github.com/meteoritesolutions
|
||||||
|
@echo " > Checking if there is any missing dependencies..."
|
||||||
|
@go get $(get)
|
||||||
|
|
||||||
|
go-install:
|
||||||
|
@echo " > Running go install..."
|
||||||
|
@go install $(GOFILES)
|
||||||
|
|
||||||
|
go-clean:
|
||||||
|
@echo " > Cleaning build cache"
|
||||||
|
@go clean
|
||||||
|
|
||||||
|
go-test: clean
|
||||||
|
@echo " > Running tests..."
|
||||||
|
@go test -coverprofile=coverage.out ./*/
|
||||||
|
|
||||||
|
go-run:
|
||||||
|
@echo " > Running ${PROJECTNAME}"
|
||||||
|
@-(cd $(GOBIN); ./$(PROJECT_BIN))
|
||||||
|
|
||||||
|
docker-build:
|
||||||
|
@docker build . -t yeslayla/birdbot:latest
|
||||||
|
|
||||||
|
docker-run: docker-build
|
||||||
|
@docker run -it -v `pwd`/build:/etc/birdbot yeslayla/birdbot:latest
|
||||||
|
|
||||||
|
## install: Download and install dependencies
|
||||||
|
install: go-get
|
||||||
|
|
||||||
|
# clean: Runs go clean
|
||||||
|
clean: go-clean
|
||||||
|
|
||||||
|
## full-build: cleans project, installs dependencies, and builds project
|
||||||
|
full-build: go-full-build
|
||||||
|
|
||||||
|
## build: Runs go build
|
||||||
|
build: go-build
|
||||||
|
|
||||||
|
## package: Builds lambda zip
|
||||||
|
package: go-full-build
|
||||||
|
@echo " > Zipping package..."
|
||||||
|
@cd $(GOBIN) && zip $(PROJECTNAME).zip $(PROJECTNAME)
|
||||||
|
|
||||||
|
## clean: Runs go clean
|
||||||
|
clean:
|
||||||
|
@rm -rf build
|
||||||
|
|
||||||
|
## run: full-builds and executes project binary
|
||||||
|
run: go-build go-run
|
||||||
|
|
||||||
|
## test: Run unit tests
|
||||||
|
test: go-test
|
||||||
|
|
||||||
|
## help: Displays help text for make commands
|
||||||
|
.DEFAULT_GOAL := help
|
||||||
|
all: help
|
||||||
|
help: Makefile
|
||||||
|
@echo " Choose a command run in "$(PROJECTNAME)":"
|
||||||
|
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
|
25
ReadMe.md
Normal file
25
ReadMe.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Bird Bot
|
||||||
|
|
||||||
|
Bird Bot is a discord bot for managing and organizing events for a small discord community.
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Creating text channels for events
|
||||||
|
- Notifying when events are created & cancelled
|
||||||
|
- Delete text channels after events
|
||||||
|
- Archive text channels after events
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To get up and running, install go and you can run `make run`!
|
||||||
|
|
||||||
|
## Using Docker
|
||||||
|
|
||||||
|
The container is expecting the config file to be located at `/etc/birdbot/birdbot.yaml`. The easily solution here is to mount the conifg with a volume.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```bash
|
||||||
|
docker run -it -v `pwd`:/etc/birdbot yeslayla/birdbot:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example, your config is in the current directory and call `birdbot.yaml`
|
205
app/bot.go
Normal file
205
app/bot.go
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"github.com/ilyakaznacheev/cleanenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Bot struct {
|
||||||
|
discord *discordgo.Session
|
||||||
|
|
||||||
|
// Discord Objects
|
||||||
|
guildID string
|
||||||
|
eventCategoryID string
|
||||||
|
archiveCategoryID string
|
||||||
|
notificationChannelID string
|
||||||
|
|
||||||
|
// Signal for shutdown
|
||||||
|
stop chan os.Signal
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initalize creates the discord session and registers handlers
|
||||||
|
func (app *Bot) Initialize(config_path string) error {
|
||||||
|
log.Printf("Using config: %s", config_path)
|
||||||
|
|
||||||
|
cfg := &Config{}
|
||||||
|
err := cleanenv.ReadConfig(config_path, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load directly from config
|
||||||
|
app.guildID = cfg.Discord.GuildID
|
||||||
|
app.eventCategoryID = cfg.Discord.EventCategory
|
||||||
|
app.archiveCategoryID = cfg.Discord.ArchiveCategory
|
||||||
|
app.notificationChannelID = cfg.Discord.NotificationChannel
|
||||||
|
|
||||||
|
if app.guildID == "" {
|
||||||
|
return fmt.Errorf("discord Guild ID is not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Discord Session
|
||||||
|
app.discord, err = discordgo.New(fmt.Sprint("Bot ", cfg.Discord.Token))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create Discord session: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register Event Handlers
|
||||||
|
app.discord.AddHandler(app.onReady)
|
||||||
|
app.discord.AddHandler(app.onEventCreate)
|
||||||
|
app.discord.AddHandler(app.onEventDelete)
|
||||||
|
app.discord.AddHandler(app.onEventUpdate)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run opens the session with Discord until exit
|
||||||
|
func (app *Bot) Run() error {
|
||||||
|
|
||||||
|
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)
|
||||||
|
signal.Notify(app.stop, os.Interrupt)
|
||||||
|
<-app.stop
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop triggers a graceful shutdown of the app
|
||||||
|
func (app *Bot) Stop() {
|
||||||
|
log.Print("Shuting down...")
|
||||||
|
app.stop <- os.Kill
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify sends a message to the notification channe;
|
||||||
|
func (app *Bot) Notify(message string) {
|
||||||
|
if app.notificationChannelID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := app.discord.ChannelMessageSend(app.notificationChannelID, message)
|
||||||
|
|
||||||
|
log.Println("Notification: ", message)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed notification: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Bot) onReady(s *discordgo.Session, r *discordgo.Ready) {
|
||||||
|
log.Print("BirdBot is ready!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Bot) onEventCreate(s *discordgo.Session, r *discordgo.GuildScheduledEventCreate) {
|
||||||
|
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, "'")
|
||||||
|
|
||||||
|
channel, err := CreateChannelIfNotExists(s, app.guildID, event.GetChannelName())
|
||||||
|
if err != nil {
|
||||||
|
log.Print("Failed to create channel for event: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if app.eventCategoryID != "" {
|
||||||
|
if _, err = s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{
|
||||||
|
ParentID: app.eventCategoryID,
|
||||||
|
}); err != nil {
|
||||||
|
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)
|
||||||
|
app.Notify(fmt.Sprintf("<@%s> is organizing an event '%s': %s", event.OrganizerID, event.Name, eventURL))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Bot) onEventDelete(s *discordgo.Session, r *discordgo.GuildScheduledEventDelete) {
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := DeleteChannel(app.discord, app.guildID, event.GetChannelName())
|
||||||
|
if err != nil {
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Bot) onEventUpdate(s *discordgo.Session, r *discordgo.GuildScheduledEventUpdate) {
|
||||||
|
|
||||||
|
// 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
|
||||||
|
switch r.Status {
|
||||||
|
case discordgo.GuildScheduledEventStatusCompleted:
|
||||||
|
app.onEventComplete(s, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *Bot) onEventComplete(s *discordgo.Session, event *Event) {
|
||||||
|
channel_name := event.GetChannelName()
|
||||||
|
|
||||||
|
if app.archiveCategoryID != "" {
|
||||||
|
|
||||||
|
// Get Channel ID
|
||||||
|
id, err := GetChannelID(s, app.guildID, channel_name)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to archive channel: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move to archive category
|
||||||
|
if _, err := s.ChannelEdit(id, &discordgo.ChannelEdit{
|
||||||
|
ParentID: app.archiveCategoryID,
|
||||||
|
}); err != nil {
|
||||||
|
log.Printf("Failed to move channel to archive category: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Archived channel: '%s'", channel_name)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Delete Channel
|
||||||
|
_, err := DeleteChannel(s, app.guildID, channel_name)
|
||||||
|
if err != nil {
|
||||||
|
log.Print("Failed to delete channel: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Deleted channel: '%s'", channel_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBot() *Bot {
|
||||||
|
return &Bot{}
|
||||||
|
}
|
68
app/channel.go
Normal file
68
app/channel.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
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)
|
||||||
|
}
|
14
app/configuration.go
Normal file
14
app/configuration.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
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"`
|
||||||
|
ArchiveCategory string `yaml:"archive_category"`
|
||||||
|
NotificationChannel string `yaml:"notification_channel"`
|
||||||
|
}
|
86
app/event.go
Normal file
86
app/event.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const REMOTE_LOCATION string = "online"
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
Name string
|
||||||
|
Location string
|
||||||
|
DateTime time.Time
|
||||||
|
|
||||||
|
OrganizerID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event *Event) GetChannelName() string {
|
||||||
|
month := event.GetMonthPrefix()
|
||||||
|
day := event.DateTime.Day() - 1
|
||||||
|
city := event.GetCityFromLocation()
|
||||||
|
|
||||||
|
channel := fmt.Sprint(month, "-", day, "-", city, "-", event.Name)
|
||||||
|
channel = strings.ReplaceAll(channel, " ", "-")
|
||||||
|
channel = strings.ToLower(channel)
|
||||||
|
|
||||||
|
re, _ := regexp.Compile(`[^\w\-]`)
|
||||||
|
channel = re.ReplaceAllString(channel, "")
|
||||||
|
|
||||||
|
return channel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event *Event) GetCityFromLocation() string {
|
||||||
|
|
||||||
|
if event.Location == REMOTE_LOCATION {
|
||||||
|
return REMOTE_LOCATION
|
||||||
|
}
|
||||||
|
parts := strings.Split(event.Location, " ")
|
||||||
|
index := -1
|
||||||
|
loc := event.Location
|
||||||
|
|
||||||
|
for i, v := range parts {
|
||||||
|
part := strings.ToLower(v)
|
||||||
|
if part == "mi" || part == "michigan" {
|
||||||
|
index = i - 1
|
||||||
|
if index < 0 {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
if index > 0 && parts[index] == "," {
|
||||||
|
index -= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if index > 1 && strings.Contains(parts[index-2], ",") {
|
||||||
|
loc = fmt.Sprintf("%s-%s", parts[index-1], parts[index])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
loc = parts[index]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return loc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event *Event) GetMonthPrefix() string {
|
||||||
|
month := event.DateTime.Month()
|
||||||
|
data := map[time.Month]string{
|
||||||
|
time.January: "jan",
|
||||||
|
time.February: "feb",
|
||||||
|
time.March: "march",
|
||||||
|
time.April: "april",
|
||||||
|
time.May: "may",
|
||||||
|
time.June: "june",
|
||||||
|
time.July: "july",
|
||||||
|
time.August: "aug",
|
||||||
|
time.September: "sept",
|
||||||
|
time.October: "oct",
|
||||||
|
time.November: "nov",
|
||||||
|
time.December: "dec",
|
||||||
|
}
|
||||||
|
|
||||||
|
return data[month]
|
||||||
|
}
|
15
go.mod
Normal file
15
go.mod
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
module github.com/manleydev/bird-bot
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.2.1 // indirect
|
||||||
|
github.com/bwmarrin/discordgo v0.26.1 // indirect
|
||||||
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
|
github.com/ilyakaznacheev/cleanenv v1.4.0 // indirect
|
||||||
|
github.com/joho/godotenv v1.4.0 // 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
|
||||||
|
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||||
|
)
|
24
go.sum
Normal file
24
go.sum
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
|
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
|
||||||
|
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/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
|
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/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=
|
||||||
|
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=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
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/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
|
23
main.go
Normal file
23
main.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/manleydev/bird-bot/app"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var config_file string
|
||||||
|
flag.StringVar(&config_file, "c", "birdbot.yaml", "Path to config file")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
bot := app.NewBot()
|
||||||
|
if err := bot.Initialize(config_file); err != nil {
|
||||||
|
log.Fatal("Failed to initialize: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bot.Run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
11
sample_config.yaml
Normal file
11
sample_config.yaml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
discord:
|
||||||
|
# Discord Bot Token
|
||||||
|
token: ""
|
||||||
|
|
||||||
|
# ID of Discord Server to Monitor
|
||||||
|
guild_id: ""
|
||||||
|
|
||||||
|
# Category & Channel IDs
|
||||||
|
event_category: ""
|
||||||
|
archive_category: ""
|
||||||
|
notification_channel: ""
|
Loading…
x
Reference in New Issue
Block a user