Skip to content

Commit

Permalink
added additional commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhayes3 committed Aug 24, 2024
1 parent c45ec58 commit 0f0b40a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 7 deletions.
8 changes: 4 additions & 4 deletions commands/core/chat.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'dotenv/config';
import { SlashCommandBuilder } from 'discord.js';
import { OpenAI } from 'openai';
import { splitString } from '../../utils.js';
import { sleep, splitString } from '../../utils.js';

const openai = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'],

Check failure on line 7 in commands/core/chat.js

View workflow job for this annotation

GitHub Actions / lint

'process' is not defined

Check failure on line 7 in commands/core/chat.js

View workflow job for this annotation

GitHub Actions / test (18)

'process' is not defined

Check failure on line 7 in commands/core/chat.js

View workflow job for this annotation

GitHub Actions / test (20)

'process' is not defined

Check failure on line 7 in commands/core/chat.js

View workflow job for this annotation

GitHub Actions / test (22)

'process' is not defined
});

const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
// const sleep = (ms) => {
// return new Promise(resolve => setTimeout(resolve, ms));
// }

const terminalStates = ["cancelled", "failed", "completed", "expired"];
const statusCheckLoop = async (openaiThreadId, runId) => {
Expand Down
48 changes: 48 additions & 0 deletions commands/core/clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SlashCommandBuilder, PermissionsBitField, EmbedBuilder, PermissionFlagsBits } from 'discord.js';

export const data = new SlashCommandBuilder()
.setName('clear')
.setDescription('Clear messages')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addStringOption(option => option.setName('amount').setDescription('The number of messages to clear (up to 99)').setRequired(true))

export async function execute(interaction) {
const { options } = interaction;

const amount = options.getString('amount');
const user = interaction.user.username;

if (!interaction.member.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
return interaction.reply({ content: 'No perms', ephemeral: true });
}

if (isNaN(amount) || parseInt(amount) < 1 || parseInt(amount) > 99) {
return interaction.reply({ content: 'Please provide a valid number between 1 and 99.', ephemeral: true });
}

await interaction.deferReply({ ephemeral: true });

const deletedSize = await deleteMessages(interaction.channel, parseInt(amount), user);

const clearEmbed = new EmbedBuilder()
.setAuthor({ name: 'Clear' })
.setColor('#333333')
.setTitle(`Clear used in ${interaction.channel}`)
.setThumbnail(interaction.client.user.avatarURL())
.setFooter({ text: `Clear` })
.setTimestamp()

console.log(deletedSize);
return interaction.followUp({ embeds: [clearEmbed], ephemeral: true });
}

async function deleteMessages(channel, amount) {
const total = amount;

// true arg forces the bot to delete msgs that older then 14 days
const count = await channel.bulkDelete(total, true);
console.log(count.size);
console.log(count);

return count;
}
37 changes: 37 additions & 0 deletions commands/core/perms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from 'discord.js';

export const data = new SlashCommandBuilder()
.setName("permissions")
.setDescription("Displays permissions of given user.")
.addUserOption(option => option.setName("user").setDescription("The user to get permissions for").setRequired(false))

export async function execute(interaction) {
if (!interaction.isChatInputCommand()) return;

const { options } = interaction;

const member = options.getMember("user") || interaction.member;
const user = options.getUser("user") || interaction.user;

if (!member) return interaction.reply({ content: "Member **could not** be found!", ephemeral: true, });

let permissionFlags = Object.keys(PermissionFlagsBits);

let output = `**Permissions for** **${member}:** \n\`\`\``;
for (let i = 0; i < permissionFlags.length; i++) {
let permissionName = permissionFlags[i];
let hasPermission = member.permissions.has(permissionName);
output += `${permissionName} ${hasPermission ? "true" : "false"}\n`;
}
output += `\`\`\``;

const PermsEmbed = new EmbedBuilder()
.setTitle('Permissions')
.setDescription(`> **${user.tag}** permissions in **${interaction.guild.name}** \n\n${output}`)
.setColor('#555555')
.setThumbnail(user.avatarURL())
.setFooter({ text: `${user.tag} permissions` })
.setTimestamp();

return interaction.reply({ embeds: [PermsEmbed] });
}
6 changes: 3 additions & 3 deletions commands/core/upload.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readFile } from 'node:fs/promises';
import { SlashCommandBuilder, AttachmentBuilder } from 'discord.js';

export const data = new SlashCommandBuilder().setName('upload').setDescription('Upload a file.');
export const data = new SlashCommandBuilder().setName('upload').setDescription('Upload a file');

export async function execute(interaction) {
await interaction.deferReply();
await interaction.deferReply({ ephemeral: true });

const image_file = await readFile('./data/image.jpg');
const image_name = "data/image.jpg";
Expand All @@ -14,5 +14,5 @@ export async function execute(interaction) {
console.log("Attachment:", attachment);

const response = "File uploaded.";
await interaction.followUp({ content: response });
await interaction.followUp({ content: response, ephemeral: true });
};
5 changes: 5 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export function splitString(str, length) {
}
return segments;
}

export const sleep = (ms) => {
console.log("Sleeping " + ms + "ms");
return new Promise(resolve => setTimeout(resolve, ms));
}

0 comments on commit 0f0b40a

Please sign in to comment.