I want to edit a existing embed in discord.js. But Im getting the error that message.edit is not a function.
Code:
await mongo().then(async mongoose => {
const results = await applicationSchema.findOneAndDelete({
guildId: guildId,
applicationId: applicationId
})
for (const application of results.applications) {
const { applicationId, userId, appliedfor } = application
user22 = bot.users.cache.get(userId);
let acceptembed = new Discord.MessageEmbed()
.setColor(colours.maincolour)
.setDescription(`Lieber <#${userId}>! Deine Bewerbung wurde angenommen!`)
user22.send(acceptembed)
// m = reaction.messages.cache.fetch(applicationId);
// m.edit(null, { embed: output });
let message = bot.channels.cache.get('823554343827013652').messages.fetch(applicationId)
await message.edit()
}
})
The message you are sending should be put in .edit() as it would for .send(). Please note that this does not add to the message, but instead replaces it.
bot.channels.cache.get('823554343827013652').messages.fetch(applicationId)
.then(message => message.edit(acceptembed));
Related
So im making a ban slash command using .addUserOption but when i try to get the permissions from the .addUserOption so that the bot doesnt crash when trying to ban an admin i get an error. I already know why, you cant get permissions from a user, only a GuildMember, so how would i go from user to GuildMember?
CODE: (if needed)
const { SlashCommandBuilder } = require('#discordjs/builders');
const { MessageEmbed } = require('discord.js')
module.exports = {
data: new SlashCommandBuilder()
.setName("ban")
.setDescription("Bans the specified user!")
.addUserOption((option) =>
option
.setName("user")
.setDescription('Who do you want to ban?')
.setRequired(true)
)
.addStringOption((option) =>
option
.setName('reason')
.setDescription("Whats the reason youre banning this user?")
),
async execute(client, interaction) {
if (client.cooldowns.has(interaction.user.id)) {
let cooldownEmbed = new MessageEmbed()
.setTitle("Please wait for the cooldown to end.")
.setDescription("This is so that the bot doesnt overload")
.setColor("RED")
.setFooter("The cooldown is 5 sec")
.setTimestamp()
interaction.reply({ embeds: [cooldownEmbed], ephemeral: true });
} else {
const user = interaction.member
const permission = user.permissions.has('BAN_MEMBERS');
let embed = new MessageEmbed()
.setTitle("❌️ | You dont have the permissions to use this command.")
if(!permission)
return interaction.reply({embeds: [embed]})
const tgt = interaction.options.getUser('user')
let permsEmbed = new MessageEmbed()
.setTitle("The user you are trying to ban is an Administrator!")
.setColor("RED")
if (tgt.permissions.has('BAN_MEMBERS')) return interaction.reply({ embeds: [permsEmbed]})
const reason = interaction.options.getString('reason') || "No reason specified"
client.cooldowns.set(interaction.user.id, true);
setTimeout(() => {
client.cooldowns.delete(interaction.user.id);
}, client.COOLDOWN_SECONDS * 1000);
}
}
}
I checked CollinD's suggestion and i managed to fix the problem. Heres the code if anyone else runs into this problem and needs an answer.
CODE:
const tgt = interaction.options.getUser('user');
let permsEmbed = new MessageEmbed()
.setTitle('The user you are trying to ban is an Administrator!')
.setColor('RED');
const guildMember = interaction.guild.members.cache.get(tgt.id);
if (guildMember.permissions.has('BAN_MEMBERS'))
return interaction.reply({ embeds: [permsEmbed] });
So I was making welcome system on my discord bot and encountered the problem where my code dosent work (dosent respond) but there is no error.
So i made command to set welcome channel --It Works
I also setted Schema (i using mongoDB) where i will save guildId, ChannelId & RoleID. --Work
Then i have file: welcomer.js which actually need to send the message when user join, but it dosent, i also dont have any errors in console:
SetChannel.js:
async execute(interaction, client, message) {
try {
if(!interaction.member.permissions.has('ADMINISTRATOR')) return interaction.reply('You are not allowed to use this command')
const channel = interaction.options.getChannel('channel')
const role = interaction.options.getRole('role')
Schema.findOne({Guild: interaction.guild.id}, async(err, data) => {
if(data) {
data.Channel = channel.id
data.Role = role.id
data.save()
} else {
new Schema({
Guild: interaction.guild.id,
Channel: interaction.channel.id,
}).save()
}
})
interaction.reply(`${channel} has been set as welcome channel
${role} will be given to the new members when they join the server.`)
//.then(console.log)
.catch(console.error)
}
catch(err) {
console.log(err)
}
}
}
welcomeSchema.js:
const mongoose = require('mongoose')
const Schema = new mongoose.Schema({
Guild: String,
Channel: String,
Role: String,
})
module.exports = mongoose.model('welcome-channel', Schema)
welcomer.js:
//const client = require('../index')
const Schema = require('../database-schema/welcomeSchema')
const { MessageEmbed, Client, Intents, Discord } = require('discord.js')
const allIntents = new Intents(32767);
const client = new Client({intents: [ allIntents ]})
client.on('guildMemberAdd', async (member, guild) => {
Schema.findOne({ Guild: member.guild.id }, async (e, data) => {
if (!data) {
return console.log('No channel to send the message!')
}
const channel = member.guild.channels.cache.get(data.Channel)
let role = member.guild.roles.cache.get(data.Role)
const embed = new MessageEmbed()
.setTitle('Welcome!')
.setColor('BLURPLE')
.setDescription(`Welcome to the server, ${member.tag}! I hope you will have a great time here.`)
.setTimestamp()
channel.send({ embeds: [embed] })
member.role.add()
})
})
first issue: all the embeds in my code stopped working - no matter what command I try to run if it has an embed in it I get the error: DiscordAPIError: Cannot send an empty message
second issue: I'm currently programming a mute command with a mongoDB database, it puts everything I need it in the database however if I try to mute someone it ends up only muting them for 1s by default, basically completely ignoring the second argument. heres what I want the command to do: when you mute someone you need to provide the user id and a time (works in ms) + reason then it puts it in the data base.
here's the code: [P.S. Im not getting an error message, it just doesnt work properly like I want it to]
const mongo = require('../mongo.js')
const muteSchema = require('../schemas/mute-schema.js')
const Discord = require('discord.js')
const ms = require ("ms")
module.exports = {
commands: 'mute',
minArgs: 2,
expectedArgs: "<Target user's #> <time> <reason>",
requiredRoles: ['Staff'],
callback: async (message, arguments) => {
const target = message.mentions.users.first() || message.guild.members.cache.get(arguments[0])
if (!target) {
message.channel.send('Please specify someone to mute.')
return
}
const { guild, channel } = message
arguments.shift()
const mutedRole = message.guild.roles.cache.find(role => role.name === 'muted');
const guildId = message.guild.id
const userId = target.id
const reason = arguments.join(' ')
const user = target
const arg2=arguments[2]
const mute = {
author: message.member.user.tag,
timestamp: new Date().getTime(),
reason,
}
await mongo().then(async (mongoose) => {
try {
await muteSchema.findOneAndUpdate(
{
guildId,
userId,
},
{
guildId,
userId,
$push: {
mutes: mute,
},
},
{
upsert: true,
}
)
} finally {
mongoose.connection.close()
}
})
message.delete()
user.roles.add(mutedRole)
setTimeout(function() {
user.roles.remove(mutedRole)
}, ms(`${arg2}`));
try{
message.channel.send(`works`)
}
catch(error){
const embed3 = new Discord.MessageEmbed()
.setDescription(`✅ I Couldn't DM them but **${target} has been muted || ${reason}**`)
.setColor('#004d00')
message.channel.send({ embeds: [embed3] });
}
},
}
djs v13 has a new update where you need to send embeds like this:
const exampleEmbed = new Discord.MessageEmbed()
.setTitle('example')
.setDescription('example')
.setColor('RANDOM')
message.channel.send({embed: [exampleEmbed]});
I want to make a poll command, where the bot send an embed with the poll and afterwards reacts to it with some emojis. I added the reaction Intent to my index.js but still get an error everytime I call the command. This is my code:
const { SlashCommandBuilder } = require('#discordjs/builders')
const { MessageEmbed } = require('discord.js')
module.exports = {
data: new SlashCommandBuilder()
.setName("poll")
.setDescription("Create a quick poll.")
.addStringOption(option => option.setName("question").setDescription("The question").setRequired(true)),
async execute(interaction) {
const question = interaction.options.getString("question")
const message = await interaction.reply({embeds: [
new MessageEmbed()
.setColor('GREEN')
.setTitle(`Discord Poll`)
.setAuthor(`Requested by ${interaction.member.user.tag}`, interaction.member.user.avatarURL())
.setDescription(question)
.setThumbnail('https://pngimg.com/uploads/question_mark/question_mark_PNG126.png')
.setTimestamp()
.setFooter('Bot by Iuke#4681', 'https://cdn.discordapp.com/emojis/912052946592739408.png?size=96')
]})
message.react(':check:')
.then(() => message.react(':neutral:')
.then(() => message.react(':xmark:')))
}
}
And get this error:
TypeError: Cannot read properties of undefined (reading 'react')
You will need to fetch the reply. You can use the fetchReply option and set it to true.
module.exports = {
data: new SlashCommandBuilder()
.setName('poll')
.setDescription('Create a quick poll.')
.addStringOption((option) =>
option
.setName('question')
.setDescription('The question')
.setRequired(true),
),
async execute(interaction) {
const question = interaction.options.getString('question');
const message = await interaction.reply({
embeds: [
new MessageEmbed()
.setColor('GREEN')
.setTitle(`Discord Poll`)
.setAuthor(
`Requested by ${interaction.member.user.tag}`,
interaction.member.user.avatarURL(),
)
.setDescription(question)
.setThumbnail(
'https://pngimg.com/uploads/question_mark/question_mark_PNG126.png',
)
.setTimestamp()
.setFooter(
'Bot by Iuke#4681',
'https://cdn.discordapp.com/emojis/912052946592739408.png?size=96',
),
],
fetchReply: true,
});
// you could use await to react in sequence instead of then()s
await message.react(':check:');
await message.react(':neutral:');
await message.react(':xmark:');
},
};
According to the docs you need to add the fetchReply option on interaction.reply like this:
const message = await interaction.reply({ content: 'Pong!', fetchReply: true });
db module is possible to add writings. But is it possible to subtract the writings? It may also be caused by my incompetence this is my code:
const db = require('quick.db')
const Discord = require('discord.js')
exports.run = async (client, message, args, config) => {
if(message.author.id === 'My ID'){
let user = message.mentions.members.first() || message.author
if (args[0]) return message.channel.send(`${message.author}, bir eşya ismi girmen gerek!`)
db.subtract(`${user.id}.envan`, args[0])
let bal = await db.fetch(`${user.id}.envan`)
let embed = new Discord.RichEmbed()
.setAuthor(`Your item is received!`, message.author.displayAvatarURL)
.addField(`Received Item:`, `${args[0]}`)
.setColor("RED")
.setTimestamp()
message.channel.send(embed)
}else{
return message.channels.send('Yetkin yok knk').then(msg => {
setTimeout(() => {
msg.delete()
}, 2500);
})
}
}
You can use db.delete if you want to delete the reference instead of 'subtracting' (as it seems to be your goal in your question)
db.subtract(`${user.id}.envan`, Math.floor(args[0]))
try this.