add reconnect, catch/dump errors, quiet debugging

This commit is contained in:
Corwin Brust 2026-05-22 12:38:48 +05:30
parent 3e40fc510c
commit fb30f8d2be

70
bot.js
View file

@ -35,11 +35,9 @@ const shredConfig = JSON.parse( fs.readFileSync( configFile ) );
function writeConfig() { fs.writeFileSync( configFile, JSON.stringify( shredConfig ) ) }
const token = fs.readFileSync( shredConfig.tokenFile ).toString();
const uri = shredConfig.uri;
const bot = new eris.Client( token, {
const bot = new eris.Client(fs.readFileSync(shredConfig.tokenFile).toString(), {
seedVoiceConnections: true,
reconnectDelay: 17000
});
@ -51,13 +49,13 @@ const joinAndPlay = function (msg) {
let activeChannels = {};
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) {
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.debug(err);
}).then((connection) => {
//connection.play( uri, { voiceDataTimeout: -1 } ); // Play the file and notify the user
const ac = { playing: false, timer: false };
@ -77,9 +75,8 @@ const _internal_join_and_play = function( joinID, msgID ) {
}
},
ac.after = function() {
//if(msgID) bot.createMessage(msgID, `Finished **${uri}**`); // Say when the file has finished playing
ac.playing = false;
console.log(`Finished in ${ joinID }`);
//console.log(`Finished in ${ joinID }`);
},
ac.cancel = function() {
@ -100,7 +97,8 @@ const _internal_join_and_play = function( joinID, msgID ) {
ac.start();
}
} catch(err) {
console.log('ERROR: failed in maybeStart ' + err);
console.log('ERROR: failed in maybeStart ' + err);
console.debug(err);
}
};
activeChannels[ joinID ] = ac;
@ -119,6 +117,7 @@ const doAutoJoin = function () {
}
catch (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(
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});
// //'[🔗]('+ fileUri +') '+ '**' +
//console.log({title:title,who:who,fUri:fileUri,artist:artist,album:album,date:date,year:year,fname:filename});
const rv = title +' '+ formatArtist(artist,album,year||date);
return [rv,fileUri];
}
@ -157,22 +155,13 @@ const setStatus = async function() {
try {
const response = await fetch( shredConfig.titleUri );
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}`);
}
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) {
let [newTitle,newLink] = formatTrackTitle(Object.fromEntries( json ));
if(newTitle && newTitle != title) {
console.log("Playing[discord]: " + newTitle + ' at ' + newLink);
//console.log("Playing[discord]: " + newTitle + ' at ' + newLink);
title = newTitle;
trackUri = newLink;
bot.editStatus("online", [ { name: title, type: 0, url: newLink}]);
@ -180,6 +169,7 @@ const setStatus = async function() {
}
} catch (err) {
console.log('ERROR: fetching title ' + err);
console.debug(err);
}
};
let timer = setInterval( setStatus, 10000 );
@ -212,16 +202,12 @@ bot.on('messageCreate', async (msg) => {
} else {
joinAndPlay( msg );
}
} else if( msg.content.indexOf( 'reconnect' ) != -1) {
// request to join a channel
setAutoJoinTimeout( 50 );
} else {
// 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");
//setStatus();
}
} catch (err) {
// 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
// message (403 status).
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 => {
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();
const autoJoinTimer = setTimeout( doAutoJoin, 10000 );
setAutoJoinTimeout( 1000 );