Edit variables with command discord.js v13 - node.js

I'm working on a discord.js v13 bot, and I want to make a command that add the channel Id where the command executed in an existing variable and, another command that works only with the channels in the variable
This is the variable
const ch = ["1068584484473143386","1068570010756337705","","","",]
The command that works with the channels stored
client.on("message", async (message) => {
if(message.author.bot) return;
if(ch.includes(message.channel.id)) {
const prompt = message.content
message.channel.sendTyping(60)
const answer = await ask(prompt);
message.channel.send(answer);
}});
Edit : I used array.push() in an command but it didn't work it didn't do anything
client.on('messageCreate', message => {
if (message.content === '..auto-on') {
if(!message.member.permissions.has("ADMINISTRATOR")) {
message.channel.reply("**You don't have permissions**")};
ch.push(`${message.channel.id}`)
message.channel.reply(`**Added ${message.channel} to auto-reply**`);
}});
Edit 2 : it worked but it get reset after restarting

If I understood you correctly you want to insert a new channel Id to an existing array, you can do that using the Array.push() method.
Example:
const arrayofItems = ["Flour", "Milk", "Sugar"]
console.log(arrayofItems) // ["Flour", "Milk", "Sugar"]
arrayofItems.push("Coffee")
console.log(arrayofItems) // ["Flour", "Milk", "Sugar", "Coffee"]
Make sure to learn the basics of JS before starting out with libraries such as Discord.JS.

Related

Store channel id with command

I'm working on a discord.js v13 bot and I want to make a commmand that stores the channel id the command sent in then, A command that works only the channels stored .
I want an example of the code to do this :)
I didn't try bec I can't realize how to do it :)
It's pretty easy when you know how to do it actually. You can listen on messages in different channels with:
client.on("message", (message) => {
console.log(`Channel-ID: ${message.channel.id}`);
});
See the Docs: Message
I don't know if it works in v13 but if not try this:
client.on("message", (message) => {
console.log(`Channel-ID: ${message.channelId}`);
});
To react when a command is used:
client.on("message", (message) => {
if(message.startsWith("!channel")){
console.log(`Channel-ID: ${message.channelId}`);
}
});

DiscordJS Error, Embed Message Within Command Handler

I am trying to make my bot use an embed message however it will not work because i keep getting the Message not defined error
I have tried just using the author.bot but then it comes up with Author not defined, Something important to mention, I am using the command handler provided by the DiscordJS Guide and then i tried using the DiscordJS Guide for Embeds and it didnt work, thats basically how i ended up here
Code Below
const { SlashCommandBuilder } = require('#discordjs/builders');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('embed')
.setDescription('A W.I.P Embed'),
async execute(interaction) {
const Embed1 = new MessageEmbed().setTitle('Some title');
if (message.author.bot) {
Embed1.setColor('#7289da');
}
}
};```
This is because you are using a message object when the exported item is: interaction.
Change:
if (message.author.bot) {
Embed1.setColor('#7289da');
}
to:
if (interaction.user.bot) {
Embed1.setColor('#7289da');
}
There is no interaction.author and only a interaction.user, therefore is the only way to check if it's a bot.

How to use AutoComplete in Discord.js v13?

How do you use the Discord autocomplete feature using Discord.js v13.6, #Discord.js/builders v0.11.0?
I couldn't find a guide or tutorial anywhere
Here is what I have so far:
const { SlashCommandBuilder } = require('#discordjs/builders');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('autocomplete')
.setDescription('Test command')
async execute(interaction) {
await interaction.replay({ content: "Hello World" })
},
};
Basically, autocomplete is a way for bots to suggest values for integer and string options for command interactions.
When a user uses a slash command (lets call it help) and this command takes a string argument of which command to describe, it might be annoying for the user to know every single command. Autocomplete fixes this by suggesting values (in this case commands).
You can set an option as autocompetable when you use the slash command builder:
import { SlashCommandBuilder } from '#discordjs/builders';
const command = new SlashCommandBuilder()
.setName('help')
.setDescription('command help')
.addStringOption((option) =>
option
.setName('command')
.setDescription('The command to get help for.')
.setRequired(true)
// Enable autocomplete using the `setAutocomplete` method
.setAutocomplete(true)
);
When autocomplete is set to true, every time a user starts using an option, Discord will send a request to your bot (as an interaction) to see what should be suggested. You can handle the response as such:
client.on('interactionCreate', async (interaction) => {
// check if the interaction is a request for autocomplete
if (interaction.isAutocomplete()) {
// respond to the request
interaction.respond([
{
// What is shown to the user
name: 'Command Help',
// What is actually used as the option.
value: 'help'
}
]);
}
});

How to do a 'Report Bug' command with discord.js

I ve been trying to do a report 'bug command'.
This is my code so far:
bot.on('message', async (client, message, args) => {
let parts = message.content.split(" ");
if(parts[0].toLocaleLowerCase() == '!bugreport') {
const owner = client.users.cache.get('567717280759283883');
const query = args.join(" ");
if(!query) return message.channel.send('Bitte schildere den Bug').then(m => m.delete({timeout: 5000}))
const reportEmbed = new Discord.MessageEmbed()
.setTitle('Fehlermeldung')
.setThumbnail(message.guild.iconURL())
.addField('Fehler:', query)
.addField('Server:', message.guild.name, true)
.setFooter(`Gesendet von ${message.author.tag}`, message.author.displayAvatarURL({dynamic: true}))
.setTimestamp();
owner.send(reportEmbed);
}
});
The error code always says that 'content' is undefined, but I do not understand that
Assuming your bot is a Client, it seems you're expecting the message event to pass more arguments than it actually does. Although that would indicate you're trying to read content from undefined, not that content itself is undefined.
The Client#message event allows 1 parameter which is an instance of Message. Change your code to this
bot.on('message', async (message) => {
//code here
})
You probably got confused with message handlers, where you can customize the function to your liking.
The message event only passes the received message according to the discord.js docs.  Your event handler should look more like this at the start:
bot.on('message', async (message) => {
Note this won't completely fix your command, as you'll still be missing the args and client parameters. Here's my best guess as to where to get those from based on how you're using them:
const args = parts.splice(0,1); // gives array of everything after "!bugreport"
const client = bot; // alternatively, replace client with bot in your code

Unresolved method in WebStorm (discord.js, code works)

I'm wrapping my head around this. Autocompletion does not work .then(r => { HERE }) either.
Kinda starting out with this and would be way easier if it just works (works outside of the promise).
Code runs without any problems as well. delete method is recognized as well but not at that part.
I have this problem in a bigger project as well and it gets me confused.
Trying to find something on the web for a few hours, but couldn't find anything that helps me out. Hope I wasn't blind at some point :P
Whole test source:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something').then(r => r.delete(5000));
}
});
Problem:
If you need to delete command triget message you can use
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("message", message => {
if (message.content === 'test'){
message.channel.send('something')
message.delete(1000)
.catch(console.error)
}
});
If you use need to delete response message after some time you code must work, but you can try use reply method.
client.on("message", message => {
if (message.content === 'test'){
message.reply('somethink')
.then(msg => {
msg.delete(10000)
})
.catch(console.error);
}
});
Maybe problem in you discord.js version? In v.12 version you need use
msg.delete({ timeout: 10000 })
Just means webstorm can't discern what functions the object will have after the promise resolves. This is because the message create action in discord.js has multiple return types. So it's possible for the message object not to be passed into r, for instance in the event that the message failed to send, possibly by trying to send a message to a channel without the proper permissions.
If you add a check to confirm that r is the same type as message before trying to call .delete() I believe the warning will go away.
You can observe the potential error this highlight is warning you of by removing the bots permission to send messages in a channel, then by sending "test" to that same channel.
Having had the error recently and having found a solution, if for example you want a text channel, by doing a get you can have a text or a voice . so check if the channel instance TextChannel (for my example)
let channel = client.guilds.cache.get(process.env.GUILD_ID).channels.cache.get(config.status_channel);
if (channel instanceof TextChannel) {
await channel.messages.fetch()
}

Resources