Home > Back-end >  Sending a random sound file as a response not working
Sending a random sound file as a response not working

Time:05-25

I'm trying to make a talking ben command for my bot, and it just won't work. I want the bot to send an audio clip as a response whenever someone asks a question, and if they don't specify a question, the bot will send a "ben" sound effect. No response to the command in Discord at all.

Here's the code:

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

CodePudding user response:

First, you need to make sure that the links to the sound files are valid. You're currently using links pointing to an HTML page, not the mp3 files.

Second, you need to use an object with a files property to send a file. See the enter image description here

CodePudding user response:

in your case, the correct way to attach files to a message is like this...

first add a name to all of your new message attachments

const ben = new Discord.MessageAttachment(
  'https://www.101soundboards.com/sounds/726450-ben',
  'ben.mp3'
);

then attach these files to your message like so:

message.channel.send({ files: [ replies[result] ])

More On Message Attachments

CodePudding user response:

The links are incorrect, you used the links that have a user interface, where the user can see the audio, login, see "related" audios etc.. not the actual source audio mp3 file.

For example, that link : https://www.101soundboards.com/sounds/726450-ben is incorrect. Replace it with https://www.101soundboards.com/storage/board_sounds_rendered/726450.mp3 Do the exact same thing with every file and you're ready to go !

  • Related