11import { Api } from "@top-gg/sdk" ;
22import { Chart , registerables } from "chart.js" ;
3- import { getUserSettingsTable } from "database/userSettings" ;
3+ import { getUserSettingsTable , setUserSetting } from "database/userSettings" ;
44import { ActivityType , Client , Partials } from "discord.js" ;
5+ import { errorEmbed } from "embeds/errorEmbed" ;
6+ import ms from "enhanced-ms" ;
57import { registerGuildCommands } from "handlers/commands" ;
68import { loadAuditEvents , loadEasterEggs , loadEvents } from "handlers/events" ;
7- import ms from "ms" ;
89import { rescheduleUnbans } from "utils/unbanScheduler" ;
910
1011export const client = new Client ( {
@@ -13,6 +14,7 @@ export const client = new Client({
1314 } ,
1415 partials : [ Partials . Message , Partials . Reaction , Partials . User ] ,
1516 intents : [
17+ "DirectMessages" ,
1618 "Guilds" ,
1719 "GuildMembers" ,
1820 "GuildMessages" ,
@@ -24,32 +26,48 @@ export const client = new Client({
2426 ] ,
2527} ) ;
2628
27- export const subscribedUsers = new Set (
28- ( await getUserSettingsTable ( "topgg" , "remind" ) ) ?. filter ( i => i . value == "1" ) . map ( i => i . userID ) ,
29- ) ;
29+ client . once ( "clientReady" , async ( ) => {
30+ if ( process . env . TOPGG_TOKEN )
31+ setInterval ( async ( ) => {
32+ const topgg = new Api ( process . env . TOPGG_TOKEN ! ) ;
33+ try {
34+ await topgg . postStats ( {
35+ serverCount : ( await client . guilds . fetch ( ) ) . size ,
36+ } ) ;
37+ console . log ( "Posted statistics to top.gg!" ) ;
38+ } catch ( error ) {
39+ console . error ( `Failed to start top.gg autoposter: ${ error } ` ) ;
40+ }
3041
31- client . once ( "ready" , async ( ) => {
32- if ( process . env . TOPGG_TOKEN ) {
33- const topgg = new Api ( process . env . TOPGG_TOKEN ! ) ;
34- try {
35- await topgg . postStats ( {
36- serverCount : ( await client . guilds . fetch ( ) ) . size ,
37- } ) ;
38- console . log ( "Posted statistics to top.gg!" ) ;
39- } catch ( error ) {
40- console . error ( `Failed to start top.gg autoposter: ${ error } ` ) ;
41- }
42+ const users = client . users ;
43+ const subscribedUsers = new Set (
44+ ( await getUserSettingsTable ( "topgg" , "remind" ) )
45+ ?. filter ( i => i . value == "1" )
46+ . map ( i => i . userID ) ,
47+ ) ;
4248
43- setInterval ( async ( ) => {
4449 for ( const user of subscribedUsers ) {
45- if ( await topgg . hasVoted ( user ) ) continue ;
46- await client . users . send (
47- JSON . parse ( user ) ,
48- "Reminder that **you can vote for Sokora** on [top.gg](https://top.gg/bot/873918300726394960/vote) - go vote!!" ,
49- ) ;
50+ try {
51+ if ( await topgg . hasVoted ( user ) ) continue ;
52+ const dmChannel = await ( await users . fetch ( JSON . parse ( user ) ) ) . createDM ( ) ;
53+ if ( ! dmChannel || ! dmChannel . isSendable ( ) ) continue ;
54+
55+ await dmChannel . send (
56+ "Reminder that **you can vote for Sokora** on [top.gg](https://top.gg/bot/873918300726394960/vote) - go vote!!" ,
57+ ) ;
58+ } catch ( error ) {
59+ await errorEmbed ( {
60+ client,
61+ error,
62+ title : "top.gg reminding error." ,
63+ log : true ,
64+ forward : true ,
65+ fileName : "bot.ts" ,
66+ } ) ;
67+ await setUserSetting ( user , "topgg" , "remind" , false ) ;
68+ }
5069 }
51- } , ms ( "3h" ) ) ;
52- }
70+ } , ms ( "6h" ) ) ;
5371
5472 await Promise . all ( [
5573 loadEvents ( client ) ,
0 commit comments