11const { SlashCommandBuilder, InteractionContextType, EmbedBuilder } = require ( 'discord.js' ) ;
2- const { QuickDB } = require ( 'quick.db' ) ;
3- const db = new QuickDB ( ) ;
42
53exports . conf = {
64 permLevel : 'Administrator' ,
@@ -47,23 +45,46 @@ exports.run = async (interaction) => {
4745 . setColor ( interaction . settings . embedColor )
4846 . setAuthor ( { name : interaction . member . displayName , iconURL : interaction . member . displayAvatarURL ( ) } ) ;
4947
48+ const [ autoRoleRows ] = await interaction . client . db . execute (
49+ /* sql */ `
50+ SELECT
51+ roles
52+ FROM
53+ auto_roles
54+ WHERE
55+ server_id = ?
56+ ` ,
57+ [ interaction . guild . id ] ,
58+ ) ;
59+ let autoRoles = autoRoleRows [ 0 ] ?. roles ? JSON . parse ( autoRoleRows [ 0 ] . roles ) : [ ] ;
60+
5061 switch ( type ) {
5162 case 'add' : {
52- const autoRoles = ( await db . get ( `servers.${ interaction . guild . id } .autoRoles` ) ) || [ ] ;
5363 if ( autoRoles . includes ( role . id ) ) {
5464 embed . setDescription ( 'This role is already set as an auto-role.' ) ;
5565 return interaction . editReply ( { embeds : [ embed ] } ) ;
5666 }
5767
5868 autoRoles . push ( role . id ) ;
59- await db . set ( `servers.${ interaction . guild . id } .autoRoles` , autoRoles ) ;
69+ await interaction . client . db . execute (
70+ /* sql */
71+ `
72+ INSERT INTO
73+ auto_roles (server_id, roles)
74+ VALUES
75+ (?, ?) ON DUPLICATE KEY
76+ UPDATE roles =
77+ VALUES
78+ (roles)
79+ ` ,
80+ [ interaction . guild . id , JSON . stringify ( autoRoles ) ] ,
81+ ) ;
6082
6183 embed . setDescription ( `The ${ role } role will be given to all new members when they join the server.` ) ;
6284 return interaction . editReply ( { embeds : [ embed ] } ) ;
6385 }
6486
6587 case 'remove' : {
66- let autoRoles = ( await db . get ( `servers.${ interaction . guild . id } .autoRoles` ) ) || [ ] ;
6788 if ( ! autoRoles . includes ( role . id ) ) {
6889 embed
6990 . setDescription ( `The ${ role } role is not as as an auto-role.` )
@@ -72,15 +93,25 @@ exports.run = async (interaction) => {
7293 }
7394
7495 autoRoles = autoRoles . filter ( ( r ) => r !== role . id ) ;
75- await db . set ( `servers.${ interaction . guild . id } .autoRoles` , autoRoles ) ;
96+ await interaction . client . db . execute (
97+ /* sql */
98+ `
99+ INSERT INTO
100+ auto_roles (server_id, roles)
101+ VALUES
102+ (?, ?) ON DUPLICATE KEY
103+ UPDATE roles =
104+ VALUES
105+ (roles)
106+ ` ,
107+ [ interaction . guild . id , JSON . stringify ( autoRoles ) ] ,
108+ ) ;
76109
77110 embed . setDescription ( `The ${ role } role will no longer be given to new members when they join the server.` ) ;
78111 return interaction . editReply ( { embeds : [ embed ] } ) ;
79112 }
80113
81114 case 'list' : {
82- const autoRoles = ( await db . get ( `servers.${ interaction . guild . id } .autoRoles` ) ) || [ ] ;
83-
84115 // Fetch all roles to ensure uncached roles are included
85116 const allRoles = await interaction . guild . roles . fetch ( ) ;
86117
@@ -89,7 +120,19 @@ exports.run = async (interaction) => {
89120
90121 // Update the database if roles were removed
91122 if ( validRoles . length !== autoRoles . length ) {
92- await db . set ( `servers.${ interaction . guild . id } .autoRoles` , validRoles ) ;
123+ await interaction . client . db . execute (
124+ /* sql */
125+ `
126+ INSERT INTO
127+ auto_roles (server_id, roles)
128+ VALUES
129+ (?, ?) ON DUPLICATE KEY
130+ UPDATE roles =
131+ VALUES
132+ (roles)
133+ ` ,
134+ [ interaction . guild . id , JSON . stringify ( validRoles ) ] ,
135+ ) ;
93136 }
94137
95138 // If no valid roles remain, send an appropriate message
0 commit comments