Send a message when an error occur

This commit is contained in:
loicbersier 2020-06-15 16:55:08 +02:00
parent 0b0d4a66c5
commit fd173b3653
2 changed files with 37 additions and 26 deletions

View file

@ -30,7 +30,7 @@ class samCommand extends Command {
async exec(message, args) {
args.samMessage = rand.random(args.samMessage, message);
let pitch = '';
let pitch;
if (args.samMessage.includes('[pitch:')) {
pitch = args.samMessage.split(/(\[pitch:.*?])/);
for (let i = 0, l = pitch.length; i < l; i++) {
@ -48,7 +48,7 @@ class samCommand extends Command {
pitch = 100;
}
let speed = '';
let speed;
if (args.samMessage.includes('[speed:')) {
speed = args.samMessage.split(/(\[speed:.*?])/);
for (let i = 0, l = speed.length; i < l; i++) {
@ -76,11 +76,16 @@ class samCommand extends Command {
headers: {
'Content-Type': 'audio/mpeg',
},
}).then((result) => {
const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFileSync(outputFilename, result.data);
return message.channel.send({files: [outputFilename]});
});
})
.catch((err) => {
console.error(err);
return message.channel.send(`Uh oh, an error has occured! please try again later.\n${err}`);
})
.then((result) => {
const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFileSync(outputFilename, result.data);
return message.channel.send({files: [outputFilename]});
});
}
}

View file

@ -30,7 +30,7 @@ class samvcCommand extends Command {
async exec(message, args) {
args.samMessage = rand.random(args.samMessage, message);
let pitch = '';
let pitch;
if (args.samMessage.includes('[pitch:')) {
pitch = args.samMessage.split(/(\[pitch:.*?])/);
for (let i = 0, l = pitch.length; i < l; i++) {
@ -48,7 +48,7 @@ class samvcCommand extends Command {
pitch = 100;
}
let speed = '';
let speed;
if (args.samMessage.includes('[speed:')) {
speed = args.samMessage.split(/(\[speed:.*?])/);
for (let i = 0, l = speed.length; i < l; i++) {
@ -76,25 +76,31 @@ class samvcCommand extends Command {
headers: {
'Content-Type': 'audio/mpeg',
},
}).then(async (result) => {
const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
})
.catch((err) => {
console.error(err);
return message.channel.send(`Uh oh, an error has occured! please try again later.\n${err}`);
})
fs.writeFile(outputFilename, result.data, async function(err) {
if (err) console.error(err);
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please enter a voice channel first.');
try {
const connection = await voiceChannel.join();
const dispatcher = connection.play(outputFilename);
dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave());
return null;
} catch (err) {
voiceChannel.leave();
return message.reply(`Oh no, an error occurred: \`${err.message}\`.`);
}
.then(async (result) => {
const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`;
fs.writeFile(outputFilename, result.data, async function(err) {
if (err) console.error(err);
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please enter a voice channel first.');
try {
const connection = await voiceChannel.join();
const dispatcher = connection.play(outputFilename);
dispatcher.once('finish', () => voiceChannel.leave());
dispatcher.once('error', () => voiceChannel.leave());
return null;
} catch (err) {
voiceChannel.leave();
return message.reply(`Oh no, an error occurred: \`${err.message}\`.`);
}
});
});
});
}
}