├── LICENSE
├── README.md
├── cmds
└── examples.js
├── config.js
├── events
├── guildCreate.js
├── interactionCreate.js
└── ready.js
├── image
└── screenshot.png
├── index.js
├── models
└── example.js
├── package.json
└── utils
├── command.js
├── event.js
├── mongoose.js
└── slash.js
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Vu4ll
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Bu proje Discord.js v14.17.3 için hazırlanmıştır.
2 | - Başlamadan önce. Bu projeyi kullanmak için Node.js sürümünüz 18 veya üstü olmalıdır.
3 | - Elimden geldiğince [Mongoose](https://www.npmjs.com/package/mongoose) anlatmaya çalıştım. Mongoose database örnekleri içermektedir.
4 | - Komut yükleyicisi olarak [bu proje](https://github.com/Vu4ll/slash-handler/) kullanılmıştır. Ve tamamen eğik çizgi (/) komutları ile hazırlanmıştır.
5 | - Örnekler `cmds/examples.js` dosyasında mevcuttur.
6 | - **[config.js](https://github.com/Vu4ll/MongoDB-bot/blob/main/config.js)** dosyasındaki gerekli yerleri doldurunuz.
7 | - Mongoose bağlantısı için [bu yazı](https://medium.com/@thearkxd/node-js-projeleri-i%C3%A7in-mongodb-atlas-connection-linki-alma-5d955bbe5ae6) işinizi görecektir.
8 |
9 |
10 |
11 | 
12 |
13 |
14 |
15 | ## Ulaşım
16 | - Bana ulaşabileceğiniz tüm platformları [buradan](https://vu4ll.com.tr/) bulabilirsiniz.
17 |
18 |
19 |
20 | 
21 |
22 |
23 |
--------------------------------------------------------------------------------
/cmds/examples.js:
--------------------------------------------------------------------------------
1 | const { SlashCommandBuilder, MessageFlags, Client, ChatInputCommandInteraction, EmbedBuilder } = require("discord.js");
2 | const model = require("../models/example"); // Models klasöründeki dosyamızı tanımladık
3 |
4 | module.exports = {
5 | data: new SlashCommandBuilder()
6 | .setName("example")
7 | .setDescription("Veritabanı örnekleri")
8 | .addSubcommand((cmd) =>
9 | cmd
10 | .setName("kaydet")
11 | .setDescription("Veritabanına kayıt ekler")
12 | .addStringOption((option) =>
13 | option
14 | .setName("veri")
15 | .setDescription("Veritabanına kayıt edilecek veri")
16 | .setRequired(true)
17 | )
18 | )
19 | .addSubcommand((cmd) =>
20 | cmd.setName("veri").setDescription("Kayıtlı olan veriyi size gönderir")
21 | )
22 | .addSubcommand((cmd) =>
23 | cmd
24 | .setName("sil")
25 | .setDescription("Kayıtlı olan veriyi veritabanından siler")
26 | ),
27 | // Eğik çizgi komutumuz için gerekli ayarlarımızı yaptık
28 |
29 | /**
30 | * @param { Client } client
31 | * @param { ChatInputCommandInteraction } interaction
32 | * @param { EmbedBuilder } embed
33 | */
34 | run: async (client, interaction, embed) => {
35 | // Komutun kullanıldığı sunucudaki veriyi çektik ve tanımladık
36 | const data = await model.findOne({
37 | SERVER: interaction.guild.id,
38 | });
39 |
40 | if (interaction.options.getSubcommand() === "kaydet") {
41 | // Eğik çizgi komutumuzda belirtilen değeri tanımladık
42 | const veri = interaction.options.getString("veri");
43 |
44 | // Eğer bir veri varsa kanala mesaj gödersin
45 | if (data)
46 | return interaction.reply({
47 | content: `Bu sunucuda kayıtlı bir veri bulunmaktadır`,
48 | flags: MessageFlags.Ephemeral,
49 | });
50 |
51 | new model({
52 | SERVER: interaction.guild.id,
53 | EXAMPLE: veri,
54 | })
55 | .save() // Verimizi veritabanına kayıt ettik
56 | .then((x) => console.log(x)); // Konsola yazdırdık
57 |
58 | interaction.reply({
59 | content: `Veritabanına **${veri}** kaydı başarıyla eklendi.`,
60 | flags: MessageFlags.Ephemeral,
61 | }); // Ve kanala mesajımızı gönderdik
62 | } else if (interaction.options.getSubcommand() === "veri") {
63 | // Yukarıdaki data tanımı kullanarak veriyi alabiliriz
64 |
65 | // Veri yoksa yapılacak işlem
66 | if (!data)
67 | return interaction.reply({
68 | content: `Kayıtlı veri bulunamadı.`,
69 | flags: MessageFlags.Ephemeral,
70 | });
71 |
72 | interaction.reply({
73 | content: data.toString(), // Object bir değer verecektir. Sadece bir veriyi almak için `data.EXAMPLE` yazmak yeterlidir.
74 | flags: MessageFlags.Ephemeral,
75 | });
76 | } else if (interaction.options.getSubcommand() === "sil") {
77 | // Yukarıdaki data tanımı kullanarak veriyi silebiliriz
78 |
79 | // Veri yoksa yapılacak işlem
80 | if (!data)
81 | return interaction.reply({
82 | content: `Kayıtlı veri bulunamadı.`,
83 | flags: MessageFlags.Ephemeral,
84 | });
85 | // Veri varsa yapılacak işlem
86 | else if (data) {
87 | // Verimizi sildik
88 | return model.findOneAndRemove(data).then(() =>
89 | // Ve kanala mesajımızı gönderdik
90 | interaction.reply({
91 | content: `Kayıt veritabanından silindi`,
92 | flags: MessageFlags.Ephemeral,
93 | })
94 | );
95 | }
96 | }
97 | },
98 | };
99 |
100 | // Vu4ll
101 |
--------------------------------------------------------------------------------
/config.js:
--------------------------------------------------------------------------------
1 | const { GatewayIntentBits, Partials } = require("discord.js");
2 |
3 | module.exports = {
4 | TOKEN: "",
5 | MONGO: "",
6 |
7 | CLIENT: {
8 | intents: [
9 | GatewayIntentBits.Guilds,
10 | GatewayIntentBits.GuildMembers,
11 | GatewayIntentBits.GuildModeration,
12 | GatewayIntentBits.GuildIntegrations,
13 | GatewayIntentBits.GuildWebhooks,
14 | GatewayIntentBits.GuildInvites,
15 | GatewayIntentBits.GuildVoiceStates,
16 | GatewayIntentBits.GuildPresences,
17 | GatewayIntentBits.GuildMessages,
18 | GatewayIntentBits.GuildMessageReactions,
19 | GatewayIntentBits.GuildMessageTyping,
20 | GatewayIntentBits.DirectMessages,
21 | GatewayIntentBits.DirectMessageReactions,
22 | GatewayIntentBits.DirectMessageTyping,
23 | GatewayIntentBits.MessageContent,
24 | GatewayIntentBits.GuildScheduledEvents,
25 | GatewayIntentBits.AutoModerationConfiguration,
26 | GatewayIntentBits.AutoModerationExecution,
27 | ],
28 | partials: [
29 | Partials.Message,
30 | Partials.Channel,
31 | Partials.Reaction,
32 | Partials.User,
33 | Partials.GuildMember,
34 | Partials.GuildScheduledEvent,
35 | Partials.ThreadMember,
36 | ],
37 | }
38 | };
--------------------------------------------------------------------------------
/events/guildCreate.js:
--------------------------------------------------------------------------------
1 | const { Guild } = require("discord.js");
2 | const client = global.client;
3 |
4 | module.exports = {
5 | event: "guildCreate",
6 |
7 | /**
8 | * @param { Guild } guild
9 | */
10 | run: async (guild) => {
11 | require("../utils/command")(client);
12 | },
13 | };
14 |
15 | // Vu4ll
16 |
--------------------------------------------------------------------------------
/events/interactionCreate.js:
--------------------------------------------------------------------------------
1 | const client = global.client;
2 | const { EmbedBuilder, InteractionType, Interaction } = require("discord.js");
3 |
4 | module.exports = {
5 | event: "interactionCreate",
6 |
7 | /**
8 | * @param { Interaction } interaction
9 | */
10 | run: async (interaction) => {
11 | const command = client.commands.get(interaction.commandName);
12 |
13 | const embed = new EmbedBuilder()
14 | .setFooter({
15 | text: `${interaction.user.tag}`,
16 | iconURL: interaction.user.avatarURL({ dynamic: true }),
17 | })
18 | .setColor("#000000")
19 | .setTimestamp();
20 |
21 | if (interaction.type === InteractionType.ApplicationCommand)
22 | command.run(client, interaction, embed);
23 | },
24 | };
25 |
26 | // Vu4ll
27 |
--------------------------------------------------------------------------------
/events/ready.js:
--------------------------------------------------------------------------------
1 | const client = global.client;
2 |
3 | module.exports = {
4 | event: "ready",
5 | run: async () => {
6 | require("../utils/command")(client);
7 | },
8 | };
9 |
10 | // Vu4ll
11 |
--------------------------------------------------------------------------------
/image/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Vu4ll/MongoDB-bot/38403d1d5643858bb978ec6570e98aaaccadebd6/image/screenshot.png
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const config = require("./config.js");
2 | const { Client, Collection } = require("discord.js");
3 | const client = new Client({
4 | intents: config.CLIENT.intents,
5 | partials: config.CLIENT.partials,
6 | });
7 |
8 | require("./utils/event.js")(client);
9 | require("./utils/mongoose.js");
10 |
11 | global.client = client;
12 | client.commands = new Collection();
13 |
14 | client
15 | .login(config.TOKEN)
16 | .then(() => console.log(`${client.user.username} aktif!`))
17 | .catch((err) => console.error(`Botu aktif ederken bir hata meydana geldi! \n${err}`));
18 |
19 | // Vu4ll
20 |
--------------------------------------------------------------------------------
/models/example.js:
--------------------------------------------------------------------------------
1 | const { Schema, model } = require("mongoose");
2 |
3 | module.exports = model(
4 | "example",
5 | new Schema({
6 | SERVER: String,
7 | EXAMPLE: String,
8 | })
9 | );
10 |
11 | // Modeller hakkında daha fazla bilgi için: https://mongoosejs.com/docs/guide.html
12 | // Vu4ll
13 |
14 | /*
15 | Bu dosya için boş bir örnek
16 |
17 | const { Schema, model } = require("mongoose");
18 |
19 | module.exports = model(
20 | "Model adımız",
21 | new Schema({
22 | EXAMPLE: String, // Verilerimiz içi alanlar
23 | })
24 | );
25 | */
26 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mongodb-bot",
3 | "description": "Discord.js botları için Mongoose veritabanı (database) kullanım örnekleri.",
4 | "main": "index.js",
5 | "author": "Vu4ll",
6 | "license": "MIT",
7 | "scripts": {
8 | "start": "node index.js"
9 | },
10 | "dependencies": {
11 | "discord.js": "^14.17.3",
12 | "@discordjs/rest": "^1.0.0",
13 | "discord-api-types": "^0.36.2",
14 | "fs": "^0.0.2",
15 | "mongoose": "^6.4.5"
16 | },
17 | "engines": {
18 | "node": "18.x"
19 | }
20 | }
--------------------------------------------------------------------------------
/utils/command.js:
--------------------------------------------------------------------------------
1 | const fs = require("fs");
2 | const slash = require("./slash.js");
3 |
4 | module.exports = async (client) => {
5 | const commandFiles = fs
6 | .readdirSync("./cmds")
7 | .filter((file) => file.endsWith(".js"));
8 |
9 | let cmdArray = [];
10 | console.log(`${commandFiles.length} command will be loaded.`);
11 | commandFiles.forEach((file) => {
12 | const command = require(`../cmds/${file}`);
13 | client.commands.set(command.data.name, command);
14 |
15 | cmdArray.push(command);
16 | console.log(`Loaded command: ${command.data.name}.`);
17 | });
18 |
19 | const finalArray = cmdArray.map((e) => e.data.toJSON());
20 | slash.register(client.user.id, finalArray);
21 | };
22 |
23 | // Vu4ll
24 |
--------------------------------------------------------------------------------
/utils/event.js:
--------------------------------------------------------------------------------
1 | const fs = require("fs");
2 |
3 | module.exports = async client => {
4 | fs.readdir("./events", (err, files) => {
5 | console.log(`${files.length} event will be loaded.`);
6 | files.forEach(file => {
7 | let props = require(`../events/${file}`);
8 | if (!props.event) return;
9 | client.on(props.event, props.run);
10 | console.log(`Loaded event: ${props.event}.`);
11 | });
12 | });
13 | };
14 |
15 | // Vu4ll
16 |
--------------------------------------------------------------------------------
/utils/mongoose.js:
--------------------------------------------------------------------------------
1 | const mongoose = require("mongoose");
2 | const { MONGO } = require("../config.js");
3 |
4 | mongoose.connect(MONGO, {
5 | useUnifiedTopology: true,
6 | useNewUrlParser: true,
7 | });
8 |
9 | mongoose.connection.on("connected", () => {
10 | console.log("MongoDB bağlandı!");
11 | });
12 |
13 | mongoose.connection.on("error", () => {
14 | console.error("MongoDB bağlanılamadı!");
15 | });
16 |
17 | // Vu4ll
18 |
--------------------------------------------------------------------------------
/utils/slash.js:
--------------------------------------------------------------------------------
1 | const slash = {
2 | register: async (clientId, commands) => {
3 | const config = require("../config.js");
4 | const { REST } = require("@discordjs/rest");
5 | const { Routes } = require("discord-api-types/v10");
6 |
7 | const rest = new REST({ version: "10" }).setToken(config.TOKEN);
8 |
9 | try {
10 | await rest
11 | .put(Routes.applicationCommands(clientId), { body: commands }) // Setting up slash commands
12 | .then(() => {
13 | console.log(`Eğik çizgi komutları yüklendi.`);
14 | });
15 | } catch (error) {
16 | console.error(`eğik çizgi komutları yüklenirken bir hata meydana geldi! \n${error}`);
17 | }
18 | },
19 | };
20 |
21 | module.exports = slash;
22 |
23 | // Vu4ll
24 |
--------------------------------------------------------------------------------