mirror of
				https://github.com/Mestima/luna.git
				synced 2025-10-30 08:02:35 +00:00 
			
		
		
		
	created APIHandler, api methods are using APIHandler now
This commit is contained in:
		| @ -1,10 +1,7 @@ | |||||||
| import axios from 'axios'; |  | ||||||
| import chalk from 'chalk'; | import chalk from 'chalk'; | ||||||
|  |  | ||||||
| axios.defaults.timeout = 1000; | const cat = async (httpClient) => { | ||||||
|  |   return await httpClient.get('https://aws.random.cat/meow') | ||||||
| const cat = async () => { |  | ||||||
|   return await axios.get('https://aws.random.cat/meow') |  | ||||||
|     .then((res) => { |     .then((res) => { | ||||||
|       return res.data.file; |       return res.data.file; | ||||||
|     }) |     }) | ||||||
|  | |||||||
| @ -1,10 +1,7 @@ | |||||||
| import axios from 'axios'; |  | ||||||
| import chalk from 'chalk'; | import chalk from 'chalk'; | ||||||
|  |  | ||||||
| axios.defaults.timeout = 1000; | const fox = async (httpClient) => { | ||||||
|  |   return await httpClient.get('https://randomfox.ca/floof/') | ||||||
| const fox = async () => { |  | ||||||
|   return await axios.get('https://randomfox.ca/floof/') |  | ||||||
|     .then((res) => { |     .then((res) => { | ||||||
|       return res.data.image; |       return res.data.image; | ||||||
|     }) |     }) | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ export default { | |||||||
|     .setName('cat') |     .setName('cat') | ||||||
|     .setDescription('Get a random cat!'), |     .setDescription('Get a random cat!'), | ||||||
|   async execute(interaction, api) { |   async execute(interaction, api) { | ||||||
|     await api.get('cat')() |     await api.handle('cat') | ||||||
|       .then(async (res) => { |       .then(async (res) => { | ||||||
|         await interaction.reply(res); |         await interaction.reply(res); | ||||||
|       }) |       }) | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ export default { | |||||||
|     .setName('fox') |     .setName('fox') | ||||||
|     .setDescription('Get a random fox!'), |     .setDescription('Get a random fox!'), | ||||||
|   async execute(interaction, api) { |   async execute(interaction, api) { | ||||||
|     await api.get('fox')() |     await api.handle('fox') | ||||||
|       .then(async (res) => { |       .then(async (res) => { | ||||||
|         await interaction.reply(res); |         await interaction.reply(res); | ||||||
|       }) |       }) | ||||||
|  | |||||||
							
								
								
									
										26
									
								
								src/handlers/APIHandler.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/handlers/APIHandler.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | |||||||
|  | import axios from 'axios'; | ||||||
|  | import APIService from '../services/APIService.js'; | ||||||
|  |  | ||||||
|  | const APIHandler = class { | ||||||
|  |   constructor() { | ||||||
|  |     this.httpClient = axios.create(); | ||||||
|  |     this.httpClient.defaults.timeout = 1000; | ||||||
|  |  | ||||||
|  |     this.APIService = new APIService(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   async init() { | ||||||
|  |     await this.APIService.init(); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   async handle(api) { | ||||||
|  |     try { | ||||||
|  |       return await this.APIService.get(api)(this.httpClient); | ||||||
|  |     } catch (e) { | ||||||
|  |       throw e; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | export default APIHandler; | ||||||
| @ -5,6 +5,7 @@ import pjson from '../package.json' assert { type: 'json' }; | |||||||
| import InteractionHandler from './handlers/InteractionHandler.js'; | import InteractionHandler from './handlers/InteractionHandler.js'; | ||||||
| import PresenceHandler from './handlers/PresenceHandler.js'; | import PresenceHandler from './handlers/PresenceHandler.js'; | ||||||
| import APIService from './services/APIService.js'; | import APIService from './services/APIService.js'; | ||||||
|  | import APIHandler from './handlers/APIHandler.js'; | ||||||
|  |  | ||||||
| config(); | config(); | ||||||
| const TOKEN = process.env.TOKEN; | const TOKEN = process.env.TOKEN; | ||||||
| @ -24,7 +25,7 @@ const LunaInteractionHandler = await new InteractionHandler().init(TOKEN) | |||||||
|     console.log(`${chalk.red('Error:')} can not initialize InteractionHandler`) |     console.log(`${chalk.red('Error:')} can not initialize InteractionHandler`) | ||||||
|     throw e; |     throw e; | ||||||
|   }); |   }); | ||||||
| const LunaAPIService = await new APIService().init(); | const LunaAPIHandler = await new APIHandler().init(); | ||||||
|  |  | ||||||
| const Luna = new Discord.Client({ | const Luna = new Discord.Client({ | ||||||
|   allowedMentions: { |   allowedMentions: { | ||||||
| @ -107,7 +108,7 @@ Luna.on(Discord.Events.ClientReady, async () => { | |||||||
| }); | }); | ||||||
|  |  | ||||||
| Luna.on(Discord.Events.InteractionCreate, async (interaction) => { | Luna.on(Discord.Events.InteractionCreate, async (interaction) => { | ||||||
|   await LunaInteractionHandler.handle(interaction, LunaAPIService); |   await LunaInteractionHandler.handle(interaction, LunaAPIHandler); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| await Luna.login(TOKEN); | await Luna.login(TOKEN); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user