简体   繁体   English

如何向频道发送消息

[英]How to send a message to a channel

I've spent 3 hours building and tweaking a Node.js web scraper and over 4 hours trying to find a freaking way to broadcast a message to a channel on Discord.我花了 3 个小时构建和调整 Node.js 网络抓取工具,并花了 4 个多小时试图找到一种将消息广播到 Discord 频道的怪异方法。 I have lost all hope at this time...这个时候我已经失去了所有的希望......

This is the code I have, and some parts work, like replying to a message.这是我拥有的代码,有些部分可以工作,例如回复消息。 But I can't find any possible way to just send a message, without that message being a reply.但是我找不到任何可能的方式来发送消息,而该消息不是回复。

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);

That might be caused by the fact that you're running your code before the bot is logged in.这可能是由于您在机器人登录之前运行代码这一事实造成的。
Every action has to be made after the bot has emitted the ready event, the only thing you can do outside the ready event is defining other event listeners.每个动作都必须在机器人发出就绪事件后进行,在ready事件之外你唯一能做的就是定义其他事件侦听器。

Try putting that part of your code inside the ready event listener, or inside a function called by that event:尝试将那部分代码放入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);

In your situation:在你的情况下:

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')

Try:尝试:

bot.channels.find(channel => channel.id === '504658757419270144').send('Your-message');

also, if the channel you are trying to send the message to is in the bots guild, you can use:此外,如果您尝试向其发送消息的频道在 bots 公会中,您可以使用:

bot.on('message' (msg) => {
    msg.guild.channels.find(channel => channel.name === 'your-channel-name').send('your-message');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM