Skip to content

Commit 860070a

Browse files
committed
Use guildid config, fix regression of not getting multiple roles added
1 parent 31730a4 commit 860070a

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

server/DiscordBot.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class DiscordBot extends Client {
1313

1414
if (!config)
1515
throw new Error("Configuration file not successfully injected.");
16-
16+
1717
this.tourneyConfig = config.config;
1818

1919
this.once('ready', () => {
@@ -25,7 +25,7 @@ export default class DiscordBot extends Client {
2525

2626
const channelId = this.tourneyConfig.discord.welcomeChannelId;
2727
const channel = guild.channels.cache.get(channelId) as TextChannel;
28-
28+
2929
channel.send(`Welcome ${member} you are now verified!`)
3030
} catch (e) {
3131
consola.error(e);
@@ -41,7 +41,7 @@ export default class DiscordBot extends Client {
4141

4242
await this.changeNickName(nickname, guildMember);
4343
consola.success(`Added ${nickname} nickname to ${userId}.`)
44-
44+
4545
const roles = this.tourneyConfig.discord.roles;
4646
const arr: string[] = [];
4747

@@ -55,25 +55,23 @@ export default class DiscordBot extends Client {
5555
consola.info(`Adding ${arr} roles to ${nickname}...`);
5656

5757
for (let i = 0; i < arr.length; i++) {
58-
if (!guildMember.roles.cache.has(roles[i].id)) {
59-
try {
60-
consola.info(`Adding ${roles[i].id} to ${nickname}...`);
61-
await guildMember.roles.add(arr);
62-
} catch (e) {
63-
consola.error(`Failed to add ${roles[i].id} to ${nickname}.\nReason: ${e}`)
64-
}
58+
try {
59+
consola.info(`Adding ${arr[i]} to ${nickname}...`);
60+
guildMember.roles.add(arr);
61+
} catch (e) {
62+
consola.error(`Failed to add ${arr[i]} to ${nickname}.\nReason: ${e}`)
6563
}
6664
}
6765

6866
this.emit('userVerified', guildMember.guild, guildMember);
6967

70-
} catch(e) {
68+
} catch (e) {
7169
console.error(e);
7270
}
7371
}
7472

7573
private async findGuildMember(userId: string) {
76-
const guild = this.guilds.cache.get("guildId");
74+
const guild = this.guilds.cache.get(this.tourneyConfig.discord.guildId);
7775

7876
if (guild === undefined)
7977
throw new Error("Invalid guild. Bot is likely not joined to the correct guild.");
@@ -96,7 +94,7 @@ export default class DiscordBot extends Client {
9694
return true;
9795
else
9896
return false;
99-
} catch(e) {
97+
} catch (e) {
10098
consola.error(e);
10199
return false;
102100
}

server/auth/DiscordAuth.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
import { NextFunction, Request, Response } from 'express';
22
import { Profile, Scope, Strategy, VerifyCallback } from '@oauth-everything/passport-discord';
33
import axios, { AxiosError } from 'axios';
4-
import { container, injectable } from "tsyringe";
4+
import { autoInjectable, container, injectable } from "tsyringe";
55
import { AuthenticationClient } from "./AuthenticationClient";
66
import { Client } from 'discord.js';
77
import DiscordBot from '../DiscordBot';
88
import { IUser } from './IUser';
99
import consola from "consola";
1010
import passport from "passport";
11+
import Configuration from '../Configuration';
1112

1213
@injectable()
14+
@autoInjectable()
1315
export class DiscordAuthentication extends AuthenticationClient {
1416
clientID = process.env.DISCORD_CLIENT_ID || '';
1517
clientSecret = process.env.DISCORD_CLIENT_SECRET || '';
1618
callbackURL = process.env.DISCORD_CALLBACK_URL || '';
1719
RootURL = "/discord";
18-
private guildId: string = process.env.DISCORD_GUILD_ID as string || '';
20+
private guildId = '';
1921

20-
constructor(scopes: Scope[]) {
22+
constructor(scopes: Scope[], config?: Configuration) {
2123
super();
2224

2325
if (!this.VarsPresent())
2426
return;
2527

28+
if (!config)
29+
throw new Error("Configuration file not successfully injected.");
30+
31+
this.guildId = config.config.discord.guildId;
32+
2633
if (scopes.includes(Scope.GUILDS_JOIN) && this.StrIsEmpty(this.guildId)) {
2734
consola.error(`Cannot use scope ${Scope.GUILDS_JOIN} when no guild id present.`);
2835
return;

0 commit comments

Comments
 (0)