Exemple #1
1
func getMentionableRoles(s *discordgo.Session, GuildID string) []*discordgo.Role {
	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	m, err := s.GuildMember(GuildID, s.State.User.ID)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	roleID := ""
	rolePos := 0

	if len(m.Roles) >= 1 {
		roleID = m.Roles[0]
	}

	for _, role := range g.Roles {
		if role.ID == roleID {
			rolePos = role.Position
			break
		}
	}

	var roles []*discordgo.Role

	for _, role := range g.Roles {
		if role.Name == "@everyone" || !role.Mentionable || role.Position > rolePos {
			continue
		}

		roles = append(roles, role)
	}

	return roles
}
Exemple #2
0
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
	if m.Author.Bot {
		return
	}

	chanl, err := s.Channel(m.ChannelID)
	if err != nil {
		return
	}

	guild, _ := s.Guild(chanl.GuildID)
	var author *discordgo.Member

	if guild != nil {
		author, _ = s.GuildMember(guild.ID, m.Author.ID)
	}

	checkLink(s, m.Content, chanl.ID, chanl.LastMessageID)

	messageContents <- m

	if len(m.Content) > 21 && len(m.Mentions) > 0 &&
		m.Mentions[len(m.Mentions)-1].Username == "MrDestructoid" {
		arr := strings.Fields(m.Content)
		switch arr[1] {
		case "BD":
			bdLinks(s, m.ChannelID)
		// case "state":
		// for _, i := range guild.VoiceStates {
		// fmt.Println(i.UserID)
		// }

		// case "bot":
		// var mem runtime.MemStats
		// runtime.ReadMemStats(&mem)
		// fmt.Println(mem.HeapAlloc)

		case "myid":
			s.ChannelMessageSend(m.ChannelID,
				fmt.Sprintf("```\n%v\n```", m.Author.ID))
		// //---------------- NO WAY TO UTILIZE -----------
		// case "joinchannel":
		// ch := make(chan string)
		// fmt.Println(strings.Join(arr[2:], " "))
		// if findNjoinChannel(s, guild.ID, guild.Channels,
		// strings.Join(arr[2:], " "), ch) {
		// echo(dgv, ch)
		// fmt.Println("opened")
		// }
		// //---------------- NO WAY TO UTILIZE ----------

		case "channels":
			var str string = fmt.Sprintf("\n%-20s\t%-5s\tID", "Name", "Type")
			for _, c := range guild.Channels {
				str += fmt.Sprintf("\n%-20s\t%-5s\t%s", c.Name, c.Type, c.ID)
			}
			s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("```%s```", str))

		case "updateroles":
			updateRolesToDB(s, guild.ID)

		case "guild":
			var owner string
			for _, i := range guild.Members {
				if i.User.ID == guild.OwnerID {
					owner = i.User.Username
				}
			}
			s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
				"```**Guild information** \nName: %s\nOwner: %s\nID: %s\nRegion: %s\nHas members: %d```",
				guild.Name, owner, guild.ID, guild.Region, len(guild.Members)))

		case "quit":
			if checkForPermissions(s, chanl.ID, author.Roles) {
				quit = true
			}

		case "ban":
			banUser(s, guild, author, m.ChannelID, arr)

		case "unban":
			manualUnban(s, chanl.ID, author.Roles, arr)

		case "bannedusers":
			bannedUsers(s, m)

		default:
			s.ChannelMessageSend(chanl.ID, fmt.Sprintf("No such command"))
		}

	}
}
Exemple #3
0
func addRole(s *discordgo.Session, GuildID string, UserID string, arg string) string {
	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return "You f*****g broke it~"
	}

	exists, role := roleExists(g, arg)

	if !exists {
		if !strings.HasPrefix(arg, "yes") {
			return fmt.Sprintf("I can't find such group~ Are you sure you didn't mistype it? Say **!subscribe yes %s** to create a new one~", arg)
		}

		if len(arg) < 5 {
			return "Are you sure you actually typed a name?~"
		}

		arg = arg[4:]

		exists, role = roleExists(g, arg)

		if !exists {
			newRole, err := s.GuildRoleCreate(GuildID)

			if err != nil {
				fmt.Println(err)
				return "Failed to create role"
			}

			role, err = s.GuildRoleEdit(GuildID, newRole.ID, arg, newRole.Color, newRole.Hoist, 37080064, true)
			if err != nil {
				fmt.Println(err)

				err = s.GuildRoleDelete(GuildID, newRole.ID)

				if err != nil {
					fmt.Println(err)
				}

				return "You f*****g broke it~"
			}
			fmt.Println(role)
		} else {
			return "Why are you trying to recreate that group?"
		}
	}

	member, err := s.GuildMember(GuildID, UserID)
	if err != nil {
		fmt.Println(err)
		return ""
	}

	if exists {
		for _, _role := range member.Roles {
			if _role == role.ID {
				return fmt.Sprintf("You're already subscribed to %s ~", arg)
			}
		}
	}

	newRoles := append(member.Roles, role.ID)

	err = s.GuildMemberEdit(GuildID, UserID, newRoles)
	if err != nil {
		fmt.Println(err)
		return "I can't touch that group dude, do it yourself~"
	}

	if exists {
		return fmt.Sprintf("You're now subscribed to %s~", arg)
	}

	return fmt.Sprintf("Created and subscribed to %s", arg)
}
Exemple #4
0
func removeRole(s *discordgo.Session, GuildID string, UserID string, arg string) string {
	fmt.Println("removeRole", arg)

	g, err := s.State.Guild(GuildID)
	if err != nil {
		fmt.Println(err)
		return "You f*****g broke it~"
	}

	exists, role := roleExists(g, arg)
	if !exists {
		return "I can't find such group~"
	}

	fmt.Println("Found?", exists, role)

	member, err := s.GuildMember(GuildID, UserID)
	if err != nil {
		fmt.Println(err)
	}

	pos := -1
	for i, r := range member.Roles {
		if r == role.ID {
			pos = i
			break
		}
	}

	if pos < 0 {
		return fmt.Sprintf("You're already not subscribed to %s~", arg)
	}

	member.Roles = append(member.Roles[:pos], member.Roles[pos+1:]...)
	err = s.GuildMemberEdit(GuildID, UserID, member.Roles)
	if err != nil {
		fmt.Println(err)
		return "I can't touch that group dude, do it yourself~"
	}

	delete := true
	for _, member := range g.Members {
		if member.User.ID == UserID {
			continue // Ignore self since it's not updated here yet
		}

		for _, r := range member.Roles {
			if r == role.ID {
				delete = false
				break
			}
		}
	}

	fmt.Println("Should delete it?", delete)

	if delete {
		err := s.GuildRoleDelete(GuildID, role.ID)
		if err != nil {
			fmt.Println(err)
			return fmt.Sprintf("Unsubscribed from but failed to delete %s~", arg)
		}

		fmt.Println("Unsubscribed and deleted")
		return fmt.Sprintf("Unsubscribed from and deleted %s~", arg)
	}

	fmt.Println("Unsubscribed")
	return fmt.Sprintf("Unsubscribed from %s~", arg)
}
func handleTeamSet(s *discordgo.Session, givenTeamName *string, authorID *string, channelID *string) {
	// validate a team option was provided to the command
	if *givenTeamName == "" {
		s.ChannelMessageSend(*channelID, "A team must be provided to this command.")
		return
	}

	// validate the team provided matches a valid option
	hasTeam := false
	for _, team := range teams {
		if *givenTeamName == team {
			hasTeam = true
		}
	}

	if !hasTeam {
		s.ChannelMessageSend(*channelID, "The team selected ("+*givenTeamName+") does not exist. Valid options are: "+strings.Join(teams, ", ")+".")
		return
	}

	currChannel, err := s.Channel(*channelID)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"channelId":     *channelID,
			"capturedError": err,
		}).Error("Could not parse channel identifier into channel object.")
		s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.")
		return
	}

	currGuild, err := s.Guild(currChannel.GuildID)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"guildId":       currChannel.GuildID,
			"capturedError": err,
		}).Error("Could not parse guild identifier into guild object.")
		s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.")
		return
	}

	currMember, err := s.GuildMember(currGuild.ID, *authorID)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"guildId":       currGuild.ID,
			"userId":        *authorID,
			"capturedError": err,
		}).Error("Could not parse guild identifier and user identifier into guild member object.")
		s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.")
		return
	}

	// ensure they are not an existing member of a team
	for _, roleID := range currMember.Roles {
		var role *discordgo.Role
		for _, guildRole := range currGuild.Roles {
			if guildRole.ID == roleID {
				role = guildRole
			}
		}
		if role == nil {
			logrus.WithFields(logrus.Fields{
				"roleId": roleID,
			}).Error("Could not retrieve role object from identifier.")
			s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.")
			return
		}

		for _, teamName := range teams {
			if role.Name == "Team "+strings.Title(teamName) {
				logrus.WithFields(logrus.Fields{
					"roleName": role.Name,
					"roleId":   role.ID,
					"userId":   currMember.User.ID,
					"userName": currMember.User.Username,
					"guildId":  currGuild.ID,
				}).Warn("Attempted team switch detected.")
				s.ChannelMessageSend(*channelID, "You are already a member of a team and cannot switch now. Ask an administrator if you have made a mistake selecting teams.")
				return
			}
		}
	}

	// add the role to the user
	teamTitle := strings.Title(*givenTeamName)
	for _, role := range currGuild.Roles {
		if role.Name == "Team "+teamTitle {
			err := s.GuildMemberEdit(currGuild.ID, *authorID, append(currMember.Roles, role.ID))
			if err != nil {
				logrus.WithFields(logrus.Fields{
					"guildId":       currGuild.ID,
					"userId":        *authorID,
					"roleId":        role.ID,
					"capturedError": err,
				}).Error("Could not add team role to user.")
				s.ChannelMessageSend(*channelID, "An error occurred while setting your team. Please wait a moment and try again.")
				return
			}
			s.ChannelMessageSend(*channelID, "You have been set to Team "+teamTitle+".")
		}
	}
}