How can i edit button label 0 to 1 when pressed button on Discord.JS v14 - discord.js

if (!interaction.isButton()) return;
const pol = await polls.findOne({ message: interaction.message.id });
if (!pol) return;
await interaction.deferReply({
ephemeral: true
});
if (pol.voters.includes(interaction.user.id)) return interaction.editReply({
embeds: [{
color: 0xff0000,
title: "❌ Already Voted!"
}]
});
pol.votes = pol.votes || {};
if (pol.votes[interaction.customId]) pol.votes[interaction.customId] += 1
else pol.votes[interaction.customId] = 1;
pol.voters.push(interaction.user.id);
await polls.findOneAndUpdate({ message: pol.message }, pol);
interaction.editReply({
embeds: [{
color: 0x008000,
title: "✅ Voted Successfully"
}]
});
},};
const m = interaction.message;
m.edit({
components: m.components?.map(row => {
row.components = row.components?.map(v => {
v.label = `${pol.votes[v.customId] || 0}`;
return v;
});
return row;
})
})
How can i edit button label 0 to 1 when pressed button on Discord.JS v14
This code is not working
I converted this code V13 to v14 but don't worked. How can i fix it? Looked guide but i don't understand it

you can use the same button twice, but change its label while editing.
Reviewing your code, you're not really sending any components with the message. Try the code below.
const button = new ButtonBuilder().setLabel("0").setStyle(ButtonStyle.PRIMARY).setCustomId("test");
interaction.reply({
content: "Hello World",
components: [new ActionRowBuilder().addComponents(button)]
});
// inside the collector, to edit the label
interaction.editReply({
components: [new ActionRowBuilder().addComponents(button.setLabel("1"))]
})

Related

Deleting an embed on discord js is not working

This probably has a very simple fix, but I'm a very beginner programmer so I have no clue what to do. I am trying to delete an embed after 5 seconds, and the error is saying "msg.delete is not a function" This is my code. (I am using slash commands).
const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('br')
.setDescription('Battle to the death with your friends'),
async execute(message) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('joinbutton')
.setLabel('JOIN BATTLE ROYALE')
.setStyle(1),
);
var embed = new EmbedBuilder()
.setTitle(`BATTLE ROYALE STARTING`)
.setColor("0090FF")
.setDescription(`<#${message.user.id}> is starting a battle royale match. Use the button below to join!`)
.setFooter({text: message.user.username, iconURL: message.user.avatarURL()})
.setTimestamp()
message.reply({ embeds: [embed], components: [row] }).then(msg => msg.delete({timeout: 5000}))
},
};
I also have code in my index.js which I don't think could be the problem but I'm just gonna send it anyway.
var brdb = require('./models/br')
if(interaction.isButton())
{
if(interaction.customId == 'joinbutton')
{
var data = {
userid: interaction.user.id,
hp: 100
}
console.log(data)
}
}
This is because "message" is a CommandInteraction, not a Message. You should instead use message.deleteReply() as mentioned in the docs.

Animated emoji name doesn't display as i wanted

So simple emoji command where it's supposed to display it's information like name, id, link and etc you get the idea. Works all fine with default emojis and custom server emojis but the moment its animated the name changes from
"<a:CS_Shrug:740861064257863690>" to just ":CS_Shrug:" i know this has something with discord displaying stuff but is there anyway to get around this? People asked for code:
let args = msg.content.slice(PREFIX.length).trim().split(/ +/); //slice message in an array
if (args[1]) { //if theres something mentioned
let newRawdata = fs.readFileSync('./emojis.json');
let newData = JSON.parse(newRawdata);
var findEmoji = Object.values(newData).find(object => object === args[1]) //just a file with all unicode emojis bc yes
if (args[1].startsWith("<:") && args[1].endsWith(">")) { //custom server emoji
//not important code
} else if (args[1].startsWith("<a:") && args[1].endsWith(">")){
let ForBots = args[1]
let namever1 = ForBots.replace('<a','')
let namever2 = namever1.replace('>','')
let namever3 = namever2.split(':')
let name = namever3[1]
let id = namever3[2]
var inforEmbed2 = new Discord.MessageEmbed()
.setColor('#2ab94c')
.setTitle('Emoji information')
.addFields(
{ name: 'Type', value: "`Animated server emoji`", inline: false },
{ name: 'Name', value: "`"+name+"`", inline: false },
{ name: 'ID', value: "`"+id+"`", inline: true },
{ name: 'For bots', value: "`"+args[1]+"`", inline: true }, //should display `<a:CS_Shrug:740861064257863690>` but instead `:CS_Shrug:`
{ name: 'Emoji link', value: "https://cdn.discordapp.com/emojis/"+id+".gif?/v=1", inline: false },
)
.setThumbnail('https://cdn.discordapp.com/emojis/'+id+'.gif?/v=1')
.setFooter("C.U.A.L - Bot| ;help", 'https://cdn.discordapp.com/attachments/864287060424785941/872600493082415124/Avatar_1.png')
msg.channel.send(inforEmbed2)
} else if (findEmoji) { //default emoji
//more none important code
} else msg.reply("donno wtf this is") // wasnt an emoji
} else msg.reply("Gotta give me something first")

Discord.js V.13 collect once with Collector

I'm making a suggest system for my bot which starts of with the user pressing a button in a channel which then prompts them to say what they want to suggest and then their suggestion gets sent to a different channel where others can vote on it. So what I want to make it do is I want it to create a createMessageComponentCollector and then register all the good/bad votes under a a period of 24h, if the suggestion got 10 good votes it's approved, otherwise it's not. Here is my code for it so far but what I'm having issue with is to start the ComponentCollector one time and then make it register the votes for each suggestion. this is the part of my code that I'm having trouble with
client.channels.cache.get("909420357285474334").send({ embeds:[embed], components:[row] })
const filter1 = m => m.customId === "Yes" && i.messageId === interaction.messageId
collector1 = interaction.channel.createMessageComponentCollector({filter: filter1, time: 86400000});
const filter2 = m => m.customId === "No" && i.messageId === interaction.messageId
collector2 = interaction.channel.createMessageComponentCollector({filter: filter2, time: 86400000});
if (interaction.customId === "Yes") {
collector1.on('collect', async i => {
client.logger("collector1 collected")
});
collector1.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
});
}
if (interaction.customId === "No") {
collector2.on('collect', async i => {
client.logger("collector2 collected")
});
collector2.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
});
}
your question is a little bit unclear but I think I get what your looking for, if you want to check if a user already has pressed a button you could add them to a Set() constructor, now you need to set a new Set() which you can define in your startup file like this:
client.alreadyPressed = new Set()
and when you've done that one possible way to do it in your suggestion code could be like this:
collector1.on('collect', async i => {
client.logger("collector1 collected")
if (client.alreadyPressed.has(interaction.user.id)) {
i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
return;
} else {
i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
}
client.alreadyPressed.add(i.user.id)
});
collector1.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
});
collector2.on('collect', async i => {
client.logger("collector2 collected")
if (client.alreadyPressed.has(interaction.user.id)) {
i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
return;
} else {
i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
}
client.alreadyPressed.add(i.user.id)
});
collector2.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
});
one problem with this is that they only can vote once in the whole channel which im not sure if that's what you wanted

Adding buttons to an application sent to specified applicationChannelId

Yes I know TLDR but I would appreciate the help
Ok I have this wall of code below I use for applications
It uses a button to start the application (asking questions)
and after the application is filled in it sends that application to a specified applicationChannelId
is there any way I could add buttons to the application that was sent to the applicationChannelId to accept or deny members?
Accept would add a role by id and sends a message to the original applicant
//Not the code I tried using just an example for what the Accept button would do
//
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
member.send("You have been accepted into the team")
Deny would send a message to the original applicant
I have tried doing this for the past few days but just can't get it to work it either does nothing or breaks everything
I have removed some info from the code below to make it shorter
Code for my button I already use to start the application for an applicant
client.on("message", async (message) => {
const reqEmbed = {
color: 0xed5181,
title: 'Blind Spot Team Requirements',
url: 'link',
author: {
name: '',
icon_url: '',
url: '',
},
description: '',
thumbnail: {
url: '',
},
fields: [{
"name": `Read through these Requirements`,
"value": `- requirments will go here`,
}, ],
image: {
url: '',
},
footer: {
text: 'Blind Spot est 2019',
icon_url: 'link',
},
};
//
// Don't reply to bots
let admins = ['741483726688747541', '741483726688747541'];
if (message.content.startsWith(`#blindspot`)) {
message.delete();
const amount = message.content.split(" ")[1];
if (!admins.includes(message.author.id)) {
message.reply("You do not have permission to do that!");
return;
}
// Perform raw API request and send a message with a button,
// since it isn't supported natively in discord.js v12
client.api.channels(message.channel.id).messages.post({
data: {
embeds: [reqEmbed],
components: [{
type: 1,
components: [{
type: 2,
style: 4,
label: "Apply",
// Our button id, we can use that later to identify,
// that the user has clicked this specific button
custom_id: "send_application"
}]
}]
}
});
}
});
Rest of code that handles questions and sends application to applicationChannelId when complete
// Channel id where the application will be sent
const applicationChannelId = "652099170835890177";
// Our questions the bot will ask the user
const questions = ["These are the questions but have deleted them to make this shorter",];
// Function that will ask a GuildMember a question and returns a reply
async function askQuestion(member, question) {
const message = await member.send(question);
const reply = await message.channel.awaitMessages((m) => {
return m.author.id === member.id;
}, {
time: 5 * 60000,
max: 1
});
return reply.first();
}
client.ws.on("INTERACTION_CREATE", async (interaction) => {
// If component type is a button
if (interaction.data.component_type === 2) {
const guildId = interaction.guild_id;
const userId = interaction.member.user.id;
const buttonId = interaction.data.custom_id;
const member = client.guilds.resolve(guildId).member(userId);
if (buttonId == "send_application") {
// Reply to an interaction, so we don't get "This interaction failed" error
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: "I have started the application process in your DM's.",
flags: 64 // make the message ephemeral
}
}
});
try {
// Create our application, we will fill it later
const application = new MessageEmbed()
.setTitle("New Application")
.setDescription(`This application was submitted by ${member}/${member.user.tag}`)
.setFooter("If the #Username doesn't appear please go to general chat and scroll through the member list")
.setColor("#ED4245");
const cancel = () => member.send("Your application has been canceled.\n**If you would like to start your application again Click on the Apply button in** <#657393981851697153>");
// Ask the user if he wants to continue
const reply = await askQuestion(member, "Please fill in this form so we can proceed with your tryout.\n" + "**Type `yes` to continue or type `cancel` to cancel.**");
// If not cancel the process
if (reply.content.toLowerCase() != "yes") {
cancel();
return;
}
// Ask the user questions one by one and add them to application
for (const question of questions) {
const reply = await askQuestion(member, question);
// The user can cancel the process anytime he wants
if (reply.content.toLowerCase() == "cancel") {
cancel();
return;
}
application.addField(question, reply);
}
await askQuestion(member, "Would you like to submit your Application?\n" + "**Type `yes` to get your Application submitted and reviewed by Staff Members.**");
// If not cancel the process
if (reply.content.toLowerCase() != "yes") {
cancel();
return;
}
// Send the filled application to the application channel
client.channels.cache.get(applicationChannelId).send(application);
} catch {
// If the user took too long to respond an error will be thrown,
// we can handle that case here.
member.send("You took too long to respond or Something went wrong, Please contact a Staff member\n" + "The process was canceled.");
}
}
}
});
at this point I just feel like not even doing this and keep it as is because its driving me insane
You very well can! just send it as you send your normal buttons!
const {
MessageButton,
MessageActionRow
} = require("discord.js"),
const denybtn = new MessageButton()
.setStyle('DANGER')
.setEmoji('❌')
.setCustomId('deny')
const acceptbtn = new MessageButton()
.setStyle('SUCCESS')
.setEmoji('✔')
.setCustomId('accept')
client.channels.cache.get(applicationChannelId).send({
embeds: [application],
components: [new MessageActionRow().addComponents["acceptbtn", "denybtn"]]
});
const collector = msg.createMessageComponentCollector({
time: 3600000,
errors: ["time"],
});
await collector.on("collect", async (r) => {
if (r.user.id !== message.author.id)
return r.reply({
content: "You may not accept/ deny this application",
ephemeral: true,
});
if (r.customId === "acceptbtn") {
let teamRole = message.guild.roles.cache.find(role => role.id == "761996603434598460")
member.roles.add(teamRole)
member.send("You have been accepted into the team")
}
if (r.customId === "denybtn") {
member.send("You have been rejected");
}
});
NOTE: Please be mindful that since your question lacks the functions / method definitions you are using I have used discord.js v13's methods, you may update your discord.js version and the code will work as intended ALTHOUGH the component collector's functions have been directly copy pasted from your question and in some instances such as member#send member is not defined, so please take this as an example and I urge you to write the code instead of copy pasting directly!

I want to check if someone has permission to be kicked, but the answer is wrong

I use msg.member.hasPermission('KICK_MEMBERS'), but even if the author of the message doesn't have this permission, the answer is true instead of false.
const Discord = require("discord.js");
const client = new Discord.Client();
channel = '858963360909099068';
module.exports ={
name: 'kick',
description: 'kick people',
execute(msg, args) {
let impostor = msg.mentions.members.first();
const mod = msg.author;
let botResponse;
if(msg.member.hasPermission('KICK_MEMBERS')){
console.log('true');
if(!impostor){
botResponse = 'You need to tag the person that you want to kick !';
impostor = 'Not specified';
}
else{
botResponse = `${impostor} was kicked !`;
impostor.kick();
}
}
else{
console.log('false');
botResponse = 'You are not able to kick people !';
}
let embed = new Discord.MessageEmbed()
.setColor('#f9ae0e')
.setTitle('Kick')
.setDescription(botResponse)
.addFields(
{ name: "Impostor", value: impostor},
{ name: "Member", value: mod}
);
msg.channel.send(embed);
}
}
This is the error:
TypeError: msg.member.hasPermission is not a function
I can't see what's wrong here, can you help me solve it?
You should change msg.member.hasPermission('PERM') to msg.member.permissions.has('PERM')

Resources