Skip to content

Commit

Permalink
restore: random-place command
Browse files Browse the repository at this point in the history
  • Loading branch information
StarManTheGamer committed May 24, 2023
1 parent 9a8029f commit 2ab0c6a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/commands/random.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './random/randomGame.js'
export * from './random/randomPlace.js'
export * from './random/randomUser.js'
export * from './random/randomGuild.js'
export * from './random/randomCatalog.js'
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@ import { dateUtils } from '../../utils/dateUtils.js'
import { randomUtils } from '../../utils/randomUtils.js'
import emojiUtils from '../../utils/emojiUtils.js'

export async function randomGame (message: Message, args: string[]) {
const randomData = await randomUtils.randomize('https://api.polytoria.com/v1/places/', function (response: any) {
return response.data.isActive
}, function () {
return { id: randomUtils.randomInt(1, 5200) }
}, 20)
export async function randomPlace (message: Message, args: string[]) {
const randomId = randomUtils.randomInt(1, 5200)
const apiUrl = `https://api.polytoria.com/v1/places/${randomId}`

if (randomData == null) {
return message.channel.send('Place not found, Please try again..')
const randomData = await randomUtils.randomize(
apiUrl,
function (response: any) {
return response.data
},
function () {
return { id: randomId }
},
20
)

if (!randomData || !randomData.data) {
return message.channel.send('Place not found, please try again.')
}

const data = randomData.data
const rating = data.rating
const creator = data.creator

const embed = new MessageEmbed({
title: (data.name + ' ' + (data.isFeatured === true ? emojiUtils.star : '')),
title: data.name + (data.isFeatured === true ? emojiUtils.star : ''),
description: data.description,
thumbnail: {
url: `${data.icon}`
},
url: `https://polytoria.com/places/${data.id}`,
color: '#ff5454',
image: {
},
image: {},
fields: [
{
name: 'Creator',
Expand Down
4 changes: 2 additions & 2 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { status } from './commands/status.js'
import { catalogSearch } from './commands/catalog-search.js'
import { toolbox } from './commands/toolbox.js'
import { commands } from './commands/commands.js'
import { randomUser } from './commands/random.js'
import { randomUser, randomPlace } from './commands/random.js'
import { place } from './commands/place.js'

export default {
Expand All @@ -21,7 +21,7 @@ export default {
catalog: commands,
friends: commands,
avatar: commands,
'random-game': commands,
'random-place': randomPlace,
'random-user': randomUser,
'random-guild': commands,
'random-catalog': commands,
Expand Down
58 changes: 24 additions & 34 deletions src/utils/randomUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ import axios from 'axios'
import { RandomResult } from '../../types/index.d.js'

export class randomUtils {
/**
* randomize Function
*
* @summary Randomize Function for randomizing stuff
*
* @param { string } api API Url
* @param { Function } vaildFunction Check if vaild, return True if vaild
* @param { Function } generateParams Generate random parameter
* @param { Number } maxTriedCount Request Limit
* @returns { Promise<RandomResult | null> } Result Data
*/
public static async randomize (
api: string,
vaildFunction: Function,
Expand All @@ -25,22 +14,23 @@ export class randomUtils {
while (true) {
triedCount++

const response = await axios.get(api, { params: generateParams(), validateStatus: () => { return true } })

if (response.status !== 404) {
if (response.status !== 400) {
let testResult = null
if (vaildFunction.constructor.name === 'AsyncFunction') {
testResult = await vaildFunction(response)
} else {
testResult = vaildFunction(response)
}
const response = await axios.get(api, {
params: generateParams(),
validateStatus: () => true
})

if (response.status !== 404 && response.status !== 400) {
let testResult = null
if (vaildFunction.constructor.name === 'AsyncFunction') {
testResult = await vaildFunction(response)
} else {
testResult = vaildFunction(response)
}

if (testResult === true) {
const data = response.data
resultData = data
break
}
if (testResult) {
const data = response.data
resultData = data
break
}
}

Expand All @@ -58,14 +48,14 @@ export class randomUtils {
}

/**
* randomInt
*
* @summary Random integer
*
* @param { number } min Min Number
* @param { number } max Max Number
* @returns { number } Randomized Number
*/
* randomInt
*
* @summary Random integer
*
* @param { number } min Min Number
* @param { number } max Max Number
* @returns { number } Randomized Number
*/
public static randomInt (min: number, max: number): number {
return Math.floor(Math.random() * (max - min)) + min
}
Expand Down

0 comments on commit 2ab0c6a

Please sign in to comment.