mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-17 02:29:10 +00:00
[bot] break out RPC and shared packages for bot service breakout
This commit is contained in:
parent
544ae65c58
commit
50b5e334a3
31 changed files with 233 additions and 111 deletions
|
@ -1,15 +0,0 @@
|
|||
class RPCError extends Error {
|
||||
constructor (msg, code, ...extra) {
|
||||
super(msg)
|
||||
this.code = code
|
||||
this.extra = extra
|
||||
}
|
||||
|
||||
static fromResponse (body, status) {
|
||||
const e = new RPCError(body.msg, status)
|
||||
e.remoteStack = body.trace
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RPCError
|
|
@ -1,7 +1,7 @@
|
|||
// @flow
|
||||
import { type AppContext } from '../Roleypoly'
|
||||
import { type Context } from 'koa'
|
||||
import RPCError from './_error'
|
||||
import RPCError from '@roleypoly/rpc-client/error'
|
||||
|
||||
import logger from '../logger'
|
||||
const log = logger(__filename)
|
||||
|
@ -122,6 +122,29 @@ export const any = (
|
|||
silent: boolean = false
|
||||
) => (...args: any) => fn(...args)
|
||||
|
||||
export const bot = (
|
||||
$: AppContext,
|
||||
fn: (ctx: Context, ...args: any[]) => any,
|
||||
silent: boolean = false
|
||||
) => (
|
||||
ctx: Context,
|
||||
...args: any
|
||||
) => {
|
||||
const authToken: ?string = ctx.request.headers['Authorization']
|
||||
|
||||
if (authToken != null && authToken.startsWith('Bot ')) {
|
||||
if (authToken === `Bot ${$.config.sharedSecret}`) {
|
||||
return fn(ctx, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
if (!silent) {
|
||||
log.info('RPC bot check failed', logFacts(ctx))
|
||||
}
|
||||
|
||||
throw PermissionError
|
||||
}
|
||||
|
||||
type Handler = (ctx: Context, ...args: any[]) => any
|
||||
type Strategy = (
|
||||
$: AppContext,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// @flow
|
||||
import fnv from 'fnv-plus'
|
||||
import autoloader from './_autoloader'
|
||||
import RPCError from './_error'
|
||||
import RPCError from '@roleypoly/rpc-client/error'
|
||||
import type Roleypoly, { Router } from '../Roleypoly'
|
||||
import type { Context } from 'koa'
|
||||
// import logger from '../logger'
|
||||
|
@ -121,7 +121,6 @@ export default class RPCServer {
|
|||
if (err instanceof RPCError || err.constructor.name === 'RPCError') {
|
||||
// $FlowFixMe
|
||||
ctx.status = err.code
|
||||
console.log(err.code)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import { type AppContext } from '../Roleypoly'
|
|||
import { type Context } from 'koa'
|
||||
import { type Guild } from 'eris'
|
||||
import * as secureAs from './_security'
|
||||
import RPCError from './_error'
|
||||
import RPCError from '@roleypoly/rpc-client/error'
|
||||
|
||||
export default ($: AppContext) => ({
|
||||
|
||||
|
|
|
@ -6,7 +6,8 @@ import * as secureAs from './_security'
|
|||
export default ($: AppContext) => ({
|
||||
|
||||
getCurrentUser: secureAs.authed($, async (ctx: Context) => {
|
||||
return $.discord.getUserPartial(ctx.session.userId)
|
||||
const u = await $.discord.getUserPartial(ctx.session.userId)
|
||||
return u
|
||||
}),
|
||||
|
||||
isRoot: secureAs.root($, () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue