integrations

package
v0.0.0-...-f00225b Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: OSL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CollegeIntegrationID = "44b61604-49a8-4b4b-a868-86276cfdba62"
View Source
var DormCommand = &dgc.Command{
	Name:          "dorm",
	Domain:        "astral.integrations.dorm",
	Aliases:       []string{"dorm", "dormitory"},
	Description:   "Get a user's dorm house and room!",
	Category:      "College",
	Usage:         "dorm <user>",
	Slash:         true,
	SlashGuilds:   []string{os.Getenv("DEV_GUILD")},
	IntegrationID: CollegeIntegrationID,
	Handler: func(ctx *dgc.Ctx) {
		db := db.New()

		var user string

		if ctx.Arguments.Amount() < 1 {
			user = ctx.Message.Author.ID
		} else {
			user = ctx.Arguments.Get(0).AsUserMentionID()
		}

		wi, err := integrations.GetWorkspaceIntegrationForCommand(ctx, CollegeIntegrationID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the workspace integration.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		data, err := db.GetIntegrationDataForUser(user, CollegeIntegrationID, wi.ID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the dorm.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		jsonStr, err := json.Marshal(data.Data)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the dorm.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		var dorm *types.CollegeIntegrationData

		err = json.Unmarshal(jsonStr, &dorm)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the dorm.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		fullUser, err := ctx.Session.GuildMember(ctx.Message.GuildID, user)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the user.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title: fmt.Sprintf("%s's Dorm is in %s %s", fullUser.User.Username, dorm.House, dorm.Room),
			Fields: []*discordgo.MessageEmbedField{
				{
					Name:   "House",
					Value:  dorm.House,
					Inline: true,
				},
				{
					Name:   "Room",
					Value:  dorm.Room,
					Inline: true,
				},
			},
			Color: 0x00ff00,
		}))
	},
}
View Source
var DormlistCommand = &dgc.Command{
	Name:        "dormlist",
	Domain:      "astral.integrations.dormlist",
	Aliases:     []string{"dormlist", "dorms"},
	Description: "Get a list of dorms.",
	Category:    "College",
	Usage:       "dormlist",
	Slash:       true,
	SlashGuilds: []string{os.Getenv("DEV_GUILD")},
	Arguments: []*discordgo.ApplicationCommandOption{
		{
			Name:        "house",
			Description: "The house to get the dorms for.",
			Type:        discordgo.ApplicationCommandOptionString,
		},
	},
	IntegrationID: CollegeIntegrationID,
	Handler: func(ctx *dgc.Ctx) {
		db := db.New()

		self := ctx.CustomObjects.MustGet("self").(types.Bot)

		house := ctx.Arguments.Get(0).Raw()
		data, err := db.GetIntegrationDataForWorkspace(*self.Workspace, CollegeIntegrationID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the integration data.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		var dorms []Dorm

		for _, v := range data {
			jsonStr, err := json.Marshal(v.Data)
			if err != nil {
				utils.ErrorHandler(err)
			}
			var d types.CollegeIntegrationData
			err = json.Unmarshal(jsonStr, &d)
			if err != nil {
				utils.ErrorHandler(err)
			}
			dorms = append(dorms, Dorm{
				House:    d.House,
				Room:     d.Room,
				Resident: v.User,
			})
		}

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the dorms.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))
			return
		}

		sort.Slice(dorms, func(i, j int) bool {
			if dorms[i].Room[0] == dorms[j].Room[0] {
				return dorms[i].Room < dorms[j].Room
			}
			return dorms[i].Room[0] < dorms[j].Room[0]
		})

		if house == "" {
			var fields []*discordgo.MessageEmbedField

			var houses []string

			for _, v := range dorms {
				if !utils.StringInSlice(v.House, houses) {
					houses = append(houses, v.House)
				}
			}

			for _, v := range houses {

				val := ""

				for _, v2 := range dorms {
					if v2.House == v {
						val += fmt.Sprintf("<@%s> - %s\n", v2.Resident, v2.Room)
					}
				}

				fields = append(fields, &discordgo.MessageEmbedField{
					Name:   v,
					Value:  val,
					Inline: false,
				})
			}

			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:  "Dorms",
				Color:  0xffff00,
				Fields: fields,
			}))
			return
		} else {
			var fields []*discordgo.MessageEmbedField

			val := ""

			for _, v2 := range dorms {
				if v2.House == house {
					val += fmt.Sprintf("<@%s> - %s\n", v2.Resident, v2.Room)
				}
			}

			fields = append(fields, &discordgo.MessageEmbedField{
				Name:   house,
				Value:  val,
				Inline: false,
			})

			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:  "Dorms",
				Color:  0xffff00,
				Fields: fields,
			}))
			return
		}
	},
}
View Source
var EmailTemplate = `` /* 1507-byte string literal not displayed */
View Source
var SetDormCommand = &dgc.Command{
	Name:        "setdorm",
	Domain:      "astral.integrations.setdorm",
	Aliases:     []string{"setdorm", "setdormitory"},
	Description: "Set your dorm house and room!",
	Category:    "College",
	Usage:       "setdorm <house> <room>",
	Slash:       true,
	SlashGuilds: []string{os.Getenv("DEV_GUILD")},
	Handler: func(ctx *dgc.Ctx) {
		house, room := ctx.Arguments.Get(0).Raw(), ctx.Arguments.Get(1).Raw()

		database := db.New()

		wi, err := integrations.GetWorkspaceIntegrationForCommand(ctx, CollegeIntegrationID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the workspace integration.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		data, err := database.GetIntegrationDataForUser(ctx.Event.Author.ID, CollegeIntegrationID, wi.ID)

		var d types.CollegeIntegrationData

		if err != nil {

		} else {
			jsonStr, err := json.Marshal(data.Data)

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching the dorm.",
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
					Color: 0xff0000,
				}))

				return
			}

			err = json.Unmarshal(jsonStr, &d)

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching the dorm.",
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
					Color: 0xff0000,
				}))

				return
			}
		}

		d.House = house
		d.Room = room

		err = database.SetIntegrationDataForUser(ctx.Event.Author.ID, CollegeIntegrationID, wi.ID, d)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while setting your dorm.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title:       "Success",
			Description: "Your dorm has been set to " + house + " " + room + "!",
			Color:       0x00ff00,
		}))
	},
}
View Source
var VerifyStudentCommand = &dgc.Command{
	Name:        "verifystudent",
	Domain:      "astral.integrations.verifystudent",
	Aliases:     []string{"verifystudent", "verifystudentemail", "vs", "vse"},
	Description: "Verify your student email address.",
	Category:    "College",
	Usage:       "verifystudent <email>",
	Slash:       true,
	SlashGuilds: []string{os.Getenv("DEV_GUILD")},
	Handler: func(ctx *dgc.Ctx) {
		email := ctx.Arguments.Get(0).Raw()

		database := db.New()

		wi, err := integrations.GetWorkspaceIntegrationForCommand(ctx, CollegeIntegrationID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the workspace integration.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		data, err := database.GetIntegrationDataForUser(ctx.Event.Author.ID, CollegeIntegrationID, wi.ID)

		var d types.CollegeIntegrationData

		if err != nil {

		} else {

			jsonStr, err := json.Marshal(data.Data)

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching your data.",
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
					Color: 0xff0000,
				}))

				return
			}

			err = json.Unmarshal(jsonStr, &d)

			if err != nil {
				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Error",
					Description: "An error occurred while fetching your data.",
					Fields: []*discordgo.MessageEmbedField{
						{
							Name:   "Error",
							Value:  err.Error(),
							Inline: false,
						},
					},
					Color: 0xff0000,
				}))

				return
			}

			if data.Data.(map[string]interface{})["email"] != nil && data.Data.(map[string]interface{})["email"].(map[string]interface{})["verified"].(bool) {
				var fields []*discordgo.MessageEmbedField = []*discordgo.MessageEmbedField{
					{
						Name:   "Email",
						Value:  data.Data.(map[string]interface{})["email"].(map[string]interface{})["address"].(string),
						Inline: false,
					},
				}

				if wi.Settings.(map[string]interface{})["verifiedRoleId"] != nil {
					roleID := wi.Settings.(map[string]interface{})["verifiedRoleId"].(string)

					err = ctx.Session.GuildMemberRoleAdd(ctx.Event.GuildID, ctx.Event.Author.ID, roleID)

					if err != nil {
						ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
							Title:       "Error",
							Description: "An error occurred while adding your role.",
							Fields: []*discordgo.MessageEmbedField{
								{
									Name:   "Error",
									Value:  err.Error(),
									Inline: false,
								},
							},
							Color: 0xff0000,
						}))

						return
					}

					fields = append(fields, &discordgo.MessageEmbedField{
						Name:   "Role",
						Value:  fmt.Sprintf("<@&%s>", roleID),
						Inline: false,
					})
				}

				ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
					Title:       "Verified!",
					Description: "You've verified your email, and I've given you any roles you're eligible for.",
					Fields:      fields,
					Color:       0x00ff00,
				}))

				return
			}

		}

		if wi.Settings.(map[string]interface{})["emailDomain"] != email[strings.LastIndex(email, "@")+1:] {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "The email domain you provided is not valid for this workspace.",
				Color:       0xff0000,
			}))

			return
		}

		if email == "" {
			ctx.ReplyEmbed(utils.ErrorEmbed(*ctx, fmt.Errorf("Please provide an email address.")))
			return
		}

		uuid, err := uuid.NewV4()

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while generating a verification code.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		guild, err := ctx.Session.Guild(ctx.Event.GuildID)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while fetching the guild.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		err = SendEmail([]string{email}, uuid.String(), wi.Workspace, wi.Integration, guild.Name)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while sending the verification email.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		d.Email = types.CollegeEmail{
			Address:          email,
			VerificationCode: uuid.String(),
			Verified:         false,
		}

		err = database.SetIntegrationDataForUser(ctx.Event.Author.ID, CollegeIntegrationID, wi.ID, d)

		if err != nil {
			ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
				Title:       "Error",
				Description: "An error occurred while setting your email.",
				Fields: []*discordgo.MessageEmbedField{
					{
						Name:   "Error",
						Value:  err.Error(),
						Inline: false,
					},
				},
				Color: 0xff0000,
			}))

			return
		}

		ctx.ReplyEmbed(utils.GenerateEmbed(*ctx, discordgo.MessageEmbed{
			Title:       "Success",
			Description: "Your email has been set to `" + email + "`! Check your email for a verification link.",
		}))
	},
}

Functions

func BuildMessage

func BuildMessage(mail Mail) string

func SendEmail

func SendEmail(to []string, code string, workspace string, integration string, guildName string) error

Types

type Dorm

type Dorm struct {
	House    string `json:"house"`
	Room     string `json:"room"`
	Resident string `json:"resident"`
}

type Mail

type Mail struct {
	Sender  string
	To      []string
	Subject string
	Body    string
}

Jump to

Keyboard shortcuts

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