add reconnect, catch/dump errors, quiet debugging
This commit is contained in:
parent
3e40fc510c
commit
1c3fa927d3
1 changed files with 72 additions and 40 deletions
112
bot.js
112
bot.js
|
|
@ -35,14 +35,28 @@ 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
|
||||||
});
|
});
|
||||||
|
bot.on('error', err => console.debug({
|
||||||
|
what:"error",
|
||||||
|
where:"bot",
|
||||||
|
error:err
|
||||||
|
}));
|
||||||
|
process.on("unhandledRejection", err => console.debug({
|
||||||
|
what:"unhandledRejection",
|
||||||
|
where:"process",
|
||||||
|
error:err
|
||||||
|
}));
|
||||||
|
process.on("uncaughtException", err => console.debug({
|
||||||
|
what:"uncaughtException",
|
||||||
|
where:"process",
|
||||||
|
error:err
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
const joinAndPlay = function (msg) {
|
const joinAndPlay = function (msg) {
|
||||||
_internal_join_and_play( msg.member.voiceState.channelID, msg.channel.id);
|
_internal_join_and_play( msg.member.voiceState.channelID, msg.channel.id);
|
||||||
|
|
@ -51,19 +65,24 @@ 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.debug({
|
||||||
|
what:"catch",
|
||||||
|
where:"join",
|
||||||
|
joinID:joinID,
|
||||||
|
msgID:msgID,
|
||||||
|
error: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 };
|
||||||
|
|
||||||
ac.stop = function() { connection.stopPlaying(); };
|
ac.stop = function() { connection.stopPlaying(); };
|
||||||
ac.play = function() {
|
ac.play = function() {
|
||||||
connection.play( uri, { voiceDataTimeout: -1 });
|
connection.play( uri, { voiceDataTimeout: 17000 });
|
||||||
ac.playing = true
|
ac.playing = true
|
||||||
};
|
};
|
||||||
ac.before = function() {
|
ac.before = function() {
|
||||||
|
|
@ -77,9 +96,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,11 +118,15 @@ const _internal_join_and_play = function( joinID, msgID ) {
|
||||||
ac.start();
|
ac.start();
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log('ERROR: failed in maybeStart ' + err);
|
console.debug({
|
||||||
|
what:"catch",
|
||||||
|
where:"maybeStart",
|
||||||
|
error:err
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
activeChannels[ joinID ] = ac;
|
activeChannels[ joinID ] = ac;
|
||||||
ac.timer = setInterval( ac.maybeStart, 1000 );
|
ac.timer = setInterval( ac.maybeStart, 100 );
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -118,7 +140,13 @@ const doAutoJoin = function () {
|
||||||
_internal_join_and_play( autoJoin[i].channel );
|
_internal_join_and_play( autoJoin[i].channel );
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.log('ERROR: failed to autojoin channel #' + i + ' guild:' + autoJoin[i].guild + ' ID:'+ autoJoin[i].channel + ' -> ' + err);
|
console.debug({
|
||||||
|
what:"catch",
|
||||||
|
where:"doAutoJoin",
|
||||||
|
guild:auoJoin[i].guild,
|
||||||
|
id:autoJoin[i].channel,
|
||||||
|
error:err
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -148,8 +176,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,29 +184,28 @@ 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)' );
|
console.debug({
|
||||||
//throw new Error(`Title response status: ${response.status}`);
|
what:"request.ok",
|
||||||
console.log(`ERROR: Title status response: ${response.status}`);
|
where:"setStatus",
|
||||||
|
error:err
|
||||||
|
});
|
||||||
}
|
}
|
||||||
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}]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('ERROR: fetching title ' + err);
|
console.debug({
|
||||||
|
what:"catch",
|
||||||
|
where:"setStatus",
|
||||||
|
error:err
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let timer = setInterval( setStatus, 10000 );
|
let timer = setInterval( setStatus, 10000 );
|
||||||
|
|
@ -212,32 +238,38 @@ bot.on('messageCreate', async (msg) => {
|
||||||
} else {
|
} else {
|
||||||
joinAndPlay( msg );
|
joinAndPlay( msg );
|
||||||
}
|
}
|
||||||
|
} else if( msg.content.indexOf( 'reconnect' ) != -1) {
|
||||||
|
// request to join a channel
|
||||||
|
if( msg.member.voiceState.channelID)
|
||||||
|
joinAndPlay( msg );
|
||||||
} 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.
|
||||||
// The API might time out or choke and return a 5xx status,
|
// The API might time out or choke and return a 5xx status,
|
||||||
// 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.debug({
|
||||||
console.warn(err);
|
what:"replyMessageError",
|
||||||
|
where:"messageCreate",
|
||||||
|
error:err
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on('error', err => {
|
let autoJoinTimer;
|
||||||
console.warn(err);
|
function setAutoJoinTimeout( waitTime ) {
|
||||||
});
|
if(autoJoinTimer) return;
|
||||||
|
autoJoinTimer = setTimeout( () => {
|
||||||
|
autoJoinTimer=null;
|
||||||
|
doAutoJoin();
|
||||||
|
}, waitTime );
|
||||||
|
return autoJoinTimer;
|
||||||
|
}
|
||||||
|
|
||||||
bot.connect();
|
bot.connect();
|
||||||
|
|
||||||
const autoJoinTimer = setTimeout( doAutoJoin, 10000 );
|
setAutoJoinTimeout( 1000 );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue