@@ -6,6 +6,8 @@
|
||||
|
||||
var BARE_OS_DISCORD_REPLY_MAX = 1900
|
||||
var BARE_OS_DISCORD_FS_MAX = 12 * 1024
|
||||
/** discord.js MessageFlags.Ephemeral (1 << 6). Avoid the deprecated `ephemeral` option. */
|
||||
var BARE_OS_DISCORD_FLAG_EPHEMERAL = 64
|
||||
var BARE_OS_DISCORD_WHITELIST_DENY =
|
||||
'Access denied. Your Discord user id is not on DISCORD_ID_WHITELIST.'
|
||||
var BARE_OS_DISCORD_RUN_ALLOW = {
|
||||
@@ -29,6 +31,66 @@ var BARE_OS_DISCORD_RUN_ALLOW = {
|
||||
'uname -a': 1
|
||||
}
|
||||
|
||||
/**
|
||||
* bare-process has no process.emitWarning. discord.js 14 calls it whenever
|
||||
* reply options include the deprecated `ephemeral` key (even when false).
|
||||
*/
|
||||
function discordInstallProcessEmitWarning(proc) {
|
||||
const p =
|
||||
proc ||
|
||||
(typeof globalThis.process !== 'undefined' ? globalThis.process : null)
|
||||
if (!p || typeof p.emitWarning === 'function') return p
|
||||
p.emitWarning = function emitWarning(warning, type, code) {
|
||||
let name = 'Warning'
|
||||
let id = ''
|
||||
let msg = ''
|
||||
if (warning && typeof warning === 'object' && warning.type && !(warning instanceof Error)) {
|
||||
name = String(warning.type || 'Warning')
|
||||
id = String(warning.code || '')
|
||||
msg = String(warning.message || warning)
|
||||
} else if (type && typeof type === 'object') {
|
||||
name = String(type.type || 'Warning')
|
||||
id = String(type.code || '')
|
||||
msg = warning instanceof Error ? warning.message : String(warning)
|
||||
} else {
|
||||
name = typeof type === 'string' ? type : 'Warning'
|
||||
id = typeof code === 'string' ? code : ''
|
||||
msg = warning instanceof Error ? warning.message : String(warning)
|
||||
}
|
||||
const line = id ? name + ' [' + id + ']: ' + msg : name + ': ' + msg
|
||||
try {
|
||||
if (typeof p.emit === 'function') {
|
||||
const err = warning instanceof Error ? warning : new Error(msg)
|
||||
err.name = name
|
||||
if (id) err.code = id
|
||||
p.emit('warning', err)
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
try {
|
||||
if (typeof console !== 'undefined' && typeof console.error === 'function') {
|
||||
console.error(line)
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
discordInstallProcessEmitWarning()
|
||||
|
||||
function discordCmdReplyPayload(result) {
|
||||
const payload = {
|
||||
content: discordCmdClip(result && result.text ? result.text : '(no output)')
|
||||
}
|
||||
if (result && result.ephemeral) {
|
||||
payload.flags = BARE_OS_DISCORD_FLAG_EPHEMERAL
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
function discordCmdClip(text, max) {
|
||||
const s = String(text == null ? '' : text)
|
||||
const n = max || BARE_OS_DISCORD_REPLY_MAX
|
||||
@@ -186,10 +248,10 @@ function discordInteractionUserId(interaction) {
|
||||
}
|
||||
|
||||
async function discordReplyWhitelistDenied(ctx, interaction) {
|
||||
const payload = {
|
||||
content: BARE_OS_DISCORD_WHITELIST_DENY,
|
||||
const payload = discordCmdReplyPayload({
|
||||
text: BARE_OS_DISCORD_WHITELIST_DENY,
|
||||
ephemeral: true
|
||||
}
|
||||
})
|
||||
try {
|
||||
if (interaction.deferred && typeof interaction.editReply === 'function') {
|
||||
await interaction.editReply(payload)
|
||||
@@ -671,10 +733,7 @@ async function discordDispatchInteraction(ctx, interaction) {
|
||||
ephemeral: true
|
||||
}
|
||||
}
|
||||
const payload = {
|
||||
content: discordCmdClip(result && result.text ? result.text : '(no output)'),
|
||||
ephemeral: Boolean(result && result.ephemeral)
|
||||
}
|
||||
const payload = discordCmdReplyPayload(result)
|
||||
try {
|
||||
if (interaction.deferred && typeof interaction.editReply === 'function') {
|
||||
await interaction.editReply(payload)
|
||||
@@ -697,7 +756,10 @@ var bareOsDiscordCommands = {
|
||||
buildSlashCommands: discordBuildSlashCommands,
|
||||
dispatchInteraction: discordDispatchInteraction,
|
||||
parseIdWhitelist: discordParseIdWhitelist,
|
||||
userAllowed: discordUserAllowed
|
||||
userAllowed: discordUserAllowed,
|
||||
installProcessEmitWarning: discordInstallProcessEmitWarning,
|
||||
replyPayload: discordCmdReplyPayload,
|
||||
FLAG_EPHEMERAL: BARE_OS_DISCORD_FLAG_EPHEMERAL
|
||||
}
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = bareOsDiscordCommands
|
||||
|
||||
Reference in New Issue
Block a user