Advertisement
FusionGamer

Discord.JS V14 TS w/GPT-4 Power

Jun 16th, 2023
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. **your-bot/**
  2. ├── **src/**
  3. │   ├── **commands/**
  4. │   │   ├── **moderation/**
  5. │   │   │   └── ban.ts
  6. │   │   └── **util/**
  7. │   │       └── ping.ts
  8. │   ├── **config/**
  9. │   │   └── config.ts
  10. │   ├── **events/**
  11. │   │   ├── messageCreate.ts
  12. │   │   └── ready.ts
  13. │   ├── **utils/**
  14. │   │   ├── permissions.ts
  15. │   │   └── registry.ts
  16. │   └── bot.ts
  17. ├── package.json
  18. └── tsconfig.json
  19.  
  20. # src/config/config.ts
  21. ```ts
  22. export const config = {
  23.   mongoURI: 'your_mongodb_connection_string',
  24. };
  25. ```
  26.  
  27. # src/commands/moderation/ban.ts
  28. ```ts
  29. import { CommandInteraction, Client } from 'discord.js';
  30. import { SlashCommandBuilder } from '@discordjs/builders';
  31.  
  32. export const command = {
  33.   data: new SlashCommandBuilder()
  34.     .setName('ban')
  35.     .setDescription('Ban a user')
  36.     .addUserOption((option) =>
  37.       option.setName('user').setDescription('The user to ban').setRequired(true)
  38.     )
  39.     .addStringOption((option) =>
  40.       option.setName('reason').setDescription('The reason for the ban').setRequired(false)
  41.     ),
  42.   async execute(interaction: CommandInteraction, client: Client) {
  43.     // Check permissions and execute the command
  44.   },
  45. };
  46. ```
  47. # src/commands/util/ping.ts
  48. ```ts
  49. import { SlashCommandBuilder } from '@discordjs/builders';
  50. import { CommandInteraction } from 'discord.js';
  51. import { MongoClient } from 'mongodb';
  52.  
  53. export const data = new SlashCommandBuilder().setName('ping').setDescription('Replies with Pong!');
  54.  
  55. export const execute = async (interaction: CommandInteraction, mongoClient: MongoClient) => {
  56.   const hasPermission = await checkPermission(interaction, mongoClient, 2);
  57.  
  58.   if (hasPermission) {
  59.     await interaction.reply('Pong!');
  60.   } else {
  61.     await interaction.reply({
  62.       content: 'You do not have permission to use this command.',
  63.       ephemeral: true,
  64.     });
  65.   }
  66. };
  67. ```
  68.  
  69. # src/events/messageCreate.ts
  70. ```ts
  71. import { Message } from 'discord.js';
  72. import { MongoClient } from 'mongodb';
  73.  
  74. export const name = 'messageCreate';
  75.  
  76. export const execute = async (message: Message, mongoClient: MongoClient) => {
  77.   // Your message handling code here
  78. };
  79. ```
  80.  
  81. # src/events/ready.ts
  82. ```ts
  83. import { Client } from 'discord.js';
  84. import { MongoClient } from 'mongodb';
  85.  
  86. export const name = 'ready';
  87. export const once = true;
  88.  
  89. export const execute = (client: Client, mongoClient: MongoClient) => {
  90.   console.log(`Logged in as ${client.user?.tag}!`);
  91. };
  92. ```
  93.  
  94. # src/utils/permissions.ts
  95. ```ts
  96. import { CommandInteraction } from 'discord.js';
  97. import { MongoClient } from 'mongodb';
  98.  
  99. export const checkPermission = async (
  100.   interaction: CommandInteraction,
  101.   mongoClient: MongoClient,
  102.   requiredPermissionLevel: number
  103. ): Promise<boolean> => {
  104.   const userId = interaction.user.id;
  105.   const collection = mongoClient.db('your_database_name').collection('permissions');
  106.  
  107.   const userPermission = await collection.findOne({ userId });
  108.  
  109.   const userPermissionLevel = userPermission ? userPermission.level : 1;
  110.  
  111.   return userPermissionLevel >= requiredPermissionLevel;
  112. };
  113. ```
  114.  
  115. # src/utils/registry.ts
  116. ```ts
  117. import { Client } from 'discord.js';
  118. import { readdirSync } from 'fs';
  119. import { join } from 'path';
  120.  
  121. export async function registerCommands(client: Client, dir: string) {
  122.   const files = readdirSync(join(__dirname, dir));
  123.   for (const file of files) {
  124.     const stat = readdirSync(join(__dirname, dir, file)).isDirectory();
  125.     if (stat) {
  126.       registerCommands(client, join(dir, file));
  127.     } else {
  128.       if (file.endsWith('.ts')) {
  129.         const { command } = await import(join(__dirname, dir, file));
  130.         client.commands.set(command.data.name, command);
  131.       }
  132.     }
  133.   }
  134. }
  135.  
  136. export async function registerEvents(client: Client, dir: string) {
  137.   const files = readdirSync(join(__dirname, dir));
  138.   for (const file of files) {
  139.     const stat = readdirSync(join(__dirname, dir, file)).isDirectory();
  140.     if (stat) {
  141.       registerEvents(client, join(dir, file));
  142.     } else {
  143.       if (file.endsWith('.ts')) {
  144.         const { event } = await import(join(__dirname, dir, file));
  145.         if (event.once) {
  146.           client.once(event.name, (...args) => event.execute(...args, client));
  147.         } else {
  148.           client.on(event.name, (...args) => event.execute(...args, client));
  149.         }
  150.       }
  151.     }
  152.   }
  153. }
  154. ```
  155.  
  156. # src/bot.ts
  157. ```ts
  158. import { Client, Collection, Intents } from 'discord.js';
  159. import { MongoClient } from 'mongodb';
  160. import dotenv from 'dotenv';
  161. import { config } from './config';
  162. import { registerCommands, registerEvents } from './utils/registry';
  163.  
  164. dotenv.config();
  165.  
  166. (async () => {
  167.   const client = new Client({ intents: [Intents.FLAGS.Guilds, Intents.FLAGS.GuildMessages] });
  168.   client.commands = new Collection();
  169.   client.events = new Collection();
  170.   client.mongo = new MongoClient(config.mongoURI);
  171.  
  172.   await client.mongo.connect();
  173.   console.log('Connected to MongoDB');
  174.  
  175.   await registerCommands(client, '../commands');
  176.   await registerEvents(client, '../events');
  177.  
  178.   client.login(process.env.DISCORD_BOT_TOKEN);
  179. })();
  180. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement