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