@@ -5,16 +5,17 @@ import { InteractionHandler } from './services/interactionHandler';
5
5
import { LogService } from './services/logService' ;
6
6
import { PermissionService } from './services/permissionService' ;
7
7
import { ReactionHandler } from './services/reactionHandler' ;
8
- import { createConnection } from 'typeorm' ;
8
+ import { DataSource } from 'typeorm' ;
9
9
import { Category , GuildConfig , JoinRole , ReactMessage , ReactRole } from './database/entities' ;
10
10
11
11
import * as Discord from 'discord.js' ;
12
12
import { DELETE_JOIN_ROLE , GET_GUILD_JOIN_ROLES } from './database/queries/joinRole.query' ;
13
13
import { DELETE_REACT_MESSAGE_BY_ROLE_ID } from './database/queries/reactMessage.query' ;
14
14
import { DELETE_REACT_ROLE_BY_ROLE_ID } from './database/queries/reactRole.query' ;
15
15
import { SlashCommand } from '../commands/command' ;
16
+ import { getInfo } from 'discord-hybrid-sharding' ;
16
17
17
- export default class RoleBot extends Discord . Client {
18
+ export class RoleBot extends Discord . Client {
18
19
config : typeof config ;
19
20
commands : Discord . Collection < string , SlashCommand > ;
20
21
@@ -37,7 +38,10 @@ export default class RoleBot extends Discord.Client {
37
38
] ,
38
39
// RoleBot does a lot of role "pings" for visuals, don't allow it to actually mention roles.
39
40
allowedMentions : { parse : [ ] } ,
41
+ shards : getInfo ( ) . SHARD_LIST ,
42
+ shardCount : getInfo ( ) . TOTAL_SHARDS ,
40
43
} ) ;
44
+
41
45
this . config = config ;
42
46
this . commands = commands ( ) ;
43
47
@@ -70,7 +74,7 @@ export default class RoleBot extends Discord.Client {
70
74
return ;
71
75
}
72
76
73
- guildUpdate ( guild , 'Joined' , this ) . catch ( ( e ) =>
77
+ guildUpdate ( guild , 'Joined' ) . catch ( ( e ) =>
74
78
this . log . error ( `Failed to send webhook for guild join.\n${ e } ` ) ,
75
79
) ;
76
80
} ) ;
@@ -80,7 +84,7 @@ export default class RoleBot extends Discord.Client {
80
84
return ;
81
85
}
82
86
83
- guildUpdate ( guild , 'Left' , this ) . catch ( ( e ) =>
87
+ guildUpdate ( guild , 'Left' ) . catch ( ( e ) =>
84
88
this . log . error ( `Failed to send webhook for guild leave.\n${ e } ` ) ,
85
89
) ;
86
90
} ) ;
@@ -96,13 +100,17 @@ export default class RoleBot extends Discord.Client {
96
100
. catch ( ( e ) => this . log . error ( e ) ) ;
97
101
} ) ;
98
102
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 ) ;
100
105
101
- if ( ! joinRoles . length ) return ;
106
+ if ( ! joinRoles . length ) return ;
102
107
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
+ }
106
114
} ) ;
107
115
// To help try and prevent unknown role errors
108
116
this . on ( 'roleDelete' , async ( role ) => {
@@ -120,26 +128,27 @@ export default class RoleBot extends Discord.Client {
120
128
}
121
129
122
130
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 ( {
129
132
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 ,
132
138
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 } ` ) ) ;
136
145
137
146
this . log . info ( `Connecting to Discord with bot token.` ) ;
138
147
await this . login ( this . config . TOKEN ) ;
139
148
this . log . info ( 'Bot connected.' ) ;
140
149
141
150
// 741682757486510081 - New RoleBot application.
142
- await buildNewCommands ( true , config . CLIENT_ID !== '741682757486510081' ) ;
151
+ await buildNewCommands ( false , config . CLIENT_ID !== '741682757486510081' ) ;
143
152
} ;
144
153
145
154
private updatePresence = ( ) => {
0 commit comments