From 64e9fded123845a421cee49e843e29a7e814e42a Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 10 Feb 2025 00:45:17 +0000 Subject: [PATCH] Add teleport command --- .env.template | 3 +- commands/fun/teleport.js | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 commands/fun/teleport.js diff --git a/.env.template b/.env.template index 787c457..3c898fc 100644 --- a/.env.template +++ b/.env.template @@ -3,4 +3,5 @@ discordGuildId= discordToken= igdbClientId= igdbClientSecret= -igdbAccessToken= \ No newline at end of file +igdbAccessToken= +googleplacesapikey= \ No newline at end of file diff --git a/commands/fun/teleport.js b/commands/fun/teleport.js new file mode 100644 index 0000000..124d21b --- /dev/null +++ b/commands/fun/teleport.js @@ -0,0 +1,67 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const axios = require('axios'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('teleport') + .setDescription('Teleport to a random location somewhere in the world'), + async execute(interaction) { + + await interaction.deferReply(); + + let latitude; + let longitude; + let url; + let response; + let data; + + do + { + latitude = (Math.random() * 180) - 90; + longitude = (Math.random() * 360) - 180; + url = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${latitude},${longitude}&radius=50000&key=${process.env.googleplacesapikey}`; + response = await axios.get(url); + data = response.data; + } while (data.results.length == 0); + + if (data.results.length > 0) { + const randomPlace = data.results[Math.floor(Math.random() * data.results.length)]; + const placeName = randomPlace.name; + const placeId = randomPlace.place_id; + + const mapsLink = `https://www.google.com/maps/search/${encodeURIComponent(placeName)}`; + + const detailsUrl = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${placeId}&key=${process.env.googleplacesapikey}`; + const detailsResponse = await axios.get(detailsUrl); + const fullAddress = detailsResponse.data.result.formatted_address; + const phoneNumber = detailsResponse.data.result.formatted_phone_number || 'No phone number available'; + const website = detailsResponse.data.result.website || 'No website available'; + const rating = detailsResponse.data.result.rating ? `${detailsResponse.data.result.rating}/5` : 'No rating available'; + const openNow = detailsResponse.data.result.opening_hours?.open_now ? 'Yes' : 'No'; + + const embed = new EmbedBuilder(); + embed.setAuthor({ name: 'You have been teleported to a new location!', iconURL: interaction.user.avatarURL() }); + embed.setTitle(placeName); + embed.setDescription(`**Address:** ${fullAddress}`); + embed.addFields({ name: 'Phone Number', value: phoneNumber, inline: true }); + embed.addFields({ name: 'Website', value: website !== 'No website available' ? `[Visit Website](${website})` : website, inline: true }); + embed.addFields({ name: 'Rating', value: rating, inline: true }); + embed.addFields({ name: 'Open Now', value: openNow, inline: true }); + embed.setURL(mapsLink); + embed.setTimestamp(); + embed.setColor(0xf9be04); + + if (randomPlace.photos) + { + const photoReference = randomPlace.photos[0].photo_reference; + embed.setThumbnail(`https://maps.googleapis.com/maps/api/place/photo?maxwidth=1600&photoreference=${photoReference}&key=${process.env.googleplacesapikey}`); + } + + embed.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }); + + await interaction.editReply({ embeds: [embed] }); + } else { + await interaction.editReply('Unable to find a location to teleport to!'); + } + }, +}; \ No newline at end of file