discord

package
v0.0.0-...-5cb0a9e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Clean = &cobra.Command{
	Use:   "discord:clean",
	Short: "Cleans the registered slash commands",
	RunE: func(cmd *cobra.Command, args []string) error {
		dg, err := discord.NewSession()
		if err != nil {
			return err
		}

		appId := config.GetAppID()
		guildId := config.GetGuildID()

		_, err = dg.ApplicationCommandBulkOverwrite(appId, guildId, []*discordgo.ApplicationCommand{})
		if err != nil {
			return err
		}

		log.Info().Msg("removed application commands")

		return nil
	},
}
View Source
var Run = &cobra.Command{
	Use:   "discord:run",
	Short: "Starts the Discord bot",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		err := cache.Content()
		if err != nil {
			return err
		}

		cache.InitRatelimit(config.GetYaml().Ratelimit.TTL)

		cache.InitAutoMod()

		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		session, err := internalDiscord.NewSession()
		if err != nil {
			return fmt.Errorf("failed to create new Discord session: %s", err)
		}

		log.Info().Str("link", internalDiscord.GenerateInviteLink()).Msg("server invite")

		command.RegisterApplicationCommands(session)

		session.AddHandler(handler.Ready)

		session.AddHandler(handler.MessageCreate)
		session.AddHandler(handler.InteractionCreate)

		session.Identify.Intents = config.BotIntents

		channels, err := session.GuildChannels(config.GetGuildID())
		if err != nil {
			return fmt.Errorf("failed to fetch guild channels: %s", err)
		}
		cache.AutoMod.SetChannels(channels)

		err = session.Open()
		if err != nil {
			return fmt.Errorf("failed to open Discord connection: %v", err)
		}

		sc := make(chan os.Signal, 1)
		signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, syscall.SIGTERM)
		signal := <-sc

		log.Warn().Str("signal", signal.String()).Msg("shutting down")

		return session.Close()
	},
	PostRun: func(cmd *cobra.Command, args []string) {
		log.Warn().Msg("discord session closed")
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL