Skip to content

Commit

Permalink
Add slash-create, fix dashboard channel menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Aug 3, 2021
1 parent f7d0451 commit 62fa8d4
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ typings/

# trivia lists
triviaLists/

.vercel
13 changes: 8 additions & 5 deletions dashboard/imports/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const decrypt = (data: Buffer): Buffer => {
const cipher = createDecipheriv('aes-256-ctr', encryptionKey, data.slice(0, 16))
return Buffer.concat([cipher.update(data.slice(16)), cipher.final()])
}
interface TextChannel { id: string, name: string }
const getMutualPermissionGuilds = async (id: string, guilds: string[], host = false
): Promise<Array<{ id: string, perm: boolean }>> => {
): Promise<Array<{ id: string, perm: boolean, textChannels: TextChannel[] }>> => {
if (botApiUrl) {
let body: Buffer
try {
Expand All @@ -50,13 +51,16 @@ const getMutualPermissionGuilds = async (id: string, guilds: string[], host = fa
return JSON.parse(decrypt(Buffer.from(await request.arrayBuffer())).toString('utf8'))
} catch (e) { throw new ApolloError('Failed to make request to IveBot private API!') }
} else {
const mutualGuildsWithPerm: Array<{ id: string, perm: boolean }> = []
const mutualGuildsWithPerm: Array<{ id: string, perm: boolean, textChannels: TextChannel[] }> = []
await Promise.all(guilds.map(async guild => {
try {
const fullGuild = await botClient.getRESTGuild(guild)
const selfMember = await botClient.getRESTGuildMember(guild, id)
const perm = host || fullGuild.permissionsOf(selfMember).has('manageGuild')
mutualGuildsWithPerm.push({
id: guild, perm: host || fullGuild.permissionsOf(selfMember).has('manageGuild')
id: guild,
perm,
textChannels: perm ? fullGuild.channels.filter(c => c.type === 0).map(c => ({ id: c.id, name: c.name })) : []
})
} catch (e) {
if (e.name === 'DiscordHTTPError') throw new ApolloError('Failed to make Discord request!')
Expand Down Expand Up @@ -167,8 +171,7 @@ export default {
id: guild.id,
name: guild.name,
icon: guild.iconURL || 'no icon',
channels: guild.channels.filter(i => i.type === 0)
.map(i => ({ id: i.id, name: i.name })),
channels: mutual.textChannels,
perms: mutual.perm
}
}).filter(e => !!e)
Expand Down
11 changes: 0 additions & 11 deletions dashboard/now.json

This file was deleted.

32 changes: 22 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"mathjs": "^9.4.3",
"moment": "^2.24.0",
"mongodb": "^4.0.1",
"node-fetch": "^2.6.1"
"node-fetch": "^2.6.1",
"slash-create": "^3.4.1"
},
"devDependencies": {
"@types/node": "^16.4.7",
Expand All @@ -52,16 +53,27 @@
},
"graphql-schema-linter": {
"rules": [
"arguments-have-descriptions", "defined-types-are-used",
"deprecations-have-a-reason", "descriptions-are-capitalized",
"enum-values-all-caps", "enum-values-have-descriptions",
"enum-values-sorted-alphabetically", "fields-are-camel-cased",
"fields-have-descriptions", "input-object-fields-sorted-alphabetically",
"input-object-values-are-camel-cased", "input-object-values-have-descriptions",
"arguments-have-descriptions",
"defined-types-are-used",
"deprecations-have-a-reason",
"descriptions-are-capitalized",
"enum-values-all-caps",
"enum-values-have-descriptions",
"enum-values-sorted-alphabetically",
"fields-are-camel-cased",
"fields-have-descriptions",
"input-object-fields-sorted-alphabetically",
"input-object-values-are-camel-cased",
"input-object-values-have-descriptions",
"interface-fields-sorted-alphabetically",
"types-are-capitalized", "types-have-descriptions"
"types-are-capitalized",
"types-have-descriptions"
],
"disabledRules": ["type-fields-sorted-alphabetically"],
"schemaPaths": ["dashboard/schema.graphql"]
"disabledRules": [
"type-fields-sorted-alphabetically"
],
"schemaPaths": [
"dashboard/schema.graphql"
]
}
}
21 changes: 18 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ await client.connect()

// Start private HTTP API for the dashboard.
if (jwtSecret) {
interface DiscordServerResponse {
id: string
perm: boolean
textChannels: Array<{ id: string, name: string }>
}
const key = createHash('sha256').update(jwtSecret).digest()
const headers = (body: NodeJS.ArrayBufferView | string): {} => ({
'Content-Length': Buffer.byteLength(body), 'Content-Type': 'application/json'
Expand All @@ -141,22 +146,32 @@ if (jwtSecret) {
try {
const decipher = createDecipheriv('aes-256-ctr', key, buffer.slice(0, 16))
const data = Buffer.concat([decipher.update(buffer.slice(16)), decipher.final()])
const valid: Array<{ id: string, perm: boolean }> = []
const valid: DiscordServerResponse[] = []
const parsed: { id: string, host: boolean, guilds: string[] } = JSON.parse(data.toString('utf8'))
if (typeof parsed.id !== 'string' || !Array.isArray(parsed.guilds)) throw new Error()
await Promise.all(parsed.guilds.map(async id => {
if (typeof id !== 'string' || id.length <= 16) return
const guild = client.guilds.get(id)
if (!guild) return
else if (parsed.host) return valid.push({ id, perm: true }) // Fast path.
if (parsed.host) {
return valid.push({
id,
perm: true,
textChannels: guild.channels.filter(c => c.type === 0).map(c => ({ id: c.id, name: c.name }))
})
}
let member = guild.members.get(parsed.id)
if (!member) {
try {
member = await client.getRESTGuildMember(id, parsed.id)
guild.members.add(member) // Cache the member for faster lookups.
} catch (e) {} // TODO: Unable to retrieve member for the guild. Hm?
}
if (member) valid.push({ id, perm: guild.permissionsOf(member).has('manageGuild') })
if (member) {
const perm = guild.permissionsOf(member).has('manageGuild')
const textChannels = perm ? guild.channels.filter(c => c.type === 0) : []
valid.push({ id, perm, textChannels: textChannels.map(c => ({ id: c.id, name: c.name })) })
}
}))
randomBytes(16, (err, iv) => {
if (err) {
Expand Down
46 changes: 39 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
dependencies:
regenerator-runtime "^0.13.4"

"@discordjs/collection@^0.2.0":
version "0.2.1"
resolved "https://registry.yarnpkg.com/@discordjs/collection/-/collection-0.2.1.tgz#ea4bc7b41b7b7b6daa82e439141222ec95c469b2"
integrity sha512-vhxqzzM8gkomw0TYRF3tgx7SwElzUlXT/Aa41O7mOcyN6wIJfj5JmDWaO5XGKsGSsNx7F3i5oIlrucCCWV1Nog==

"@endemolshinegroup/cosmiconfig-typescript-loader@3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@endemolshinegroup/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-3.0.2.tgz#eea4635828dde372838b0909693ebd9aafeec22d"
Expand Down Expand Up @@ -1344,6 +1349,11 @@ eventemitter3@^3.1.0:
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7"
integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q==

eventemitter3@^4.0.7:
version "4.0.7"
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==

extract-files@11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/extract-files/-/extract-files-11.0.0.tgz#b72d428712f787eef1f5193aff8ab5351ca8469a"
Expand Down Expand Up @@ -2077,6 +2087,11 @@ lodash.get@^4:
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=

lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
Expand All @@ -2087,6 +2102,11 @@ lodash.truncate@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=

lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=

lodash.without@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"
Expand Down Expand Up @@ -2628,6 +2648,11 @@ remove-trailing-separator@^1.0.1:
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
integrity sha1-wkvOKig62tW8P1jg1IJJuSN52O8=

require-all@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/require-all/-/require-all-3.0.0.tgz#473d49704be310115ce124f77383b1ebd8671312"
integrity sha1-Rz1JcEvjEBFc4ST3c4Ox69hnExI=

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
Expand Down Expand Up @@ -2760,6 +2785,18 @@ signal-exit@^3.0.2:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==

slash-create@^3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/slash-create/-/slash-create-3.4.1.tgz#3e2e5f178244c21b5cc74b3847556355420ba47e"
integrity sha512-Il9PdW1WyHGJ/ZmVBm9RzRWRiCwy31BvuAi05h+wic4hDHTrGvFpdLx24GpxG1/LjXcKPQG2h1UzKIoY42rnBA==
dependencies:
"@discordjs/collection" "^0.2.0"
eventemitter3 "^4.0.7"
lodash.isequal "^4.5.0"
lodash.uniq "^4.5.0"
require-all "^3.0.0"
tweetnacl "^1.0.3"

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down Expand Up @@ -3080,7 +3117,7 @@ tsutils@^3.21.0:
dependencies:
tslib "^1.8.1"

tweetnacl@^1.0.1:
tweetnacl@^1.0.1, tweetnacl@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==
Expand Down Expand Up @@ -3277,12 +3314,7 @@ ws@8.0.0:
resolved "https://registry.yarnpkg.com/ws/-/ws-8.0.0.tgz#550605d13dfc1437c9ec1396975709c6d7ffc57d"
integrity sha512-6AcSIXpBlS0QvCVKk+3cWnWElLsA6SzC0lkQ43ciEglgXJXiCWK3/CGFEJ+Ybgp006CMibamAsqOlxE9s4AvYA==

"ws@^5.2.0 || ^6.0.0 || ^7.0.0":
version "7.5.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.3.tgz#160835b63c7d97bfab418fc1b8a9fced2ac01a74"
integrity sha512-kQ/dHIzuLrS6Je9+uv81ueZomEwH0qVYstcAQ4/Z93K8zeko9gtAbttJWzoC5ukqXY1PpoouV3+VSOqEAFt5wg==

ws@^7.2.1:
"ws@^5.2.0 || ^6.0.0 || ^7.0.0", ws@^7.2.1:
version "7.5.3"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.3.tgz#160835b63c7d97bfab418fc1b8a9fced2ac01a74"
integrity sha512-kQ/dHIzuLrS6Je9+uv81ueZomEwH0qVYstcAQ4/Z93K8zeko9gtAbttJWzoC5ukqXY1PpoouV3+VSOqEAFt5wg==
Expand Down

0 comments on commit 62fa8d4

Please sign in to comment.