Example #1
0
func manualUnban(s *discordgo.Session, channelID string,
	roleID []string, arr []string) {

	if checkForPermissions(s, channelID, roleID) &&
		len(arr) == 3 && len(arr[2]) == 21 {
		arr[2] = arr[2][2 : len(arr[2])-1]
		pgDb := dbConn()
		tx, err := pgDb.Begin()
		checkErr(err)

		user, _ := s.User(arr[2])
		name := user.Username

		rows, err := tx.Query(
			"SELECT id, guild_id, role_id FROM bans WHERE name = $1", name)
		checkErr(err)

		flag := false
		for rows.Next() {
			flag = true
			var id, guild_id, role_id string
			rows.Scan(&id, &guild_id, &role_id)

			if len(guild_id) != 0 {
				rows.Close()
				_, err = tx.Exec("DELETE FROM bans WHERE id = $1", id)
				checkErr(err)
				s.GuildMemberEdit(guild_id, arr[2], []string{role_id})
				s.ChannelMessageSend(channelID,
					fmt.Sprintf("User %s has been unbanned", name))
				break
			}
		}
		tx.Commit()
		pgDb.Close()
		if flag == false {
			s.ChannelMessageSend(channelID,
				fmt.Sprintf("User %s is not banned", name))
		}

	} else {
		s.ChannelMessageSend(channelID,
			fmt.Sprintf("Please check the parameters"))
	}
}
Example #2
0
func checkBans(s *discordgo.Session) {
	for {
		time.Sleep(10 * time.Second)
		currentTs := int64(time.Now().Unix())

		pgDb := dbConn()
		tx, err := pgDb.Begin()
		checkErr(err)

		rows, err := tx.Query(`SELECT id, name, guild_id, role_id, user_id 
		FROM bans WHERE (time_stamp + duration) <= $1`, currentTs)
		checkErr(err)

		var id, name, guild_id, role_id, user_id string
		for rows.Next() {

			rows.Scan(&id, &name, &guild_id, &role_id, &user_id)

			if id != "" && name != "" && guild_id != "" &&
				role_id != "" && user_id != "" {
				rows.Close()
				fmt.Println("passed")
				fmt.Println(id, name, guild_id, role_id, user_id)
				_, err := tx.Exec("DELETE FROM bans WHERE id = $1", id)
				checkErr(err)

				s.GuildMemberEdit(guild_id, user_id, []string{role_id})
				break
			}

		}

		tx.Commit()
		pgDb.Close()

	}
}
Example #3
0
func banUser(s *discordgo.Session, guild *discordgo.Guild,
	author *discordgo.Member, channelID string, arr []string) {
	if len(arr) == 4 && len(arr[2]) == 21 &&
		checkForPermissions(s, channelID, author.Roles) {
		arr[2] = arr[2][2 : len(arr[2])-1]
		userRole := ""
		username := ""
		for _, member := range guild.Members {
			if arr[2] == member.User.ID {
				username = member.User.Username
				userRole = member.Roles[0]
				break
			}
		}
		duration, _ := (strconv.Atoi(arr[3]))

		pgDb := dbConn()
		tx, err := pgDb.Begin()
		checkErr(err)

		rows, err := tx.Query(
			`SELECT duration FROM bans 
			WHERE user_id = $1`, arr[2])
		checkErr(err)

		var (
			dur int
			i   int
		)

		for rows.Next() {
			i++
			rows.Scan(&dur)
		}
		rows.Close()
		if i == 0 {

			_, err = tx.Exec(
				`INSERT INTO bans(name, time_stamp, duration,
				guild_id, role_id, user_id)
				VALUES($1, $2, $3, $4, $5, $6)`,
				username, int64(time.Now().Unix()),
				duration*60, guild.ID, userRole, arr[2])
			checkErr(err)

			s.GuildMemberEdit(guild.ID, arr[2], []string{roleBannedId})
			for _, c := range guild.Channels {
				if c.Name == "AFK" {
					s.GuildMemberMove(guild.ID, arr[2], c.ID)
					break
				}
			}
			s.ChannelMessageSend(channelID,
				fmt.Sprintf(
					"User %s has been temporary banned for %d minute(s)",
					username, duration))
		} else {
			_, err = tx.Exec(
				`UPDATE bans SET duration = $1
				WHERE user_id = $2`, dur+duration*60, arr[2])
			checkErr(err)

			s.ChannelMessageSend(channelID,
				fmt.Sprintf(
					"Users %s temporary ban has been extended by %d minute(s)",
					username, duration))
		}
		tx.Commit()
		pgDb.Close()
	} else {
		s.ChannelMessageSend(channelID,
			fmt.Sprintf("Please check the parameters"))
	}
}
Example #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)
}
Example #5
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)
}
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+".")
		}
	}
}