justusbot/cmd/bot/main.go

339 lines
9.4 KiB
Go

package main
import (
"context"
"os"
_ "sync"
"github.com/urfave/cli/v3"
"go.destrealm.net/errors"
"go.destrealm.net/logging/v3"
"go.destrealm.net/vfs"
"code.destrealm.net/zancarius/justusbot/internal/commands"
)
// Send any text message to the bot after the bot has been started
func main() {
var rootfs vfs.VFS
ctx := context.Background()
logger := logging.Logger()
if root, err := os.Getwd(); err != nil {
logger.Error("could not discern working directory:", err)
os.Exit(1)
} else {
rootfs = vfs.MustVFS(vfs.NewVFS(root))
}
cmdHost, err := commands.NewCommandHost(ctx, rootfs)
if err != nil {
logger.Error("unable to configure host:", errors.Unfurl(err))
os.Exit(1)
}
cmd := cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Configuration location. Defaults to working directory.",
Value: "settings.toml",
},
},
Commands: []*cli.Command{
{
Name: "announcements",
Description: "Announcement management",
Commands: []*cli.Command{
{
Name: "add",
Description: "Adds an announcement to the queue.",
Action: cmdHost.Announcements().AddAnnouncement,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
&cli.BoolFlag{
Name: "reminders",
Aliases: []string{"r"},
Usage: "Enables reminders for this announcement. Reminders will be triggered one week prior and one day prior by default.",
},
&cli.StringFlag{
Name: "title",
Aliases: []string{"t"},
Usage: "Announcement title (optional).",
},
&cli.StringFlag{
Name: "content",
Aliases: []string{"a"},
Usage: "Announcement content (required).",
},
},
},
{
Name: "weekly",
Description: "Lists this week's announcements.",
Action: cmdHost.Announcements().ListThisWeeksAnnouncements,
},
{
Name: "today",
Description: "Lists the announcements for today.",
Action: cmdHost.Announcements().ListTodaysAnnouncements,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
},
},
},
},
{
Name: "birthdays",
Description: "Birthday management",
Commands: []*cli.Command{
{
Name: "add",
Description: "Adds a birthday to the roster. Chat ID and name required.",
Action: cmdHost.Birthdays().AddBirthday,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "User's name. The user does not have to be present in the specified chat.",
},
&cli.Int64Flag{
Name: "userid",
Aliases: []string{"u"},
Usage: "User ID. The user must be present in the specified chat if this flag is provided.",
},
&cli.IntFlag{
Name: "birthday-id",
Aliases: []string{"b"},
Usage: "Birthday ID. Use this flag to update a given birthday. This can be used to change the birthday's date or to attach a user ID with a previously entered birthday.",
},
},
},
{
Name: "list",
Description: "Lists all birthdays in the current roster for the specified chat ID.",
Action: cmdHost.Birthdays().ListBirthdays,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
&cli.BoolFlag{
Name: "annunciate",
Aliases: []string{"a"},
Usage: "Displays announcement status.",
},
},
},
{
Name: "weekly",
Description: "Lists the weekly birthdays for this week.",
Action: cmdHost.Birthdays().ListWeeklyBirthdays,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
},
},
{
Name: "today",
Description: "Lists birthdays for today.",
Action: cmdHost.Birthdays().ListTodaysBirthdays,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"c"},
Usage: "Specifies the chat ID. Required.",
},
},
},
},
},
{
Name: "chats",
Description: "Chats management",
Commands: []*cli.Command{
{
Name: "add",
Description: "Adds a chat to the bot. This is analogous to running /start within a chat.",
Action: cmdHost.Chats().AddChat,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "id",
Aliases: []string{"i"},
Usage: "Specifies the chat id.",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Specifies the chat name.",
},
},
},
{
Name: "list",
Description: "List the chats currently configured for this bot.",
Action: cmdHost.Chats().ListChats,
},
},
},
{
Name: "init",
Description: "Initialize the bot, its database, and prepare for first run.",
Action: cmdHost.Initalizer().DoInstall,
},
{
Name: "metadata",
Description: "Bible metadata-related management.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "translation",
Aliases: []string{"t"},
Value: "bsb",
Usage: "Specifies the Bible translation to use for metadata extraction and inspection.",
},
},
Commands: []*cli.Command{
{
Name: "dump",
Description: "Dump Bible metadata",
Commands: []*cli.Command{
{
Name: "structure",
Description: "Dumps an entire translation's book, chapter, and verse structure.",
Action: cmdHost.Meta().DumpStructure,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"f"},
Value: "json",
Usage: `Output format. One of "json" or "toml".`,
},
},
},
},
},
},
},
{
Name: "migrate",
Description: "Migration-related commands and options.",
Commands: []*cli.Command{
{
Name: "up",
Description: `Perform upgrade.`,
Action: cmdHost.Initalizer().DoUp,
},
{
Name: "down",
Description: `Perform downgrade.`,
},
{
Name: "list",
Description: "List available migrations",
Action: cmdHost.Initalizer().ListMigrations,
},
},
},
{
Name: "start",
Description: "Start the bot.",
Action: cmdHost.App().Start,
},
{
Name: "verse",
Description: "Bible verse helpers and tools.",
Commands: []*cli.Command{
{
Name: "add",
Description: "Add a verse to the random rotation.",
Action: cmdHost.Verse().AddVerse,
//Future implementation.
Flags: []cli.Flag{
&cli.StringFlag{
Name: "translation",
Usage: "Specifies which translation this verse should be included from.",
Aliases: []string{"t"},
Value: "bsb",
},
&cli.StringFlag{
Name: "date",
Usage: "Specifies a date for the provided verse to be displayed. Overrides random rotation. Use with caution as this will override random verses for all connected chats.",
Aliases: []string{"d"},
},
},
},
{
Name: "list",
Description: "List verses configured for this bot.",
Action: cmdHost.Verse().ListVerses,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "chatid",
Usage: "Include verses configured for the specified chat ID",
Aliases: []string{"i"},
},
&cli.BoolFlag{
Name: "random",
Usage: "Include verse random rotation status (if available); requires --chatid",
Aliases: []string{"r"},
},
},
},
{
Name: "random",
Description: "select a random verse from the bot's database",
Action: cmdHost.Verse().RandomVerse,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "simulate",
Usage: "Simulates a random verse selection from the bot and populates the internal verse rotation status. Requires --chatid.",
},
},
},
{
Name: "rotation",
Description: "Random verse rotation management",
Commands: []*cli.Command{
{
Name: "delete",
Description: "Delete rotations; this will reset the specified chat's exhausted verses list",
Action: cmdHost.Verse().DeleteRotation,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "chatid",
Aliases: []string{"i"},
Usage: "Specifies the chat from which rotation records will be removed.",
},
},
},
},
},
},
},
},
}
err = cmd.Run(ctx, os.Args)
if err != nil {
panic(err)
}
}