DiscordJS Error, Embed Message Within Command Handler - node.js

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.

Related

Edit variables with command discord.js v13

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.

Discord.js bot doesn't actually kick member after mentioned

I am trying to make a kick command for my Discord bot, however I'm not sure why it does not kick the member after mentioned.
When I use the command #kick itself without anything it works and says "ERROR: You need to mention a member".
module.exports = {
name: 'kick',
description: "This command kicks members.",
execute(message, args){
const member = message.mentions.users.first();
if (member) {
const memberTarget = message.guild.members.cache.get(member.id)
memberTarget.kick();
message.channel.send("User has been successfully kicked.")
}
else {
message.channel.send('**ERROR:** You need to mention a member.')
}
}
}
Also, yes I have checked the bot permissions, it has administrator.
if (message.mentions.users.size === 1) {
const member = client.users.fetch(message.mentions.users.first());
member.Kick()
}
else {
message.reply("Mention a user.")
// or its more than 1 mentions idk....
}

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'
}
]);
}
});

Discord Bot Update: How to play audio?

So recently the new version of the discord bot API came out for Node along with interactions and all that. And, they also changed some other stuff, don't know why. but they did.
I was trying to just try out the audio playing code to see how it works and maybe update some of my older bots, when I ran into the issue that it just doesn't work. I've been following the docs at https://discordjs.guide/voice/voice-connections.html#life-cycle and https://discordjs.guide/voice/audio-player.html#life-cycle but they're really just not working.
Just testing code looks like this:
const Discord = require('discord.js');
const {token} = require("./config.json");
const { join } = require("path");
const {joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus, VoiceConnectionStatus, SubscriptionStatus, StreamType } = require("#discordjs/voice");
client.on("ready", async () => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const audioPlayer = createAudioPlayer();
const resource = createAudioResource(createReadStream(join(__dirname, "plswork.mp3")));
const subscription = connection.subscribe(audioPlayer);
audioPlayer.play(resource);
audioPlayer.on(AudioPlayerStatus.Playing, () => {
console.log("currently playing");
console.log("resource started:", resource.started);
});
audioPlayer.on('error', error => {
console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`);
});
audioPlayer.on(AudioPlayerStatus.AutoPaused, () => {
console.log("done?");
});
I create a connection, audioPlayer, and resource but after subscribing the connection to the audioPlayer and playing the resource no audio is played, no error is raised (in AudioPlayer.on("error"...)) and the AutoPaused status is immediately called.
By logging the resource I see that resource.playbackDuration is 0, but I don't know how to fix this as I can't find much on the internet about this topic.
From what I can tell by looking at your code you are not requiring the correct json from #discordjs/voice, you need to require at least AudioPlayerStatus and createAudioPlayer. Also, by your resource I think you're trying to create an AudioResource, so you'll need it as well. You'll need something like:
const { AudioPlayerStatus, createAudioPlayer, AudioResource, StreamType } = require('#discordjs/voice');
/* */
const audioPlayer = createAudioPlayer();
/* */
audioPlayer.play(resource);
audioPlayer.on(AudioPlayerStatus.Playing, () => {
// Do whatever you need to do when playing
});
/* */
In conclusion, I suggest you to look up the life cycle of an AudioPlayer and the creation of an AudioPlayer. Be sure, also, to create correctly your resource, here you'll find the AudioResource documentation if you'll ever need it.

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

Resources