Cannot read property 'channel' of undefined - discord js
Пишу бота для дискорда, но при вводе команды "!crypto" выводит в консоль это - "Cannot read property 'channel' of undefined"
const {
Client,
Message,
MessageEmbed
} = require('discord.js');
const axios = require('axios');
module.exports = {
name: 'crypto',
aliases: [],
description: '',
usage: '',
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {
const btcPrice = axios.get('https://api.cryptowat.ch/markets/kraken/btcusd/price').then((res) => {
return res.data.result.price;
});
const ethPrice = axios.get('https://api.cryptowat.ch/markets/kraken/ethusd/price').then((res) => {
return res.data.result.price;
});
const ltcPrice = axios.get('https://api.cryptowat.ch/markets/kraken/ltcusd/price').then((res) => {
return res.data.result.price;
});
const bchPrice = axios.get('https://api.cryptowat.ch/markets/kraken/bchusd/price').then((res) => {
return res.data.result.price;
});
const adaPrice = axios.get('https://api.cryptowat.ch/markets/kraken/adausd/price').then((res) => {
return res.data.result.price;
});
const dogePrice = axios.get('https://api.cryptowat.ch/markets/kraken/dogeusd/price').then((res) => {
return res.data.result.price;
});
const emoji = ':dollar:';
const embed = new MessageEmbed()
.setTitle('Current crypto prices.')
.addFields({
name: 'BTC',
value: `${emoji} ${btcPrice}`,
inline: true
}, {
name: 'ETH',
value: `${emoji} ${ethPrice}`,
inline: true
}, {
name: 'LTC',
value: `${emoji} ${ltcPrice}`,
inline: true
}, {
name: 'BCH',
value: `${emoji} ${bchPrice}`,
inline: true
}, {
name: 'ADA',
value: `${emoji} ${adaPrice}`,
inline: true
}, {
name: 'DOGE',
value: `${emoji} ${dogePrice}`,
inline: true
}, )
.setColor('GREEN')
.setTimestamp();
message.channel.send(embed);
}
};
заранее спасибо за помощь)
Ответы (1 шт):
Автор решения: grad
→ Ссылка
В вашем примере не используются Client и Message. Также непонятно где используется функция run и что именно в неё приходит.
По документации
'use strict';
/**
* A ping pong bot, whenever you send "ping", it replies "pong".
*/
// Import the discord.js module
const Discord = require('discord.js');
// Create an instance of a Discord client
const client = new Discord.Client();
/**
* The ready event is vital, it means that only _after_ this will your bot start reacting to information
* received from Discord
*/
client.on('ready', () => {
console.log('I am ready!');
});
// Create an event listener for messages
client.on('message', message => {
// If the message is "ping"
if (message.content === 'ping') {
// Send "pong" to the same channel
message.channel.send('pong');
}
});
// Log our bot in using the token from https://discord.com/developers/applications
client.login('your token here');
Чтобы использовать message необходимо создать инстанс client. Вызывать у него метод on('message', function) и только потом уже отсылать message.channel.send('')