Skip to content

Commit fc47afd

Browse files
committedMay 25, 2024
fix: catch joinrole crashes, update pg connection off deprecated method
1 parent 7ac83d7 commit fc47afd

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed
 

‎src/bot.ts

+30-21
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import { InteractionHandler } from './services/interactionHandler';
55
import { LogService } from './services/logService';
66
import { PermissionService } from './services/permissionService';
77
import { ReactionHandler } from './services/reactionHandler';
8-
import { createConnection } from 'typeorm';
8+
import { DataSource } from 'typeorm';
99
import { Category, GuildConfig, JoinRole, ReactMessage, ReactRole } from './database/entities';
1010

1111
import * as Discord from 'discord.js';
1212
import { DELETE_JOIN_ROLE, GET_GUILD_JOIN_ROLES } from './database/queries/joinRole.query';
1313
import { DELETE_REACT_MESSAGE_BY_ROLE_ID } from './database/queries/reactMessage.query';
1414
import { DELETE_REACT_ROLE_BY_ROLE_ID } from './database/queries/reactRole.query';
1515
import { SlashCommand } from '../commands/command';
16+
import { getInfo } from 'discord-hybrid-sharding';
1617

17-
export default class RoleBot extends Discord.Client {
18+
export class RoleBot extends Discord.Client {
1819
config: typeof config;
1920
commands: Discord.Collection<string, SlashCommand>;
2021

@@ -37,7 +38,10 @@ export default class RoleBot extends Discord.Client {
3738
],
3839
// RoleBot does a lot of role "pings" for visuals, don't allow it to actually mention roles.
3940
allowedMentions: { parse: [] },
41+
shards: getInfo().SHARD_LIST,
42+
shardCount: getInfo().TOTAL_SHARDS,
4043
});
44+
4145
this.config = config;
4246
this.commands = commands();
4347

@@ -70,7 +74,7 @@ export default class RoleBot extends Discord.Client {
7074
return;
7175
}
7276

73-
guildUpdate(guild, 'Joined', this).catch((e) =>
77+
guildUpdate(guild, 'Joined').catch((e) =>
7478
this.log.error(`Failed to send webhook for guild join.\n${e}`),
7579
);
7680
});
@@ -80,7 +84,7 @@ export default class RoleBot extends Discord.Client {
8084
return;
8185
}
8286

83-
guildUpdate(guild, 'Left', this).catch((e) =>
87+
guildUpdate(guild, 'Left').catch((e) =>
8488
this.log.error(`Failed to send webhook for guild leave.\n${e}`),
8589
);
8690
});
@@ -96,13 +100,17 @@ export default class RoleBot extends Discord.Client {
96100
.catch((e) => this.log.error(e));
97101
});
98102
this.on('guildMemberAdd', async (member) => {
99-
const joinRoles = await GET_GUILD_JOIN_ROLES(member.guild.id);
103+
try {
104+
const joinRoles = await GET_GUILD_JOIN_ROLES(member.guild.id);
100105

101-
if (!joinRoles.length) return;
106+
if (!joinRoles.length) return;
102107

103-
member.roles.add(joinRoles.map((r) => r.roleId)).catch((e) => {
104-
this.log.debug(`Issue giving member join roles\n${e}`);
105-
});
108+
member.roles.add(joinRoles.map((r) => r.roleId)).catch((e) => {
109+
this.log.debug(`Issue giving member join roles\n${e}`);
110+
});
111+
} catch (e) {
112+
this.log.error(`Failed to get join roles for new member.\n${e}`, member.guild.id);
113+
}
106114
});
107115
// To help try and prevent unknown role errors
108116
this.on('roleDelete', async (role) => {
@@ -120,26 +128,27 @@ export default class RoleBot extends Discord.Client {
120128
}
121129

122130
public start = async () => {
123-
/**
124-
* Connect to postgres with all the entities.
125-
* URL points to my home server.
126-
* SYNC_DB should only be true if on dev.
127-
*/
128-
await createConnection({
131+
const dataSource = new DataSource({
129132
type: 'postgres',
130-
url: config.POSTGRES_URL,
131-
synchronize: config.SYNC_DB,
133+
host: config.POSTGRES_HOST,
134+
username: config.POSTGRES_USER,
135+
password: config.POSTGRES_PASSWORD,
136+
port: 5432,
137+
database: config.POSTGRES_DATABASE,
132138
entities: [ReactMessage, ReactRole, Category, GuildConfig, JoinRole],
133-
})
134-
.then(() => this.log.debug(`Successfully connected to postgres DB.`))
135-
.catch((e) => this.log.critical(`Failed to connect to postgres\n${e}`));
139+
logging: false,
140+
synchronize: config.SYNC_DB,
141+
});
142+
143+
await dataSource.initialize()
144+
.catch((error) => this.log.critical(`DataSource error on initialization.\n${error}`));
136145

137146
this.log.info(`Connecting to Discord with bot token.`);
138147
await this.login(this.config.TOKEN);
139148
this.log.info('Bot connected.');
140149

141150
// 741682757486510081 - New RoleBot application.
142-
await buildNewCommands(true, config.CLIENT_ID !== '741682757486510081');
151+
await buildNewCommands(false, config.CLIENT_ID !== '741682757486510081');
143152
};
144153

145154
private updatePresence = () => {

0 commit comments

Comments
 (0)
Please sign in to comment.