Cuando envío un mensaje a mi bot, arroja el siguiente error:
(node:5852) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message at c:\Users\Owner\Desktop\selfbot\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15 at c:\Users\Owner\Desktop\selfbot\node_modules\snekfetch\src\index.js:215:21 at processTicksAndRejections (internal/process/task_queues.js:97:5) (Use `node --trace-warnings ...` to show where the warning was created) <node_internals>/internal/process/warning.js:33 (node:5852) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) <node_internals>/internal/process/warning.js:33Este es mi código:
bot.on("message", async function(message) { try { if (message.author == bot.user) return; if (message.channel.type == "dm" || message.channel.type == "group") { await new Promise(resolve => setTimeout(resolve, 3000)); if (!message.channel.typing) { message.channel.startTyping(); setTimeout(function() { const URI = `http://api.brainshop.ai/get?bid=161231&key=80GaZTVrQICIxCnI&uid=[uid]&msg=${message}`; const encodedURI = encodeURI(URI); axios.get(encodedURI) .then(async response => { var data = response message.reply(response) }); message.channel.stopTyping(); }, 5000); } } } catch (e) { console.log(e) } });Estoy creando un bot de discordia que envía un mensaje generado por IA a los usuarios. Cuando envío la respuesta, dice que es un mensaje vacío. ¿No estoy analizando algo bien?
El problema es que la response es un objeto y debe enviar una cadena o un objeto con claves específicas. Como su objeto de response no tiene estas claves, el contenido está vacío.
El método get de Axios devuelve un objeto que tiene una propiedad data ; es la respuesta proporcionada por el servidor. La respuesta de la API de BrainShop es un objeto JSON, con una clave cnt . Probablemente significa contenido.
Para obtener el contenido de texto de la API, deberá usar response.data.cnt :
if (!message.channel.typing) { message.channel.startTyping(); setTimeout(async () => { const URI = `http://api.brainshop.ai/get?bid=161231&key=80GaZTVrQICIxCnI&uid=[uid]&msg=${message}`; const encodedURI = encodeURI(URI); const response = await axios.get(encodedURI); message.reply(response.data.cnt); message.channel.stopTyping(); }, 5000); }