I want to change the value of conf.perms.commands in my mercyxrd.js config file as seen in the below command,
please help me on how to do this (:
const conf = require("../../mercyxrd.js")
const Discord = require('discord.js')
const fs = require('fs');
class roller extends Command {
constructor(client) {
super(client, {
name: "roles",
aliases: ["roles"],
usage: "roles",
description: "bu komut hakkında bilgi bulunmuyor."
});
}
async run(message, args, perm) {
if(message.author.id !== "411621794131476480") return
let embed = new Discord.MessageEmbed()
if(!args[0]) return message.channel.send(embed.setDescription(`specify an argument`))
if(args[0] == "register-auth") {
let rol = message.mentions.roles.first() || message.guild.roles.cache.get(args.splice(1)[0]) || message.guild.roles.cache.find(r=>r.name===args.splice(1).join(''));
if(!rol) return message.channel.send(embed.setDescription(`please specify a role`)).then(x=>x.delete({timeout: 9000}))
//In this part, I want to change the value in my config file. conf.perms.commands
message.channel.send(`${rol} designated as the registration role.`)
}
}};
module.exports = roller;
Related
it might be a stupid question but since I'm a beginner I sadly don't understand most of how javascript works yet.
I'm trying to create a small Discord bot for a project that I'm working on and I would like to make it so you can select an option from a dropdown menu and whatever you select it sends a different embedded message in the channel. But whatever I do it doesn't send the embedded message. I've looked up multiple tutorials, but it seems like I'm missing something. Maybe someone can direct me in the right direction.
It is sending the dropdown menu, but sending error "InteractionAlreadyReplied". And whenever I try to select an option it won't send the embedded messages.
Here is my code so you can see the approach I'm using. I've tried using "MessageActionRow" and "MessageSelectMenu", but it seems like they don't work anymore. After inserting "ActionRowBuilder" and "StringSelectMenuBuilder" I got the menu working, but as said I don't get the embedded messages as an option. (didn't include the .env file since its only the token in there) I'm just not sure how I can make it so it connects the values from the dropdown menu to the right embeds.
CommandFile:
const { Discord, MessageActionRow, MessageSelectMenu,
SlashCommandBuilder, EmbedBuilder, roleMention, ButtonBuilder,
StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ActionRowBuilder } = require('discord.js');
const { MessageEmbed } = require("discord.js")
module.exports = {
data: new SlashCommandBuilder()
.setName('dropdown')
.setDescription('Returns a select dropdown!'),
async execute(interaction, client, message, args) {
const row = new ActionRowBuilder()
.addComponents(
new StringSelectMenuBuilder()
.setCustomId("select")
.setPlaceholder("Wähle etwas aus:")
.addOptions([
{
label: "Auswahl 1",
description: "Beschreibung Auswahl 1",
value: "first"
},
{
label: "Auswahl 2",
description: "Beschreibung Auswahl 2",
value: "second"
},
{
label: "Auswahl 3",
description: "Beschreibung Auswahl 3",
value: "third"
},
])
)
let embed = new EmbedBuilder()
.setTitle("Embed Startseite")
.setDescription("Ab hier kann man selecten, was man möchte")
.setColor(0x18e1ee)
let sendmsg = await interaction.reply({ content:
"ㅤ", ephemeral:true, embeds: [embed], components: [row] })
let embed1 = new EmbedBuilder()
.setTitle("Embed 1")
.setDescription("Embed 1 Beschreibung")
.setColor(0x18e1ee)
let embed2 = new EmbedBuilder()
.setTitle("Embed 2")
.setDescription("Embed 2 Beschreibung")
.setColor(0x18e1ee)
let embed3 = new EmbedBuilder()
.setTitle("Embed 3")
.setDescription("Embed 3 Beschreibung")
.setColor(0x18e1ee)
const collector = message.createMessageComponentCollector({
componentType: "StringSelectMenuBuilder"
})
collector.on("collect", async (collected) => {
const value = collected.values[0]
if(value === "first") {
collected.reply({ embeds: [embed1], ephemeral:true })
}
if(value === "second") {
collected.reply({ embeds: [embed2], ephemeral:true })
}
if(value === "third") {
collected.reply({ embeds: [embed3], ephemeral:true })
}
})
}
}
bot.js:
require("dotenv").config();
const { token } = process.env;
const { Client, Collection, GatewayIntentBits, ChannelSelectMenuInteraction } = require("discord.js");
const fs = require("fs");
const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new Collection();
client.buttons = new Collection();
client.selectMenus = new Collection();
client.commandArray = [];
const functionFolders = fs.readdirSync(`./src/functions`);
for (const folder of functionFolders) {
const functionFiles = fs
.readdirSync(`./src/functions/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`./functions/${folder}/${file}`)(client);
}
client.handleEvents();
client.handleCommands();
client.handleComponents();
client.login(token);
HandleComponents.js :
const { readdirSync } = require('fs');
module.exports = (client) => {
client.handleComponents = async () => {
const componentFolders = readdirSync(`./src/components`);
for (const folder of componentFolders) {
const componentFiles = readdirSync(`./src/components/${folder}`).filter(
(file) => file.endsWith('.js')
);
const { buttons, selectMenus } = client;
switch (folder) {
case "buttons":
for (const file of componentFiles) {
const button = require(`../../components/${folder}/${file}`);
buttons.set(button.data.name, button);
}
break;
case "selectMenus":
for (const file of componentFiles) {
const menu = require(`../../components/${folder}/${file}`);
selectMenus.set(menu.data.name, menu);
}
break;
default:
break;
}
}
};
};
HandleCommands.js :
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const fs = require("fs");
module.exports = (client) => {
client.handleCommands = async () => {
const commandFolders = fs.readdirSync("./src/commands");
for (const folder of commandFolders) {
const commandFiles = fs
.readdirSync(`./src/commands/${folder}`)
.filter(file => file.endsWith(".js"));
const { commands, commandArray } = client;
for (const file of commandFiles) {
const command = require(`../../commands/${folder}/${file}`);
commands.set(command.data.name, command);
commandArray.push(command.data.toJSON());
}
}
const clientId = "IDbutCensored";
const guildId = "IDbutCensored";
const rest = new REST({ version: '9' }).setToken(process.env.token);
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: client.commandArray,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
};
};
handleEvents.js :
const fs = require("fs");
module.exports = (client) => {
client.handleEvents = async () => {
const eventFolders = fs.readdirSync(`./src/events`);
for (const folder of eventFolders) {
const eventFiles = fs
.readdirSync(`./src/events/${folder}`)
.filter((file) => file.endsWith(".js"));
switch (folder) {
case "client":
for (const file of eventFiles) {
const event = require(`../../events/${folder}/${file}`);
if (event.once) client.once(event.name, (...args) => event.execute(...args, client));
else client.on(event.name, (...args) => event.execute(...args, client));
}
break;
default:
break;
}
}
}
}
The error is pretty self explanatory. The error InteractionAlreadyReplied means you already replied and you're trying to reply again.
You can solve this by doing the following;
• Use .followUp() to send a new message
• If you deferred reply it's better to use .editReply()
• Information about responding to slash commands / buttons / select menus
From the Discord docs
I'm getting this error message on heroku and I think I'm getting it cause of Procfile.
I'm using Worker at the moment, but I'm trying to figure out how to have heroku access both index.js and ping.js. Unless I'm reading the error message completely wrong and this could be a different issue. Any help is appreciated!
EDIT:
Here's my code for index.js
const Discord = require('discord.js');
const music = require('#koenie06/discord.js-music');
const fs = require('fs');
const { dir } = require('console');
const bot = new Discord.Client({
shards: "auto",
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Discord.Intents.FLAGS.DIRECT_MESSAGES,
Discord.Intents.FLAGS.GUILD_VOICE_STATES
]
});
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
//Command handler and aliases
fs.readdirSync('./commands/').forEach(dir => {
//in the commands folder, we gonna check for the category
fs.readdir(`./commands/${dir}`, (err, files) => {
//console log error(catch error)
if(err)throw err;
//checking if the files ends with .js if its a javascript file
var jsFiles = files.filter(f => f.split('.').pop() === 'js');
//if there is no commands in the file it will return
if(jsFiles.length <= 0) {
console.log("Can't find any commands");
return;
}
jsFiles.forEach(file => {
//console the loaded commands
var fileGet = require(`./commands/${dir}/${file}`);
console.log(`[COMMAND HANDLER] - File ${file} was loaded`);
//gonna let the commands run
try {
bot.commands.set(fileGet.help.name, fileGet);
// it search in the commands folder if there is any aliases
fileGet.help.aliases.forEach(alias => {
bot.aliases.set(alias, fileGet.help.name);
})
} catch(err) {
//catch error in console
return console.log(err);
}
})
})
})
/**
* ECHO STUFF
*/
//slash command to echo
bot.on('ready', async () => {
bot.user.setPresence({ activities: [{ name: "Tedi", type: "WATCHING"}] });
console.log("bye");
const data = {
name: 'echo',
description: 'Echo your text',
options: [{
name: 'text',
type: 'STRING',
description: 'The user input',
required: true,
}],
};
const command = await bot.guilds.cache.get('872986148681703444')?.commands.create(data);
})
bot.on('messageCreate', async message => {
if(message.author.bot || message.channel.type == 'DM') return
let prefix = '~'
let messageArray = message.content.split(' ');
let cmd = messsageArray[0];
let args = messageArray.slice(1);
//it will make the cmd work with his original name and his aliases
let commands = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)));
if(commands) {
if(!message.content.startsWith(prefix)) return
commands.run(bot, message, args, prefix);
}
})
//interactionCreate for echo slash command
bot.on('interactionCreate', async interaction => {
/**
* isButton() used to check if its a button
* isCommand() used to check if its a slash command
* isSelectMenu() used to check if its a dropdown menu
* isMessageComponent()
*/
if(interaction.isCommand()) {
if(interaction.commandName === 'echo') {
const text = interaction.options.getString('text');
await interaction.reply({ content: text, ephemeral: false}); //if ephemeral if true, it would make the slash command private
}
}
})
bot.login(process.env.token);
Here is my ping.js
const Discord = require("discord.js");
module.exports.run = async (Client, message, args, prefix) => {
message.channel.send("pong")
}
module.exports.help = {
name: "ping",
aliases: ["p"]
}
This error is not because of Heroku, it's basically because there is a file in your command handler that doesn't have a name while handling it, as example like this code over here:
const Discord = require("discord.js");
module.exports.run = async (Client, message, args, prefix) => {
message.channel.send("pong")
}
module.exports.help = {
// here the name isn't included
aliases: ["p"]
}
// so just check if you have a file without a name while handling it and put a name, and if you don't want aliases make it `aliases: []`
This command handler should be like thisCommands Folder commands > Subfolder e.g. Moderation > kick.jsThats how it works, also thank you for watching my videos, I'm UltraX :)
I'm encountering an error while making a command for my discord.js bot which says UnhandledPromiseRejectionWarning: ReferenceError: client is not defined even tho i defined client in the main bot file so i'm pretty confused the code for the command i'm making:
let ticketGen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("");
let ticketStr = "";
for(let i = 0; i < 3; i++) {
ticketStr += ticketGen[Math.floor(Math.random() * ticketGen.length)];
}
return ticketStr;
}
const fsn = require("fs-nextra");
const colors = require("colors");
module.exports = {
name: 'order',
description: 'Ordering something',
aliases: ['o'],
execute(message) {
let order = message.content.substring(8);
let customer = message.author.id
fsn.readJSON("./blacklist.json").then((blacklistDB) => {
let entry = blacklistDB[message.guild.id];
// Checks is server is blacklisted or not.
if(entry === undefined) {
// Gets ticket ID.
const ticketID = generateID();
// Sends ticket information to tickets channel.
client.guilds.cache.get("745409671430668389").channels.get("746423099871985755").send({embed: {
color: 0xFFFFFF,
title: message.author.username,
fields: [{
name: "New Order",
value: `${message.author.username} would like to order something.`,
}, {
name: "Order Description",
value: order,
}, {
name: "Order ID",
value: ticketID,
}, {
name: "Guild Infomation",
value: `This order came from ${message.guild} (${message.guild.id}) in ${message.channel} (${message.channel.id}).`,
}, {
name: "Order Status",
value: "Unclaimed",
}],
timestamp: new Date(),
footer: {
text: "Taco Bot"
}
}}).then((m) => {
m = m.id;
//rest of the code
forgive me if it was an stupid error i'm completely new to coding and was watching youtube videos to learn the code for the main bot file:
const fs = require("fs");
const { prefix, token, ownerID } = require('./config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("ready", () => {
console.log(`ready!.`);
console.log(token);
// Activities
const activities_list = [
`Serving Tacos | .help`,
`Preparing Orders | .help`
];
setInterval(() => {
const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
client.user.setActivity(activities_list[index]);
}, 10000);
});
//Joined Guild
client.on("guildCreate", (guild) => {
console.log(colors.green(`Joined New Guild, ${guild.name}`));
});
//Left Guild
client.on("guildDelete", (guild) => {
console.log(colors.green(`Left Guild, ${guild.name}`));
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
process.on("error", () => {
console.log("Oops something happened!");
});
client.login(token);
There is no need to create a new client, and absolutely no need to pass it in as an additional argument in your execute method. As you have seen in the comments, the issue is that while client is defined in your main bot file, it is not defined in your command's file. Variables defined in one file aren't accessible in all other files, unless you specifically export that variable using module.exports.
But luckily, you don't need to export your client or pass it as a parameter; discord.js conveniently has a message.client property on the message object which allows you to access your client easily. All you need to do to fix your code is add a single line to the top of your execute() method in your command file:
module.exports = {
name: 'order',
description: 'Ordering something',
aliases: ['o'],
execute(message) {
const client = message.client; //<- Line added right here
let order = message.content.substring(8);
let customer = message.author.id
fsn.readJSON("./blacklist.json").then((blacklistDB) => {
let entry = blacklistDB[message.guild.id];
// Checks is server is blacklisted or not.
if(entry === undefined) {
// Gets ticket ID.
const ticketID = generateID();
// Sends ticket information to tickets channel.
client.guilds.cache.get("745409671430668389").channels.get("746423099871985755").send({embed: {
color: 0xFFFFFF,
title: message.author.username,
fields: [{
name: "New Order",
value: `${message.author.username} would like to order something.`,
}, {
name: "Order Description",
value: order,
}, {
name: "Order ID",
value: ticketID,
}, {
name: "Guild Infomation",
value: `This order came from ${message.guild} (${message.guild.id}) in ${message.channel} (${message.channel.id}).`,
}, {
name: "Order Status",
value: "Unclaimed",
}],
timestamp: new Date(),
footer: {
text: "Taco Bot"
}
}}).then((m) => {
m = m.id;
//rest of the code
And that's all! Problem solved. That message.client property is amazingly convenient.
Relevant Resources:
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=client
I will suggest you to add the client in the command.execute() too, because it is easier to use. So you will have:
try {
command.execute(client, message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
And in every command file you just simply do this.
execute(client, message, args) {
//code
}
So I'm making a discord bot, and I want to have a file called createcommand.js, so when you run it with node createcommand.js is starts asking for user input. So it ask "what should the name be" and then it creates a file from the input with the name, the it will ask "what should it send" and then it will add the command in the file. I've tried with https://pastebin.com/UARJcExh
#!/usr/bin/env node
const fs = require('file-system');
const ncp = require('ncp');
const init = async function () {
const modulee = require('../index.js')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
const name =
readline.question(`Welcome, what should the name of the command be?`, (input) =>{
js.name = input
let config = {}
readline.question(`What should it output`, (input) => {
js.output = input;
readline.close();
})
})
}
var js = `
exports.run = async (client, message, args, level) => {
message.channel.send(output)
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: [],
permLevel: "User"
};
exports.help = {
name: "support",
category: "Miscelaneous",
description: "",
usage: name
};
module.exports = config;`;
fs.appendFile(`${js.name}`, js ,function (err) {
if (err) {
throw new Error(err);
}})
init();
. But it doesn't work.
i made a discord bot with discord.js and tried to do a help command to show the user all available commands.
example command: avatar.js
module.exports.run = async(bot, message, args) => {
let msg = await message.channel.send("doing some magic ...");
let target = message.mentions.users.first() || message.author;
await message.channel.send({files: [
{
attachment: target.displayAvatarURL,
name: "avatar.png"
}
]});
msg.delete();
}
module.exports.help = {
name: "avatar",
description: "show the avatar of a user",
usage: "[#user]"
}
Then i tried to send a message with the complete list of the commands like:
command 1
description
usage
command 2
description
usage
...
help.js
const fs = require("fs");
const Discord = require("discord.js");
module.exports.run = async(bot, message, args, con) => {
fs.readdir("./cmds/", (err, files) => {
if(err) console.error(err);
let jsfiles = files.filter(f => f.split(".").pop() === "js");
if(jsfiles.length <= 0) {
console.log("No commands to load!");
return;
}
var namelist = "";
var desclist = "";
var usage = "";
let result = jsfiles.forEach((f, i) => {
let props = require(`./${f}`);
namelist = props.help.name;
desclist = props.help.description;
usage = props.help.usage;
});
message.author.send(`**${namelist}** \n${desclist} \n${usage}`);
});
}
module.exports.help = {
name: "help",
description: "show all commands",
usage: ""
}
my code is kinda working but it only sends the first command.
Im pretty new to javascript and i can't find a solution to this.
I tried to google everything on foreach maps discord collections and stuff but i cant find a example where the results get combined together.
If anybody can help me or give me a hint where i can search for something like this. Would be awesome.
The reason your code is only sending the one command is because your code only calls message.author.send('...' once. You successfully set the variables namelist, desclist, and usage with data from every file, but your .forEach(... loop just overwrites all of the data when it moves to the next files.
Try to send data inside each iteration of the .forEach(... loop like this:
var namelist = "";
var desclist = "";
var usage = "";
let result = jsfiles.forEach((f, i) => {
let props = require(`./${f}`);
namelist = props.help.name;
desclist = props.help.description;
usage = props.help.usage;
// send help text
message.author.send(`**${namelist}** \n${desclist} \n${usage}`);
});
you should do this in an Array and it'll solve the problem, so it should look like this.
module.exports.run = async(bot, message, args, con) => {
fs.readdir("./cmds/", (err, files) => {
if(err) console.error(err);
let jsfiles = files.filter(f => f.split(".").pop() === "js");
if(jsfiles.length <= 0) {
console.log("No commands to load!");
return;
}
let result = jsfiles.forEach((f, i) => {
let props = require(`./${f}`);
let filesArray = [props.help.name, props.help.description, props.help.usage]
message.author.send(`**${filesArray[0]}** \n${filesArray[1]} \n${filesArray[2]}`);
});
});
}
sorry for the late response.
Here is my take on this.... it works for me.
const Discord = require('discord.js');
const fs = require("fs");
module.exports = {
name: 'help',
description: 'Lists available commands',
async run(client, message, args, con) {
fs.readdir("./commands/", (err, files) => {
if(err) console.error(err);
let jsfiles = files.filter(f => f.split(".").pop() === "js");
if(jsfiles.length <= 0) {
console.log("No commands to load!");
return;
}
var namelist = "";
var desclist = "";
let result = jsfiles.forEach((f, i) => {
let props = require(`./${f}`);
namelist = props.name;
desclist = props.description;
message.author.send(`**${namelist}** \n${desclist} \n`);
});
});
}
I'm using DiscordJS 12 so this may not work on 11.
const discord = require('discord.js')
module.exports = {
name: 'cmd-list',
async run(client, message, args) {
const commandFiles = readdirSync(join(__dirname, "<your commands folder name>")).filter(file => file.endsWith(".js")); // Get files
const cmdmap = commandFiles.map(files => `${files}`).join(' | Working\n')
const embed = new discord.MessageEmbed()
.setDescription(cmdmap)
message.channel.send(embed)
}
}
You can just use discord 's normal embed system like this:
const { Message, MessageEmbed } = require('discord.js');
Add this to the top of your code then type everything as the following to make a normal embed like this:
const embed = new MessageEmbed()
.setTitle(`Your title!`)
.setColor(5814783)
.setTimestamp()
.setThumbnail('')
.setDescription(`Your description`)
.addFields(
{ name: 'Field 1', value: `Filed 1 Value`},
{ name: 'Field 2', value: `Filed 2 Value` },
{ name: 'Field 3', value: `Filed 3 Value`, inline: true },
{ name: 'Field 4', value: `Filed 4 Value`, inline: true },
{ name: 'Field 5', value: `Field 5 Value`, inline: true },
)
message.channel.send(embed2)