feat: add midori skeleton, bot scaffolding, CD seeds

This commit is contained in:
41666 2020-09-27 00:32:15 -04:00
parent 7c861b37ca
commit 49d44df231
30 changed files with 854 additions and 123 deletions

View file

@ -11,6 +11,8 @@ go_library(
importpath = "github.com/roleypoly/roleypoly/src/discord-bot",
visibility = ["//visibility:private"],
deps = [
"//src/common",
"//src/common/bot",
"//src/common/version",
"//src/discord-bot/internal/strings",
"@com_github_bwmarrin_discordgo//:discordgo",

View file

@ -1,47 +1,37 @@
package main
import (
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
_ "github.com/joho/godotenv/autoload"
"github.com/lampjaw/discordclient"
"github.com/roleypoly/roleypoly/src/common"
"github.com/roleypoly/roleypoly/src/common/bot"
"github.com/roleypoly/roleypoly/src/common/version"
"k8s.io/klog"
)
var (
botToken = os.Getenv("DISCORD_BOT_TOKEN")
botClientID = os.Getenv("DISCORD_CLIENT_ID")
rootUsers = strings.Split(os.Getenv("ROOT_USERS"), ",")
allowedBots = strings.Split(os.Getenv("ALLOWED_BOTS"), ",")
appURL = os.Getenv("PUBLIC_URL")
selfMention = regexp.MustCompile("<@!?" + botClientID + ">")
botToken = common.Getenv("DISCORD_BOT_TOKEN").String()
botClientID = common.Getenv("DISCORD_CLIENT_ID").String()
rootUsers = common.Getenv("ROOT_USERS").StringSlice()
allowedBots = common.Getenv("ALLOWED_BOTS").StringSlice()
appURL = common.Getenv("PUBLIC_URL").String()
selfMention = bot.MentionMatcher(botClientID)
)
func main() {
klog.Info(version.StartupInfo("discord-bot"))
discordClient := discordclient.NewDiscordClient(botToken, rootUsers[0], botClientID)
discordClient.GatewayIntents = discordgo.IntentsGuildMessages
messageChannel, err := discordClient.Listen(-1)
err := bot.ScaffoldBot(bot.BotScaffolding{
RootUsers: rootUsers,
AllowBots: true,
BotClientID: botClientID,
BotToken: botToken,
GatewayIntents: discordgo.IntentsGuildMessages,
Handler: handle,
})
if err != nil {
klog.Fatal(err)
}
defer awaitExit()
l := listener{
client: discordClient,
}
go l.processMessages(messageChannel)
}
func isBotAllowlisted(userID string) bool {
@ -53,15 +43,3 @@ func isBotAllowlisted(userID string) bool {
return false
}
func awaitExit() {
syscallExit := make(chan os.Signal, 1)
signal.Notify(
syscallExit,
syscall.SIGINT,
syscall.SIGTERM,
os.Interrupt,
os.Kill,
)
<-syscallExit
}

View file

@ -1,28 +1,16 @@
package main
import (
"context"
"github.com/lampjaw/discordclient"
"k8s.io/klog"
"github.com/roleypoly/roleypoly/src/common/bot"
"github.com/roleypoly/roleypoly/src/discord-bot/internal/strings"
)
type listener struct {
client *discordclient.DiscordClient
}
func (l *listener) processMessages(messageChannel <-chan discordclient.Message) {
for {
go l.handle(<-messageChannel)
}
}
func (l *listener) handle(message discordclient.Message) {
// Only if it's a message create
if message.Type() != discordclient.MessageTypeCreate {
return
}
func handle(ctx context.Context, message discordclient.Message) {
// Only if it's an allowed bot
if message.IsBot() && !isBotAllowlisted(message.UserID()) {
return
@ -33,21 +21,17 @@ func (l *listener) handle(message discordclient.Message) {
return
}
l.defaultResponse(message)
bot.ReplyToMessage(ctx, defaultResponse(message))
}
func (l *listener) defaultResponse(message discordclient.Message) {
channel := message.Channel()
func defaultResponse(message discordclient.Message) string {
guild, err := message.ResolveGuildID()
if err != nil {
klog.Warning("failed to fetch guild, ", err)
}
l.client.SendMessage(
channel,
strings.Render(
strings.MentionResponse,
strings.MentionResponseData{GuildID: guild, AppURL: appURL},
),
return strings.Render(
strings.MentionResponse,
strings.MentionResponseData{GuildID: guild, AppURL: appURL},
)
}