@@ -334,15 +334,21 @@ async function run(ctx, argv) {
|
||||
})
|
||||
}
|
||||
|
||||
let loggingIn = true
|
||||
let loginTimer = null
|
||||
let loginTick = null
|
||||
|
||||
if (Events.Warn) {
|
||||
client.on(Events.Warn, function (message) {
|
||||
ctx.console.error('discord-bot: ' + String(message || ''))
|
||||
})
|
||||
}
|
||||
|
||||
if (debugOn && Events.Debug) {
|
||||
if (Events.Debug) {
|
||||
client.on(Events.Debug, function (message) {
|
||||
ctx.console.log('discord-bot: ' + String(message || ''))
|
||||
if (loggingIn || debugOn) {
|
||||
ctx.console.log('discord-bot: ' + String(message || ''))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -355,8 +361,33 @@ async function run(ctx, argv) {
|
||||
)
|
||||
}
|
||||
|
||||
let loggingIn = true
|
||||
let loginTimer = null
|
||||
if (typeof REST === 'function' && Routes && Routes.gatewayBot) {
|
||||
ctx.console.log('discord-bot: checking Discord REST /gateway/bot...')
|
||||
try {
|
||||
const restProbe = new REST({ timeout: 15000 }).setToken(loaded.token)
|
||||
const gw = await restProbe.get(Routes.gatewayBot())
|
||||
ctx.console.log(
|
||||
'discord-bot: REST ok gateway=' +
|
||||
String((gw && gw.url) || '?') +
|
||||
' shards=' +
|
||||
String((gw && gw.shards) || '?')
|
||||
)
|
||||
} catch (err) {
|
||||
ctx.console.error(
|
||||
'discord-bot: REST /gateway/bot failed: ' + discordErrText(err)
|
||||
)
|
||||
const status = err && (err.status || err.statusCode)
|
||||
if (status === 401 || (err && err.code === 'TokenInvalid')) {
|
||||
ctx.console.error(
|
||||
'Invalid token: Developer Portal → Bot → Reset Token (not the OAuth2 client secret).'
|
||||
)
|
||||
}
|
||||
ctx.exitCode = 1
|
||||
await Promise.resolve(client.destroy()).catch(function () {})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
function discordGatewayFatal(event) {
|
||||
const code = event && typeof event === 'object' ? event.code : event
|
||||
if (code === 4014) {
|
||||
@@ -384,19 +415,33 @@ async function run(ctx, argv) {
|
||||
try {
|
||||
await Promise.race([
|
||||
client.login(loaded.token),
|
||||
new Promise(function (resolve) {
|
||||
client.once(Events.ClientReady, function () {
|
||||
resolve('ready')
|
||||
})
|
||||
}),
|
||||
new Promise(function (_, reject) {
|
||||
let waited = 0
|
||||
loginTick = setInterval(function () {
|
||||
waited += 5
|
||||
if (!loggingIn) return
|
||||
ctx.console.log(
|
||||
'discord-bot: still connecting (' +
|
||||
waited +
|
||||
's / ' +
|
||||
loginMs / 1000 +
|
||||
's)...'
|
||||
)
|
||||
}, 5000)
|
||||
loginTimer = setTimeout(function () {
|
||||
reject(
|
||||
new Error(
|
||||
'login timed out after ' +
|
||||
loginMs +
|
||||
'ms (REST/gateway). Retry with --debug or DISCORD_DEBUG=1.'
|
||||
'ms (gateway ready). Last lines above are discord.js debug.'
|
||||
)
|
||||
)
|
||||
}, loginMs)
|
||||
if (loginTimer && typeof loginTimer.unref === 'function') {
|
||||
loginTimer.unref()
|
||||
}
|
||||
if (Events.ShardError) {
|
||||
client.on(Events.ShardError, function (error) {
|
||||
if (!loggingIn) return
|
||||
@@ -429,11 +474,13 @@ async function run(ctx, argv) {
|
||||
ctx.exitCode = 1
|
||||
loggingIn = false
|
||||
if (loginTimer) clearTimeout(loginTimer)
|
||||
if (loginTick) clearInterval(loginTick)
|
||||
await Promise.resolve(client.destroy()).catch(function () {})
|
||||
return
|
||||
}
|
||||
loggingIn = false
|
||||
if (loginTimer) clearTimeout(loginTimer)
|
||||
if (loginTick) clearInterval(loginTick)
|
||||
|
||||
await new Promise(function (resolve) {
|
||||
function shutdown() {
|
||||
|
||||
Reference in New Issue
Block a user