【问题标题】:send an image using discord.js使用 discord.js 发送图像
【发布时间】:2021-11-06 12:16:25
【问题描述】:

我一直在遵循一些不同的指南来编写一个简单的不和谐机器人。一切正常,除了我无法让它发送图像。我看过这些以前的问题12,但他们的解决方案对我不起作用。这是我的代码:

const {Client, Intents} = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '?';

client.once('ready', () => {
  console.log("Dog is online!");
});

client.on('messageCreate', message => {
  if(!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if(command === 'ping') {
    message.channel.send('pong!');
  }
  else if (command === 'bark') {
    message.channel.send('bark bark bark grrrr...')
  }
  else if(command === 'nick') {
    message.channel.send('grrr...');
  }
  else if (command === 'pic') {
    message.channel.send("little dog", {files: ["https://i.imgur.com/xxXXXxx.jpeg"] });
  }

});

//must be last line
client.login('');

我的客户端登录在我的编辑器中,只是不在这里分享。 “pic”命令不起作用。它显示“小狗”文本,但不发送图像。我使用 imgur 的唯一原因是我不确定您是否可以发送本地图像;如果有人知道使用本地文件的解决方案,我会接受。

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

你可以使用

files: [{ attachment: "YourImage.jpg" }] });

您也可以使用重命名图像

files: [{ attachment: <images>.toBuffer(), name: 'newName.png' }] });

例子:

message.channel.send({ files: [{ attachment: 'YourImage.png' }] });

&lt;images&gt; 是您的图像变量

const images = blablabla

【讨论】:

  • 我尝试了你的第一个建议,它仍然只是发送没有图像的文本,第二个建议给我一个错误,说“images.tobuffer 不是一个函数”
  • 用你的图片替换“图片”
  • 我有一个常量:const images = "My_dog.jpg" 我现在使用的行是:message.channel.send("little dog", {files: [{attachment: images.toBuffer()}] }); 这给了我错误TypeError: images.toBuffer is not a function
  • 我想通了,我最终只是将 imgur 链接视为一个字符串并像发送任何其他消息一样发送它。没有使用file: 或`attachment。
猜你喜欢
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 2018-08-25
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多