Example #1
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"))
	}
}