This commit is contained in:
Lukian LEIZOUR 2024-08-17 19:32:16 +02:00
parent 4cb2217898
commit 8a23e86b12
5 changed files with 89 additions and 1 deletions

View file

@ -0,0 +1,38 @@
import { SlashCommandBuilder, CommandInteraction, PermissionsBitField } from "discord.js";
import { connectToDb, getguild, addGuild } from "../../libs/mysql.js"
import { errorEmbed, successEmbed } from "../../libs/discord.js";
export default {
data: new SlashCommandBuilder()
.setName("register")
.setDescription("Register the guild to the bot.")
.setDMPermission(false),
async execute(interaction: CommandInteraction) {
const guild_id: string = interaction.guildId ? interaction.guildId : "";
const member = interaction.guild?.members.cache.get(interaction.user.id);
if (! member?.permissions.has(PermissionsBitField.Flags.ManageGuild)) {
const embed = errorEmbed("You are not allowed to use that command.", interaction.client.user.displayAvatarURL());
return await interaction.reply({embeds: [embed]});
}
const connection = await connectToDb();
const guild: any[] = await getguild(connection, guild_id);
if (guild[0]) {
const embed = await errorEmbed("Your server is already registered.", interaction.client.user.displayAvatarURL());
return await interaction.reply({embeds: [embed]});
}
await addGuild(connection, guild_id);
connection.end();
const embed = await successEmbed("your server has been added to the bot.", interaction.client.user.displayAvatarURL());
await interaction.reply({embeds: [embed]});
},
};

18
src/events/guildCreate.ts Normal file
View file

@ -0,0 +1,18 @@
import { Events, Guild } from "discord.js";
import { connectToDb, getguild, addGuild } from "../libs/mysql.js";
export default {
name: Events.GuildCreate,
async execute(guild: Guild) {
const connection = await connectToDb();
const guild_obj: any[] = await getguild(connection, guild.id);
connection.end()
if (guild_obj[0]) {
return;
}
addGuild(connection, guild.id);
},
};

View file

@ -18,4 +18,4 @@ export default {
}
}
},
};
};

View file

@ -37,6 +37,14 @@ export function errorEmbed(error: string, iconURL: string) {
.setColor("#000000");
}
export function successEmbed(message: string, iconURL: string) {
return new EmbedBuilder()
.setTitle("Success")
.setDescription(message)
.setFooter({ text: "Bot by @ninja_jambon.", iconURL: iconURL})
.setColor("#000000");
}
export function quotaEmbed(quota: number, iconURL: string) {
return new EmbedBuilder()
.setTitle("Quota left")

View file

@ -73,3 +73,27 @@ export function resetQuota(connection: mysql.Connection) {
})
})
}
export function getguild(connection: mysql.Connection, guild_id: string): Promise<any[]> {
return new Promise((resolve, reject) => {
connection.query(`SELECT * FROM guilds WHERE guild_id = "${guild_id}"`, (error, result) => {
if (error) {
reject(error);
}
resolve(result);
})
})
}
export function addGuild(connection: mysql.Connection, guild_id: string) {
return new Promise((resolve, reject) => {
connection.query(`INSERT INTO guilds (guild_id) VALUES ("${guild_id}")`, (error, result) => {
if (error) {
reject(error);
}
resolve(result);
})
})
}