1+ // Commands taken from https://github.com/NiaAxern/discord-youtube-subscriber-count/blob/main/src/commands/utilities.ts
2+
3+ import client from '.' ;
4+ import { type CommandInteraction } from 'discord.js' ;
5+ import { heapStats } from 'bun:jsc' ;
6+ import { getGuildLeaderboard , makeGETRequest } from './utils/requestAPI' ;
7+ import convertToLevels from './utils/convertToLevels' ;
8+ import quickEmbed from './utils/quickEmbed' ;
9+
10+ interface Command {
11+ data : {
12+ options : any [ ] ;
13+ name : string ;
14+ description : string ;
15+ integration_types : number [ ] ;
16+ contexts : number [ ] ;
17+ } ;
18+ execute : ( interaction : CommandInteraction ) => Promise < void > ;
19+ }
20+
21+ const commands : Record < string , Command > = {
22+ ping : {
23+ data : {
24+ options : [ ] ,
25+ name : 'ping' ,
26+ description : 'Check the ping of the bot!' ,
27+ integration_types : [ 0 , 1 ] ,
28+ contexts : [ 0 , 1 , 2 ] ,
29+ } ,
30+ execute : async ( interaction : { reply : ( arg0 : { ephemeral : boolean ; content : string ; } ) => Promise < any > ; client : { ws : { ping : any ; } ; } ; } ) => {
31+ await interaction
32+ . reply ( {
33+ ephemeral : false ,
34+ content : `Ping: ${ interaction . client . ws . ping } ms` ,
35+ } )
36+ . catch ( console . error ) ;
37+ } ,
38+ } ,
39+ help : {
40+ data : {
41+ options : [ ] ,
42+ name : 'help' ,
43+ description : 'Get help on what each command does!' ,
44+ integration_types : [ 0 , 1 ] ,
45+ contexts : [ 0 , 1 , 2 ] ,
46+ } ,
47+ execute : async ( interaction : { reply : ( arg0 : { ephemeral : boolean ; content : string ; } ) => Promise < any > ; } ) => {
48+ await client . application ?. commands ?. fetch ( ) . catch ( console . error ) ;
49+ const chat_commands = client . application ?. commands . cache . map ( ( a ) => {
50+ return `</${ a . name } :${ a . id } >: ${ a . description } ` ;
51+ } ) ;
52+ await interaction
53+ . reply ( {
54+ ephemeral : true ,
55+ content : `Commands:\n${ chat_commands ?. join ( '\n' ) } ` ,
56+ } )
57+ . catch ( console . error ) ;
58+ } ,
59+ } ,
60+ sourcecode : {
61+ data : {
62+ options : [ ] ,
63+ name : 'sourcecode' ,
64+ description : "Get the link of the app's source code." ,
65+ integration_types : [ 0 , 1 ] ,
66+ contexts : [ 0 , 1 , 2 ] ,
67+ } ,
68+ execute : async ( interaction : { reply : ( arg0 : { ephemeral : boolean ; content : string ; } ) => Promise < any > ; } ) => {
69+ await interaction
70+ . reply ( {
71+ ephemeral : true ,
72+ content : `[Github repository](https://github.com/GalvinPython/discord-autopublish)` ,
73+ } )
74+ . catch ( console . error ) ;
75+ } ,
76+ } ,
77+ uptime : {
78+ data : {
79+ options : [ ] ,
80+ name : 'uptime' ,
81+ description : 'Check the uptime of the bot!' ,
82+ integration_types : [ 0 , 1 ] ,
83+ contexts : [ 0 , 1 , 2 ] ,
84+ } ,
85+ execute : async ( interaction : { reply : ( arg0 : { ephemeral : boolean ; content : string ; } ) => Promise < any > ; } ) => {
86+ await interaction
87+ . reply ( {
88+ ephemeral : false ,
89+ content : `Uptime: ${ ( performance . now ( ) / ( 86400 * 1000 ) ) . toFixed (
90+ 2 ,
91+ ) } days`,
92+ } )
93+ . catch ( console . error ) ;
94+ } ,
95+ } ,
96+ usage : {
97+ data : {
98+ options : [ ] ,
99+ name : 'usage' ,
100+ description : 'Check the heap size and disk usage of the bot!' ,
101+ integration_types : [ 0 , 1 ] ,
102+ contexts : [ 0 , 1 , 2 ] ,
103+ } ,
104+ execute : async ( interaction : { reply : ( arg0 : { ephemeral : boolean ; content : string ; } ) => Promise < any > ; } ) => {
105+ const heap = heapStats ( ) ;
106+ Bun . gc ( false ) ;
107+ await interaction
108+ . reply ( {
109+ ephemeral : false ,
110+ content : [
111+ `Heap size: ${ ( heap . heapSize / 1024 / 1024 ) . toFixed ( 2 ) } MB / ${ (
112+ heap . heapCapacity /
113+ 1024 /
114+ 1024
115+ ) . toFixed ( 2 ) } MB (${ ( heap . extraMemorySize / 1024 / 1024 ) . toFixed ( 2 , ) } MB) (${ heap . objectCount . toLocaleString ( ) } objects, ${ heap . protectedObjectCount . toLocaleString ( ) } protected-objects)`,
116+ ]
117+ . join ( '\n' )
118+ . slice ( 0 , 2000 ) ,
119+ } )
120+ . catch ( console . error ) ;
121+ } ,
122+ } ,
123+ xp : {
124+ data : {
125+ options : [ ] ,
126+ name : 'xp' ,
127+ description : 'Get your XP and Points' ,
128+ integration_types : [ 0 ] ,
129+ contexts : [ 0 , 2 ] ,
130+ } ,
131+ execute : async ( interaction ) => {
132+ if ( interaction ?. guildId ) {
133+ const guild = interaction . guild ?. id
134+ const user = interaction . user . id
135+ const xp = await makeGETRequest ( guild as string , user )
136+ await interaction . reply ( {
137+ ephemeral : false ,
138+ content : `<@${ user } > you have ${ xp . xp } XP! (Level ${ convertToLevels ( xp . xp ) } )`
139+ } )
140+ }
141+ }
142+ } ,
143+ top : {
144+ data : {
145+ options : [ ] ,
146+ name : 'top' ,
147+ description : 'Get the top users for the server' ,
148+ integration_types : [ 0 ] ,
149+ contexts : [ 0 , 2 ] ,
150+ } ,
151+ execute : async ( interaction ) => {
152+ if ( interaction ?. guildId ) {
153+ const guild = interaction . guild ?. id ;
154+
155+ try {
156+ const leaderboard = await getGuildLeaderboard ( guild as string ) ;
157+
158+ if ( leaderboard . length === 0 ) {
159+ await interaction . reply ( 'No leaderboard data available.' ) ;
160+ return ;
161+ }
162+
163+ // Create a new embed using the custom embed function
164+ const leaderboardEmbed = quickEmbed ( {
165+ color : 'Blurple' ,
166+ title : `Leaderboard for ${ interaction . guild ?. name } ` ,
167+ description : 'Top 10 Users'
168+ } , interaction ) ;
169+
170+ // Add a field for each user with a mention
171+ leaderboard . forEach ( ( entry : { user_id : any ; xp : any ; } , index : number ) => {
172+ leaderboardEmbed . addFields ( [
173+ {
174+ name : `${ index + 1 } .` ,
175+ value : `<@${ entry . user_id } >: ${ entry . xp } XP` ,
176+ inline : false
177+ }
178+ ] ) ;
179+ } ) ;
180+
181+ // Send the embed
182+ await interaction . reply ( { embeds : [ leaderboardEmbed ] } ) ;
183+ } catch ( error ) {
184+ console . error ( 'Error executing command:' , error ) ;
185+ await interaction . reply ( 'There was an error retrieving the leaderboard.' ) ;
186+ }
187+ } else {
188+ await interaction . reply ( 'This command can only be used in a guild.' ) ;
189+ }
190+ }
191+ }
192+ } ;
193+
194+ // Convert commands to a Map
195+ const commandsMap = new Map < string , Command > ( ) ;
196+ for ( const key in commands ) {
197+ if ( commands . hasOwnProperty ( key ) ) {
198+ const command = commands [ key ] ;
199+ console . log ( 'loading ' + key ) ;
200+ commandsMap . set ( key , command ) ;
201+ }
202+ }
203+
204+ export default commandsMap ;
0 commit comments