Skip to content

Commit

Permalink
Merge pull request #3 from CSC510-G35-Fall2022/journalCommand
Browse files Browse the repository at this point in the history
Journal command
  • Loading branch information
Culcheese authored Sep 22, 2022
2 parents dc544e1 + 1fc78af commit f227303
Show file tree
Hide file tree
Showing 1,979 changed files with 225,982 additions and 24 deletions.
21 changes: 21 additions & 0 deletions commands/journal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { EmbedBuilder } = require("discord.js");
const fs = require("fs");
const { parse } = require("csv-parse");

module.exports = {
data: new SlashCommandBuilder()
.setName("journal")
.setDescription("Replies with pong"),
async execute(interaction, options) {
num = Math.floor(Math.random() * options.length);

const embed = new EmbedBuilder()
.setColor(0x0099ff)
.setTitle(options[num])
.setThumbnail("https://cdn-icons-png.flaticon.com/512/3352/3352475.png")
.setDescription("answer this journal prompt");

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);
};
4 changes: 4 additions & 0 deletions journalPrompts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
How are you doing today?
What are you grateful for?
What is scaring you right now?
List 3 things you are proud of yourself for:
100 changes: 76 additions & 24 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
//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");
const { REST, Routes } = require("discord.js");
const clientId = "1017062749538361474";
const clientId = process.env.CLIENT_ID;
const guildId = "1011989055736660061";

// slash commands
const commands = [
{
name: "ping",
description: "Replies with Pong!",
},
{
name: "help",
description: "help function for bot, lists commands",
},
];
const fs = require("fs");
const { parse } = require("csv-parse");
const Discord = require("discord.js");
journalPrompts = [];

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

(async () => {
async () => {
try {
console.log("Started refreshing application (/) commands.");

Expand All @@ -33,23 +31,77 @@ const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
} catch (error) {
console.error(error);
}
})();
};

// run bot
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
parseJournal();
});

client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;

if (interaction.commandName === "ping") {
await interaction.reply("Pong!");
client.on("message", (msg) => {
if (msg.author.bot) return;
if (msg.content === "/journal") {
msg.reply;
}
});

const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));

//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") {
await interaction.reply("not yet");
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"));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}

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() {
fs.createReadStream("./journalPrompts.csv")
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", function (row) {
prompt = row.toString();
journalPrompts.push(row[0]);
})
.on("error", function (error) {
console.log(error.message);
})
.on("end", function () {});
}
client.login(process.env.TOKEN);
Loading

0 comments on commit f227303

Please sign in to comment.