mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-15 01:49:10 +00:00
add server list connection to discord
This commit is contained in:
parent
6840446cdf
commit
13cd3bd4a0
13 changed files with 138 additions and 34 deletions
14
Server/api/servers_test.js
Normal file
14
Server/api/servers_test.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
module.exports = (R, $) => {
|
||||
R.get('/api/~/relevant-servers/:user', (ctx, next) => {
|
||||
// ctx.body = 'ok'
|
||||
const srv = $.discord.getRelevantServers(ctx.params.user)
|
||||
ctx.body = $.discord.presentableServers(srv, ctx.params.user)
|
||||
return
|
||||
})
|
||||
|
||||
R.get('/api/~/roles/:server', (ctx, next) => {
|
||||
// ctx.body = 'ok'
|
||||
ctx.body = $.discord.getRoles(ctx.params.server)
|
||||
return
|
||||
})
|
||||
}
|
|
@ -29,7 +29,7 @@
|
|||
"pg": "^7.4.0",
|
||||
"pg-hstore": "^2.3.2",
|
||||
"pm2": "^2.8.0",
|
||||
"sequelize": "^4.25.1",
|
||||
"sequelize": "^4.26.0",
|
||||
"socket.io": "^2.0.4",
|
||||
"standard": "^10.0.3",
|
||||
"superagent": "^3.8.1",
|
||||
|
|
|
@ -10,13 +10,58 @@ class DiscordService extends Service {
|
|||
this.clientId = process.env.DISCORD_CLIENT_ID
|
||||
this.clientSecret = process.env.DISCORD_CLIENT_SECRET
|
||||
this.oauthCallback = process.env.OAUTH_AUTH_CALLBACK
|
||||
this.botCallback = `${ctx.config.appUrl}/oauth/bot/callback`
|
||||
this.botCallback = `${ctx.config.appUrl}/api/oauth/bot/callback`
|
||||
|
||||
this.client = new discord.Client()
|
||||
|
||||
this.startBot()
|
||||
}
|
||||
|
||||
async startBot () {
|
||||
await discord.login(this.botToken)
|
||||
await this.client.login(this.botToken)
|
||||
}
|
||||
|
||||
getRelevantServers (userId) {
|
||||
return this.client.guilds.filter((g) => g.members.has(userId))
|
||||
}
|
||||
|
||||
presentableServers (collection, userId) {
|
||||
return collection.map((server) => {
|
||||
const gm = server.members.get(userId)
|
||||
|
||||
return {
|
||||
id: server.id,
|
||||
gm: {
|
||||
nickname: gm.nickname,
|
||||
color: gm.displayHexColor
|
||||
},
|
||||
server: {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
ownerID: server.ownerID,
|
||||
icon: server.icon
|
||||
},
|
||||
perms: this.getPermissions(gm)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
presentableRoles (serverId) {
|
||||
return this.client.guilds.get(serverId).roles.map((role) => {
|
||||
return {
|
||||
color: role.hexColor,
|
||||
position: role.calculatedPosition,
|
||||
id: role.id,
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
getPermissions (gm) {
|
||||
return {
|
||||
isAdmin: gm.permissions.hasPermission('ADMINISTRATOR'),
|
||||
canManageRoles: gm.permissions.hasPermission('MANAGE_ROLES', false, true)
|
||||
}
|
||||
}
|
||||
|
||||
// oauth step 2 flow, grab the auth token via code
|
||||
|
@ -42,31 +87,31 @@ class DiscordService extends Service {
|
|||
}
|
||||
|
||||
// on sign out, we revoke the token we had.
|
||||
async revokeAuthToken (code, state) {
|
||||
const url = 'https://discordapp.com/api/oauth2/revoke'
|
||||
try {
|
||||
const rsp =
|
||||
await superagent
|
||||
.post(url)
|
||||
.send({
|
||||
client_id: this.clientId,
|
||||
client_secret: this.clientSecret,
|
||||
grant_type: 'authorization_code',
|
||||
code: code,
|
||||
redirect_uri: this.oauthCallback
|
||||
})
|
||||
// async revokeAuthToken (code, state) {
|
||||
// const url = 'https://discordapp.com/api/oauth2/revoke'
|
||||
// try {
|
||||
// const rsp =
|
||||
// await superagent
|
||||
// .post(url)
|
||||
// .send({
|
||||
// client_id: this.clientId,
|
||||
// client_secret: this.clientSecret,
|
||||
// grant_type: 'authorization_code',
|
||||
// code: code,
|
||||
// redirect_uri: this.oauthCallback
|
||||
// })
|
||||
|
||||
return rsp.body
|
||||
} catch (e) {
|
||||
this.log.error('getAuthToken failed', e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
// return rsp.body
|
||||
// } catch (e) {
|
||||
// this.log.error('getAuthToken failed', e)
|
||||
// throw e
|
||||
// }
|
||||
// }
|
||||
|
||||
// returns oauth authorize url with IDENTIFY permission
|
||||
// we only need IDENTIFY because we only use it for matching IDs from the bot
|
||||
getAuthUrl (state) {
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify`
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify&state=${state}`
|
||||
}
|
||||
|
||||
// returns the bot join url with MANAGE_ROLES permission
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue