add reconnect, catch/dump errors, quiet debugging
This commit is contained in:
parent
3e40fc510c
commit
40ff35c37d
1 changed files with 39 additions and 31 deletions
70
bot.js
70
bot.js
|
|
@ -35,11 +35,9 @@ const shredConfig = JSON.parse( fs.readFileSync( configFile ) );
|
||||||
|
|
||||||
function writeConfig() { fs.writeFileSync( configFile, JSON.stringify( shredConfig ) ) }
|
function writeConfig() { fs.writeFileSync( configFile, JSON.stringify( shredConfig ) ) }
|
||||||
|
|
||||||
const token = fs.readFileSync( shredConfig.tokenFile ).toString();
|
|
||||||
|
|
||||||
const uri = shredConfig.uri;
|
const uri = shredConfig.uri;
|
||||||
|
|
||||||
const bot = new eris.Client( token, {
|
const bot = new eris.Client(fs.readFileSync(shredConfig.tokenFile).toString(), {
|
||||||
seedVoiceConnections: true,
|
seedVoiceConnections: true,
|
||||||
reconnectDelay: 17000
|
reconnectDelay: 17000
|
||||||
});
|
});
|
||||||
|
|
@ -51,13 +49,13 @@ const joinAndPlay = function (msg) {
|
||||||
let activeChannels = {};
|
let activeChannels = {};
|
||||||
|
|
||||||
const _internal_join_and_play = function( joinID, msgID ) {
|
const _internal_join_and_play = function( joinID, msgID ) {
|
||||||
bot.joinVoiceChannel(joinID).catch((err) => { // Join the user's voice channel
|
bot.joinVoiceChannel(joinID).catch((err) => {
|
||||||
if(msgID) {
|
if(msgID) {
|
||||||
bot.createMessage(msgID, "Error joining voice channel: " + err.message); // Notify the user if there is an error
|
bot.createMessage(msgID, "Error joining voice channel: " + err.message);
|
||||||
}
|
}
|
||||||
console.log({'error joining voice channel': err,chan:joinID}); // Log the error
|
console.log({'error joining voice channel': err,chan:joinID}); // Log the error
|
||||||
|
console.debug(err);
|
||||||
}).then((connection) => {
|
}).then((connection) => {
|
||||||
//connection.play( uri, { voiceDataTimeout: -1 } ); // Play the file and notify the user
|
|
||||||
|
|
||||||
const ac = { playing: false, timer: false };
|
const ac = { playing: false, timer: false };
|
||||||
|
|
||||||
|
|
@ -77,9 +75,8 @@ const _internal_join_and_play = function( joinID, msgID ) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ac.after = function() {
|
ac.after = function() {
|
||||||
//if(msgID) bot.createMessage(msgID, `Finished **${uri}**`); // Say when the file has finished playing
|
|
||||||
ac.playing = false;
|
ac.playing = false;
|
||||||
console.log(`Finished in ${ joinID }`);
|
//console.log(`Finished in ${ joinID }`);
|
||||||
},
|
},
|
||||||
|
|
||||||
ac.cancel = function() {
|
ac.cancel = function() {
|
||||||
|
|
@ -100,7 +97,8 @@ const _internal_join_and_play = function( joinID, msgID ) {
|
||||||
ac.start();
|
ac.start();
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log('ERROR: failed in maybeStart ' + err);
|
console.log('ERROR: failed in maybeStart ' + err);
|
||||||
|
console.debug(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
activeChannels[ joinID ] = ac;
|
activeChannels[ joinID ] = ac;
|
||||||
|
|
@ -119,6 +117,7 @@ const doAutoJoin = function () {
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.log('ERROR: failed to autojoin channel #' + i + ' guild:' + autoJoin[i].guild + ' ID:'+ autoJoin[i].channel + ' -> ' + err);
|
console.log('ERROR: failed to autojoin channel #' + i + ' guild:' + autoJoin[i].guild + ' ID:'+ autoJoin[i].channel + ' -> ' + err);
|
||||||
|
console.debug(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -148,8 +147,7 @@ function formatTrackTitle(meta) {
|
||||||
+ encodeURI(
|
+ encodeURI(
|
||||||
filename.replace('/spokes/gw11/storage/ftp/music/','')
|
filename.replace('/spokes/gw11/storage/ftp/music/','')
|
||||||
);
|
);
|
||||||
//console.log({title:title,who:who,fileUri:fileUri,artist:artist,album:album,date:date,year:year,filename:filename});
|
//console.log({title:title,who:who,fUri:fileUri,artist:artist,album:album,date:date,year:year,fname:filename});
|
||||||
// //'[🔗]('+ fileUri +') '+ '**' +
|
|
||||||
const rv = title +' '+ formatArtist(artist,album,year||date);
|
const rv = title +' '+ formatArtist(artist,album,year||date);
|
||||||
return [rv,fileUri];
|
return [rv,fileUri];
|
||||||
}
|
}
|
||||||
|
|
@ -157,22 +155,13 @@ const setStatus = async function() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch( shredConfig.titleUri );
|
const response = await fetch( shredConfig.titleUri );
|
||||||
if(!response.ok) {
|
if(!response.ok) {
|
||||||
//await msg.channel.createMessage( 'Playing (error retreiving title)' );
|
|
||||||
//throw new Error(`Title response status: ${response.status}`);
|
|
||||||
console.log(`ERROR: Title status response: ${response.status}`);
|
console.log(`ERROR: Title status response: ${response.status}`);
|
||||||
}
|
}
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
// if(json && json.title && json.title !== title) {
|
|
||||||
// //await msg.channel.createMessage( "Playing: " + json.title );
|
|
||||||
// title = json.title;
|
|
||||||
// console.log("New track title: " + json.title);
|
|
||||||
// bot.editStatus("online", [ { name: title, type: 0, url: "https://shred.ing" }]);
|
|
||||||
// }
|
|
||||||
//const {t,ar,al,dt,yr,fn} = await response.json();
|
|
||||||
if(json) {
|
if(json) {
|
||||||
let [newTitle,newLink] = formatTrackTitle(Object.fromEntries( json ));
|
let [newTitle,newLink] = formatTrackTitle(Object.fromEntries( json ));
|
||||||
if(newTitle && newTitle != title) {
|
if(newTitle && newTitle != title) {
|
||||||
console.log("Playing[discord]: " + newTitle + ' at ' + newLink);
|
//console.log("Playing[discord]: " + newTitle + ' at ' + newLink);
|
||||||
title = newTitle;
|
title = newTitle;
|
||||||
trackUri = newLink;
|
trackUri = newLink;
|
||||||
bot.editStatus("online", [ { name: title, type: 0, url: newLink}]);
|
bot.editStatus("online", [ { name: title, type: 0, url: newLink}]);
|
||||||
|
|
@ -180,6 +169,7 @@ const setStatus = async function() {
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('ERROR: fetching title ' + err);
|
console.log('ERROR: fetching title ' + err);
|
||||||
|
console.debug(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let timer = setInterval( setStatus, 10000 );
|
let timer = setInterval( setStatus, 10000 );
|
||||||
|
|
@ -212,16 +202,12 @@ bot.on('messageCreate', async (msg) => {
|
||||||
} else {
|
} else {
|
||||||
joinAndPlay( msg );
|
joinAndPlay( msg );
|
||||||
}
|
}
|
||||||
|
} else if( msg.content.indexOf( 'reconnect' ) != -1) {
|
||||||
|
// request to join a channel
|
||||||
|
setAutoJoinTimeout( 50 );
|
||||||
} else {
|
} else {
|
||||||
// treat everything else a request for current track title
|
// treat everything else a request for current track title
|
||||||
// const response = await fetch( 'https://shred.ing/json' );
|
|
||||||
// if(!response.ok) {
|
|
||||||
// await msg.channel.createMessage( 'Playing (error retreiving title)' );
|
|
||||||
// throw new Error(`Title response status: ${response.status}`);
|
|
||||||
// }
|
|
||||||
// const json = await response.json();
|
|
||||||
await msg.channel.createMessage(title +"\n\n[Download/Permalink]("+ trackUri +")\n");
|
await msg.channel.createMessage(title +"\n\n[Download/Permalink]("+ trackUri +")\n");
|
||||||
//setStatus();
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// There are various reasons why sending a message may fail.
|
// There are various reasons why sending a message may fail.
|
||||||
|
|
@ -229,15 +215,37 @@ bot.on('messageCreate', async (msg) => {
|
||||||
// or the bot may not have permission to send the
|
// or the bot may not have permission to send the
|
||||||
// message (403 status).
|
// message (403 status).
|
||||||
console.warn('Failed to respond to mention.');
|
console.warn('Failed to respond to mention.');
|
||||||
console.warn(err);
|
console.debug(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let autoJoinTimer;
|
||||||
|
function setAutoJoinTimeout( waitTime ) {
|
||||||
|
if(autoJoinTimer) return;
|
||||||
|
autoJoinTimer = setTimeout( () => {
|
||||||
|
autoJoinTimer=null;
|
||||||
|
doAutoJoin();
|
||||||
|
}, waitTime );
|
||||||
|
return autoJoinTimer;
|
||||||
|
}
|
||||||
|
|
||||||
bot.on('error', err => {
|
bot.on('error', err => {
|
||||||
console.warn(err);
|
console.error("[ERR[bot][botError]]", err);
|
||||||
|
//setAutoJoinTimeout( 10000 )
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (err) => {
|
||||||
|
console.error("[ERR[process][unhandledRejection]]", err);
|
||||||
|
//setAutoJoinTimeout( 10000 )
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("uncaughtException", (err) => {
|
||||||
|
console.error("[ERR[process][uncaughtExcept]] ", err);
|
||||||
|
console.debug(err);
|
||||||
|
//setAutoJoinTimeout( 500 )
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.connect();
|
bot.connect();
|
||||||
|
|
||||||
const autoJoinTimer = setTimeout( doAutoJoin, 10000 );
|
setAutoJoinTimeout( 1000 );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue