Skip to content

Commit

Permalink
Moved commands into new files in command folder
Browse files Browse the repository at this point in the history
  • Loading branch information
maya-dc-patel committed Sep 21, 2022
1 parent fd48112 commit 4bf9234
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 68 deletions.
36 changes: 17 additions & 19 deletions commands/journal.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
// import { Message } from "discord.js";
// import { i18n } from "../utils/i18n";
// import { canModifyQueue } from "../utils/queue";
// import { bot } from "../index";
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs");
const { parse } = require("csv-parse");

// export default {
// name: "loop",
// aliases: ["l"],
// description: i18n.__("loop.description"),
// execute(message: Message) {
// const queue = bot.queues.get(message.guild!.id);
module.exports = {
data: new SlashCommandBuilder()
.setName("journal")
.setDescription("Replies with pong"),
async execute(interaction, options) {
num = Math.floor(Math.random() * options.length);

// if (!queue) return message.reply(i18n.__("loop.errorNotQueue")).catch(console.error);
// if (!canModifyQueue(message.member!)) return i18n.__("common.errorNotChannel");
const embed = new EmbedBuilder()
.setColor(0x0099ff)
.setTitle(options[num])
.setDescription("answer this journal prompt");

// queue.loop = !queue.loop;

// return message
// .reply(i18n.__mf("loop.result", { loop: queue.loop ? i18n.__("common.on") : i18n.__("common.off") }))
// .catch(console.error);
// }
// };
const messageId = interaction.reply({ embeds: [embed] });
},
};
10 changes: 10 additions & 0 deletions commands/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with pong'),
async execute(interaction) {
interaction.reply({ content: 'Pong' })
}
};
15 changes: 15 additions & 0 deletions deploy-commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { REST, SlashCommandBuilder, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');

const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info!'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info!'),
]
.map(command => command.toJSON());

const rest = new REST({ version: '10' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then((data) => console.log(`Successfully registered ${data.length} application commands.`))
.catch(console.error);
20 changes: 20 additions & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (client, message) => {
// Ignore all bots
if (message.author.bot) return;

// Ignore messages not starting with the prefix (in config.json)
if (message.content.indexOf(client.config.prefix) !== 0) return;

// Our standard argument/command name definition.
const args = message.content.slice(client.config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();

// Grab the command data from the client.commands Enmap
const cmd = client.commands.get(command);

// If that command doesn't exist, silently exit and do nothing
if (!cmd) return;

// Run the command
cmd.run(client, message, args);
};
95 changes: 46 additions & 49 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//do not delete needed for env
require("dotenv").config();

const { Client, GatewayIntentBits } = require("discord.js");
const {
Client,
Collection,
Intents,
GatewayIntentBits,
} = require("discord.js");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const { EmbedBuilder } = require("discord.js");
const { MessageEmbed } = require("discord.js");
Expand All @@ -13,26 +18,6 @@ const { parse } = require("csv-parse");
const Discord = require("discord.js");
journalPrompts = [];

// slash commands
const commands = [
{
name: "ping",
description: "Replies with Pong!",
},
{
name: "help",
description: "help function for bot, lists commands",
},
{
name: "date",
description: "returns the date",
},
{
name: "journal",
description: "get a journal prompt",
},
];

//register slash commands
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);

Expand All @@ -56,43 +41,58 @@ client.on("ready", () => {

client.on("message", (msg) => {
if (msg.author.bot) return;

if (msg.content === "/journal") {
msg.reply;
}
});

client.on("interactionCreate", (interaction) => {
if (!interaction.isChatInputCommand()) return;
const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));

if (interaction.commandName === "ping") {
interaction.reply("Pong!");

}
//referenced https://dev.to/kunal/how-to-make-a-slash-commands-bot-with-discord-js-v13-3l6k
for (const file of eventFiles) {
const eventName = file.split(".")[0];
const event = require(`./events/${file}`);
client.on(eventName, (...args) => event.execute(...args, null));
}

if (interaction.commandName === "help") {
interaction.reply("not yet");
console.log(new Date().getTime());
}
if (interaction.commandName === "date") {
interaction.reply(new Date().getDate().toString());
}
if (interaction.commandName === "journal") {
prompt = "prompt";
num = Math.floor(Math.random() * journalPrompts.length);
console.log(journalPrompts);
console.log(journalPrompts[num]);
client.commands = new Collection();
const commands = [];

// Creating a collection for commands in client
client.commands = new Collection();
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

const embed = new EmbedBuilder().setColor(0x0099FF).setTimestamp().setTitle(journalPrompts[num]).setDescription("answer this journal prompt");
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}


const messageId = interaction.reply({ embeds: [embed] });
// interaction.reply(journalPrompts[num]);
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === "journal") {
console.log("journal");
}
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, journalPrompts);
} catch (error) {
if (error) console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
});

// referenced: https://sebhastian.com/read-csv-javascript/
//parses the CSV file of journal prompts
function parseJournal() {
console.log("parsing journal");
fs.createReadStream("./journalPrompts.csv")
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", function (row) {
Expand All @@ -102,9 +102,6 @@ function parseJournal() {
.on("error", function (error) {
console.log(error.message);
})
.on("end", function () {
console.log("finished");
});
.on("end", function () {});
}

client.login(process.env.TOKEN);

0 comments on commit 4bf9234

Please sign in to comment.