commands

package
v0.0.0-...-2de04ff Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandChainRoles = Command{
	Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate, lcm *common.LifeCycleManager, db *database.Database, log *log.Logger) {
		data := i.ApplicationCommandData()

		baseRoleID := ""
		newRoles := make([]*discordgo.Role, 0)
		for _, v := range data.Options {
			if v == nil {
				continue
			}

			if v.Name == baseRoleName {
				baseRoleID = v.RoleValue(s, i.GuildID).ID
				continue
			}

			newRoles = append(newRoles, v.RoleValue(s, i.GuildID))
		}

		roleSetFailure := false
		var highestID big.Int
		for {
			members, err := s.GuildMembers(i.GuildID, highestID.String(), 1000)
			if err != nil {
				log.Printf("%s: failed to list guild members: %s", i.ID, err)
				InternalError(s, i, log)
				return
			}

			log.Printf("Found %d users... processing...", len(members))

			for _, v := range members {
				if v == nil {
					log.Printf("%s: nil guild member in query")
					continue
				}

				var id big.Int
				id.SetString(v.User.ID, 10)
				if id.Cmp(&highestID) == 1 {
					highestID = id
				}

				foundRole := false
				for _, v := range v.Roles {
					if v == baseRoleID {
						foundRole = true
						break
					}
				}

				if foundRole {
					log.Printf("%s: Assigning new roles to user %s", i.ID, v.User.String())

					for _, newRole := range newRoles {
						err = s.GuildMemberRoleAdd(i.GuildID, v.User.ID, newRole.ID)
						if err != nil {
							roleSetFailure = true
							log.Printf("%s: role addition failed (user: %s role: %s): %err", i.ID, v.User.String(), newRole.Name, err)
						}
					}
				}
			}

			if len(members) < 1000 {
				break
			}
		}

		if !roleSetFailure {
			log.Printf("%s: Assigned all new roles to users.", i.ID)
			TryRespond(s, i, "All new roles were assigned.", log)
		} else {
			log.Printf("%s: Some roles failed to assign.", i.ID)
			TryRespond(s, i, fmt.Sprintf("Some roles failed to assign. Contact bot owner with %s.", i.ID), log)
		}
	},
	DgCommand: &discordgo.ApplicationCommand{
		Name:                     "chainroles",
		Description:              "Assign multiple roles to members with existing roles",
		DefaultMemberPermissions: common.Pointer(int64(discordgo.PermissionManageRoles)),
		DMPermission:             common.Pointer(false),
		Options: func() []*discordgo.ApplicationCommandOption {
			out := make([]*discordgo.ApplicationCommandOption, 24)

			for k := range out {
				out[k] = &discordgo.ApplicationCommandOption{
					Type:        discordgo.ApplicationCommandOptionRole,
					Name:        fmt.Sprintf("new_role%d", k),
					Description: "The initial required role to receive additional roles.",
					Required:    false,
				}
			}

			required := []*discordgo.ApplicationCommandOption{
				{
					Type:        discordgo.ApplicationCommandOptionRole,
					Name:        baseRoleName,
					Description: "The initial required role to receive additional roles.",
					Required:    true,
				},
			}

			return append(required, out...)
		}(),
	},
	HelpText: chainRolesHelptext,
}
View Source
var CommandEvents = Command{
	Handler: CommandEventsHandler,
	AutoCompleteHandlers: map[string]interface{}{
		"create": map[string]interface{}{
			"frequency":     autocomplete.CreateEventFrequency,
			"initial_event": autocomplete.CreateEventInitialEvent,
		},
		"details": map[string]interface{}{
			"series": autocomplete.EventGetAvailableSeries,
		},
		"delete": map[string]interface{}{
			"series": autocomplete.EventGetAvailableSeries,
		},
		"update": map[string]interface{}{
			"series":    autocomplete.EventGetAvailableSeries,
			"frequency": autocomplete.CreateEventFrequency,
		},
	},
	DgCommand: &discordgo.ApplicationCommand{
		Type:                     discordgo.ChatApplicationCommand,
		Name:                     "events",
		Description:              "Wrangle auto-scheduled events.",
		DefaultMemberPermissions: common.Pointer(int64(discordgo.PermissionManageEvents)),
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "create",
				Description: "Create a new auto-scheduled event. Can clone existing events.",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "initial_event",
						Description:  "Initial event. Autocorrect presents the 25 soonest non-series events.",
						Required:     true,
						Autocomplete: true,
					},
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "frequency",
						Description:  "How frequently the event should exist",
						Required:     true,
						Autocomplete: true,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "details",
				Description: "Check frequency of auto-scheduled events.",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "series",
						Description:  "Target series. Autocorrect prevents the 25 series with the soonest events.",
						Required:     true,
						Autocomplete: true,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "list",
				Description: "List auto-scheduled events.",
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "delete",
				Description: "Delete auto-scheduled events.",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "series",
						Description:  "Target series. Autocorrect prevents the 25 series with the soonest events.",
						Required:     true,
						Autocomplete: true,
					},
					{
						Type:        discordgo.ApplicationCommandOptionBoolean,
						Name:        "delete_finale",
						Description: "Delete the final event? (default: true)",
						Required:    false,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "update",
				Description: "Adjust the frequency of an event.",
				Options: []*discordgo.ApplicationCommandOption{
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "series",
						Description:  "Target series. Autocorrect prevents the 25 series with the soonest events.",
						Required:     true,
						Autocomplete: true,
					},
					{
						Type:         discordgo.ApplicationCommandOptionString,
						Name:         "frequency",
						Description:  "How frequently the event should exist",
						Required:     true,
						Autocomplete: true,
					},
				},
			},
			{
				Type:        discordgo.ApplicationCommandOptionSubCommand,
				Name:        "purge",
				Description: "Purge the database of events that do not match",
			},
		},
	},
	HelpText: EventsHelptext,
}
View Source
var CommandSource = Command{
	Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate, lcm *common.LifeCycleManager, db *database.Database, log *log.Logger) {
		TryRespond(s, i, "My source code is available at: https://github.com/Riven-Spell/hydaelyn", log)
	},
	DgCommand: &discordgo.ApplicationCommand{
		Name:        "source",
		Description: "Links to the source code.",
		Type:        discordgo.ChatApplicationCommand,
	},
	HelpText: "Links to the source code.",
}
View Source
var EventsHelptext string
View Source
var GlobalCommands = map[string]Command{
	RoleReactCommand.DgCommand.Name:  RoleReactCommand,
	CommandChainRoles.DgCommand.Name: CommandChainRoles,
	CommandSource.DgCommand.Name:     CommandSource,
	CommandEvents.DgCommand.Name:     CommandEvents,
}
View Source
var HelpCommand = Command{
	Handler: HelpHandler,
	DgCommand: &discordgo.ApplicationCommand{
		Version:      "1.0",
		Type:         discordgo.ChatApplicationCommand,
		Name:         "help",
		DMPermission: common.Pointer(true),
		Description:  "List commands & information",
		Options: []*discordgo.ApplicationCommandOption{
			{
				Type:        discordgo.ApplicationCommandOptionString,
				Name:        "command",
				Description: "Target command to view",
				Required:    false,
			},
		},
	},
	HelpText: helpHelptext,
}
View Source
var RoleReactCommand = Command{
	Handler: RoleReactCommandHandler,
	DgCommand: &discordgo.ApplicationCommand{
		Type:                     discordgo.ChatApplicationCommand,
		Name:                     "rolereact",
		DMPermission:             common.Pointer(false),
		Description:              "Creates a role react message for unprivileged users. /setup required.",
		DefaultMemberPermissions: common.Pointer(int64(discordgo.PermissionManageRoles)),
		Options: func() []*discordgo.ApplicationCommandOption {

			out := make([]*discordgo.ApplicationCommandOption, 24)
			for k, _ := range out {
				out[k] = &discordgo.ApplicationCommandOption{
					Type:        discordgo.ApplicationCommandOptionString,
					Name:        fmt.Sprintf("rolereact%d", k),
					Description: "Role react format: `emoji:roleid,roleid...`",
					Required:    false,
				}
			}

			textOption := &discordgo.ApplicationCommandOption{
				Type:        discordgo.ApplicationCommandOptionString,
				Name:        "description",
				Description: "Describe this role react message!",
				Required:    true,
			}

			out = append([]*discordgo.ApplicationCommandOption{textOption}, out...)

			return out
		}(),
	},
	HelpText: roleReactHelptext,
}

Functions

func GetEmojiID

func GetEmojiID(emoji string) string

func RegisterGlobalCommands

func RegisterGlobalCommands(s *discordgo.Session, cfg common.ConfigDiscord) error

func TryGetUsername

func TryGetUsername(i *discordgo.InteractionCreate) string

func TryRespond

func TryRespond(s *discordgo.Session, i *discordgo.InteractionCreate, response string, log *log.Logger) *discordgo.Message

Types

type Command

type Command struct {
	Handler              Handler
	AutoCompleteHandlers map[string]interface{} // either another similar map or a Handler
	DgCommand            *discordgo.ApplicationCommand
	HelpText             string
}

Directories

Path Synopsis
autocompletes

Jump to

Keyboard shortcuts

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