commit
This commit is contained in:
parent
4cb2217898
commit
8a23e86b12
5 changed files with 89 additions and 1 deletions
38
src/commands/guilds/register.ts
Normal file
38
src/commands/guilds/register.ts
Normal 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
18
src/events/guildCreate.ts
Normal 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);
|
||||
},
|
||||
};
|
|
@ -18,4 +18,4 @@ export default {
|
|||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
|
@ -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")
|
||||
|
|
|
@ -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);
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue