mirror of
https://github.com/Mestima/luna.git
synced 2025-05-05 21:50:15 +00:00
created APIHandler, api methods are using APIHandler now
This commit is contained in:
parent
b2b1f5c09b
commit
a5dd97dccb
@ -1,10 +1,7 @@
|
||||
import axios from 'axios';
|
||||
import chalk from 'chalk';
|
||||
|
||||
axios.defaults.timeout = 1000;
|
||||
|
||||
const cat = async () => {
|
||||
return await axios.get('https://aws.random.cat/meow')
|
||||
const cat = async (httpClient) => {
|
||||
return await httpClient.get('https://aws.random.cat/meow')
|
||||
.then((res) => {
|
||||
return res.data.file;
|
||||
})
|
||||
|
@ -1,10 +1,7 @@
|
||||
import axios from 'axios';
|
||||
import chalk from 'chalk';
|
||||
|
||||
axios.defaults.timeout = 1000;
|
||||
|
||||
const fox = async () => {
|
||||
return await axios.get('https://randomfox.ca/floof/')
|
||||
const fox = async (httpClient) => {
|
||||
return await httpClient.get('https://randomfox.ca/floof/')
|
||||
.then((res) => {
|
||||
return res.data.image;
|
||||
})
|
||||
|
@ -5,7 +5,7 @@ export default {
|
||||
.setName('cat')
|
||||
.setDescription('Get a random cat!'),
|
||||
async execute(interaction, api) {
|
||||
await api.get('cat')()
|
||||
await api.handle('cat')
|
||||
.then(async (res) => {
|
||||
await interaction.reply(res);
|
||||
})
|
||||
|
@ -5,7 +5,7 @@ export default {
|
||||
.setName('fox')
|
||||
.setDescription('Get a random fox!'),
|
||||
async execute(interaction, api) {
|
||||
await api.get('fox')()
|
||||
await api.handle('fox')
|
||||
.then(async (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 PresenceHandler from './handlers/PresenceHandler.js';
|
||||
import APIService from './services/APIService.js';
|
||||
import APIHandler from './handlers/APIHandler.js';
|
||||
|
||||
config();
|
||||
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`)
|
||||
throw e;
|
||||
});
|
||||
const LunaAPIService = await new APIService().init();
|
||||
const LunaAPIHandler = await new APIHandler().init();
|
||||
|
||||
const Luna = new Discord.Client({
|
||||
allowedMentions: {
|
||||
@ -107,7 +108,7 @@ Luna.on(Discord.Events.ClientReady, async () => {
|
||||
});
|
||||
|
||||
Luna.on(Discord.Events.InteractionCreate, async (interaction) => {
|
||||
await LunaInteractionHandler.handle(interaction, LunaAPIService);
|
||||
await LunaInteractionHandler.handle(interaction, LunaAPIHandler);
|
||||
});
|
||||
|
||||
await Luna.login(TOKEN);
|
||||
|
Loading…
x
Reference in New Issue
Block a user