Command Handler Issues

I would like to use my command handler to execute this code whenever someone types a message in a specific channel. This command iis NOT tied to a slash command, but instead listens for messages sent by users. I don't understand how to make the command handler work with code other than code using SlashCommandBuilder.
12 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
M@
M@OP2y ago
const { Configuration, OpenAIApi } = require("openai");

module.exports = {
data: new OpenAIApi(
new Configuration({
apiKey: process.env.API_KEY,
})
.setName("gpt3.5")
.setDescription("An OpenAI chatbot.")
),
run: ({ interaction }) => {
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.channel.id !== process.env.CHANNEL_ID) return;
if (message.content.startsWith("!")) return;

let conversationLog = [{ role: "system", content: "You are a chatbot." }];

conversationLog.push({
role: "user",
content: message.content,
});

await message.channel.sendTyping();

const result = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: conversationLog,
});

message.reply(result.data.choices[0].message);
});
},
};
const { Configuration, OpenAIApi } = require("openai");

module.exports = {
data: new OpenAIApi(
new Configuration({
apiKey: process.env.API_KEY,
})
.setName("gpt3.5")
.setDescription("An OpenAI chatbot.")
),
run: ({ interaction }) => {
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.channel.id !== process.env.CHANNEL_ID) return;
if (message.content.startsWith("!")) return;

let conversationLog = [{ role: "system", content: "You are a chatbot." }];

conversationLog.push({
role: "user",
content: message.content,
});

await message.channel.sendTyping();

const result = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: conversationLog,
});

message.reply(result.data.choices[0].message);
});
},
};
I've tried removing the .setName() and .setDescription() functions, but when I do, I'm given an error stating my command must have a name. I don't understand, because no one will ever need to read this "invisble" command. I guess it's treating it like a slash command but it's not. I just wanbt to store each of my commands in seperate files so my index.js does not get too crammed.
require("dotenv/config");
const { Client, IntentsBitField } = require("discord.js");
const { CommandHandler } = require("djs-commander");
const path = require("path");

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

new CommandHandler({
client,
commandsPath: path.join(__dirname, "commands"),
eventsPath: path.join(__dirname, "events"),
validationsPath: path.join(__dirname, "validations"),
testServer: "1027046318796963840",
});

client.login(process.env.TOKEN);
require("dotenv/config");
const { Client, IntentsBitField } = require("discord.js");
const { CommandHandler } = require("djs-commander");
const path = require("path");

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

new CommandHandler({
client,
commandsPath: path.join(__dirname, "commands"),
eventsPath: path.join(__dirname, "events"),
validationsPath: path.join(__dirname, "validations"),
testServer: "1027046318796963840",
});

client.login(process.env.TOKEN);
This is my index.js file, if it helps. [email protected] Node: v18.16.1
SpecialSauce
SpecialSauce2y ago
Do you have an event handler? You’re only calling this run function once hopefully.
M@
M@OP2y ago
Yes, I have an event handler but I don’t think any of this code interacts with my events handler.
SpecialSauce
SpecialSauce2y ago
If you have a messageCreate event listener you should check if the message is in the desired channel and call run() if it is. Move the ‘command’ file out of your slash command folder so that you’re not trying to parse it as one.
M@
M@OP2y ago
Would I put it in my events folder?
SpecialSauce
SpecialSauce2y ago
It’s not an event either so it would error there with the event handler
M@
M@OP2y ago
Could I make my command folder a slashcommands folder and make a new folder for basic commands. My OpenAI code isn’t really a command. I think a better word would be interaction or integration.
SpecialSauce
SpecialSauce2y ago
You wouldn’t be able you put it anywhere you would have a handler load it in and require it to have certain properties it doesn’t. I would import it in your messageCreate event and call run() when needed. And that also means run() would NOT be creating any event listeners.
M@
M@OP2y ago
But could I have it in a separate file from index.js? Right now I have it in a file named gpt3.5.js. That’s my main concern and take away from all of this.
SpecialSauce
SpecialSauce2y ago
Yeah as long as you just import it from your messageCreate event it should be fine.
M@
M@OP2y ago
I got it! Thank you, SpecialSauce!
Want results from more Discord servers?
Add your server