Skip to content

Commit

Permalink
Added createDiscordClient to discord-bot, changed backend to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec LaLonde committed Mar 9, 2021
1 parent 8a1ea64 commit 24765d0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docker/backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY schema.graphql .
COPY tsconfig.base.json .
COPY packages/backend/*.json ./packages/backend/
COPY packages/utils/*.json ./packages/utils/
COPY packages/discord-bot/*.json ./packages/discord-bot/

RUN yarn install --pure-lockfile

Expand All @@ -23,6 +24,7 @@ FROM base as build
# Copy source files
COPY packages/backend ./packages/backend/
COPY packages/utils ./packages/utils/
COPY packages/discord-bot ./packages/discord-bot/
COPY packages/@types ./packages/@types/

# Build
Expand Down
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"license": "ISC",
"dependencies": {
"3box": "1.22.2",
"@metafam/discord-bot": "0.1.0",
"@metafam/utils": "1.0.0",
"bluebird": "3.7.2",
"body-parser": "1.19.0",
Expand Down
17 changes: 8 additions & 9 deletions packages/backend/src/handlers/triggers/updateDiscordRole.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import Discord, { Role } from 'discord.js';
import { createDiscordClient } from '@metafam/discord-bot';
import { Role } from 'discord.js';

import { CONFIG } from '../../config';
import { AccountType_Enum, Player, PlayerRank_Enum } from '../../lib/autogen/hasura-sdk';
import { client } from '../../lib/hasuraClient';
import { TriggerPayload } from './types';
Expand Down Expand Up @@ -30,17 +30,16 @@ export const updateDiscordRole = async (

// look up guild by guildname = 'metagame' (for now),
const getGuildResponse = await client.GetGuild({guildname: 'metafam'});
const discordAccount = getGuildResponse.guild[0]?.guild_accounts?.find(a => a.type === AccountType_Enum.Discord);
if (discordAccount == null) return;
const discordGuildAccount = getGuildResponse.guild[0]?.guild_accounts?.find(a => a.type === AccountType_Enum.Discord);
if (discordGuildAccount == null) return;

// call discord API. We'll need serverId, playerId, and roleIds
const discordClient = new Discord.Client();
discordClient.login(CONFIG.discordBotToken);
// instantiate discord client. We'll need serverId, playerId, and roleIds
const discordClient = await createDiscordClient();

// todo add jsonb field to guild and populate ranks, e.g. { ranks: [] }
const guild = await discordClient.guilds.fetch(discordAccount.identifier);
const guild = await discordClient.guilds.fetch(discordGuildAccount.identifier);
if (guild == null) {
console.warn(`No discord server found matching ${discordAccount.identifier}!`);
console.warn(`No discord server found matching ${discordGuildAccount.identifier}!`);
return;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"references": [
{
"path": "../utils"
},
{
"path": "../discord-bot"
}
],
"include": ["./src"]
Expand Down
1 change: 1 addition & 0 deletions packages/discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.1.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "node ./dist/index.js",
"build": "tsc -b",
Expand Down
8 changes: 6 additions & 2 deletions packages/discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Client } from '@typeit/discord';

import { CONFIG } from './config';

async function start() {
async function createDiscordClient(): Promise<Client> {
const client = new Client({
classes: [
`${__dirname}/*Discord.ts`, // glob string to load the classes
Expand All @@ -14,6 +14,10 @@ async function start() {
});

await client.login(CONFIG.discordBotToken);

return client;
}

start();
createDiscordClient();

export { createDiscordClient };

0 comments on commit 24765d0

Please sign in to comment.