Я потратил 3 часа на строительство и настройку веб-скребка Node.js и более 4 часов, пытаясь найти волнующий способ трансляции сообщения на канал на Discord. В это время я потерял всякую надежду...
Это код, который у меня есть, и некоторые части работают, как ответ на сообщение. Но я не могу найти какой-либо возможный способ просто отправить сообщение без ответа этого сообщения.
const discord = require('discord.js');
const bot = new discord.Client();
const cfg = require('./config.json')
bot.on('ready', () => {//this works
console.log('Logged in as ${bot.user.tag}(${bot.user.id}) on ${bot.guilds.size} servers')
});
bot.on('message', (msg) => {
switch (msg.content) {
case '/epicinfo':
msg.channel.send('w00t'); //this works
}
});
console.log(bot.users.get("id", "504658757419270144")) //undefined
console.log(bot.channels.get("name", "testbot")) //undefined
console.log(bot.users.find("id", "504658757419270144")) //undefined
console.log(bot.channels.find("name", "testbot")) //undefined
console.log(bot.guilds.get("504658757419270144")); //undefined
console.log(bot.channels.get(504658757419270144)) //undefined
bot.send((discord.Object(id = '504658757419270144'), 'Hello')) //discord.Object is not a function
bot.login(cfg.token);
Это может быть вызвано тем фактом, что вы запускаете свой код до того, как бот вошел в систему.
Каждое действие должно быть выполнено после того, как бот выпустил готовое событие, единственное, что вы можете сделать вне ready
события, - это определение других прослушивателей событий.
Попробуйте поместить эту часть кода в ready
прослушиватель событий или внутри функции, вызванной этим событием:
client.on('ready', () => {
console.log("Your stuff...");
});
// OR
function partA () {...}
function partB () {...}
client.on('ready', () => {
partA();
console.log("Your stuff...");
partB();
});
// OR
function load() {...}
client.on('ready', load);
В вашей ситуации:
client.on('ready', () => { // once the client is ready...
let guild = client.guilds.get('guild id here'); // ...get the guild.
if (!guild) throw new Error("The guild does not exist."); // if the guild doesn't exist, exit.
let channel = guild.channels.get('channel id here'); // if it does, get the channel
if (!channel) throw new Error("That channel does not exist in this guild."); // if it doesn't exist, exit.
channel.send("Your message here.") // if it does, send the message.
});
client.login('your token here')
Пытаться:
bot.channels.find(channel => channel.id === '504658757419270144').send('Your-message');
также, если канал, который вы пытаетесь отправить сообщение, находится в гильдии ботов, вы можете использовать:
bot.on('message' (msg) => {
msg.guild.channels.find(channel => channel.name === 'your-channel-name').send('your-message');
});
function load() { paste your console.log()s here }
затем в событие ready добавьтеload()
в новую строку.