发送随机声音文件作为响应无效

Sending a random sound file as a response not working

我正在尝试为我的机器人制作一个会说话的 ben 命令,但它不起作用。我希望机器人在有人提问时发送音频剪辑作为回应,如果他们没有指定问题,机器人将发送“ben”音效。完全没有对 Discord 中的命令的响应。

代码如下:

ben.js:

const Discord = require('discord.js');

const yes = new Discord.MessageAttachment(
  'https://soundboardguy.com/sounds/talking-ben-yes_scachnw/',
);
const no = new Discord.MessageAttachment(
  'https://soundboardguy.com/sounds/talking-ben-no/',
);
const laugh = new Discord.MessageAttachment(
  'https://soundboardguy.com/sounds/talking-ben-laugh/',
);
const uhh = new Discord.MessageAttachment(
  'https://www.101soundboards.com/sounds/726466-uhh',
);
const ben = new Discord.MessageAttachment(
  'https://www.101soundboards.com/sounds/726450-ben',
);

module.exports = {
  name: 'ben',
  description: 'talking ben command',

  async execute(client, message, args) {
    if (!args[0]) return ben;
    let benreplies = [yes, no, laugh, uhh];

    let result = Math.floor(Math.random() * benreplies.length);

    message.channel.send(replies[result]);
  },
};

main.js:

const Discord = require('discord.js');
const client = new Discord.Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] });

const prefix = '.';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs
  .readdirSync('./commands/')
  .filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);

  client.commands.set(command.name, command);
}

client.once('ready', () => {
  console.log('Blueberry bot 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();

  // ...

  else if (command === 'ben') {
    client.commands.get('ben').execute(message, args, Discord);
  }
});

首先,您需要确保声音文件的链接有效。您当前使用的链接指向 HTML 页面,而不是 mp3 文件。

其次,您需要使用带有 files 属性 的对象来发送文件。参见MessageOptionsfiles 也需要是一个数组。以下将起作用:

let sounds = [
  {
    id: 'ben',
    link: 'https://soundboardguy.com/wp-content/uploads/2022/03/talking-ben-ben.mp3',
  },
  {
    id: 'laugh',
    link: 'https://soundboardguy.com/wp-content/uploads/2022/02/Talking-Ben-Laughing-Sound-Effect-1.mp3',
  },
  {
    id: 'no',
    link: 'https://soundboardguy.com/wp-content/uploads/2022/03/Talking-Ben-No-Sound-Effect.mp3',
  },
  {
    id: 'uhh',
    link: 'https://soundboardguy.com/wp-content/uploads/2021/06/huh-uhh.mp3',
  },
  {
    id: 'yes',
    link: 'https://soundboardguy.com/wp-content/uploads/2022/03/talking-ben-yes_SCacHNW.mp3',
  },
];
let randomSound = sounds[Math.floor(Math.random() * sounds.length)];

message.channel.send({
  files: [new MessageAttachment(randomSound.link, `${randomSound.id}.mp3`)],
});

link 不正确,您使用的 link 具有用户界面,用户可以在其中查看音频、登录、查看“相关”音频等。不是实际源音频 mp3 文件。

例如,link : https://www.101soundboards.com/sounds/726450-ben 是不正确的。将其替换为 https://www.101soundboards.com/storage/board_sounds_rendered/726450.mp3 对每个文件执行完全相同的操作,您就可以开始了!