How to use discord.js .setSelfDeaf() in my code? - javascript

My join.js Code is Below :-
I want the bot to auto self deafen when ever it joins any Voice Channel.
module.exports = {
name: "join",
description: "Joins your voice channel",
category: "music",
execute(message, args) {
if (message.member.voice.channel) {
message.member.voice.channel.join().then((connections) => {
message.channel.send({
embed: {
color: message.client.messageEmbedData.color,
author: {
name: "✔️ Hey, i joined your voice channel",
},
timestamp: new Date(),
},
});
});
} else {
message.channel.send({
embed: {
color: message.client.messageEmbedData.color,
author: {
name: "❗ You are not in a voice channel",
},
timestamp: new Date(),
},
});
}
},
};
I tried many way but can't implement .setSelfDeaf(true) .
Someone please assist?

You can use VoiceState#setSelfDeaf to achieve that. It's pretty easy to do so.
if (message.guild.me.voice.channel) { // Checking if the bot is in a VoiceChannel.
message.guild.me.voice.setSelfDeaf(true); // Using setSelfDeaf to self-deafen the bot.
};
module.exports = {
name: "join",
description: "Joins your voice channel",
category: "music",
execute(message, args) {
if (message.member.voice.channel) {
message.member.voice.channel.join().then((connections) => {
message.guild.me.voice.setSelfDeaf(true);
message.channel.send({
embed: {
color: message.client.messageEmbedData.color,
author: {
name: "✔️ Hey, i joined your voice channel",
},
timestamp: new Date(),
},
});
});
} else {
message.channel.send({
embed: {
color: message.client.messageEmbedData.color,
author: {
name: "❗ You are not in a voice channel",
},
timestamp: new Date(),
},
});
}
},
};

Related

embed update - slash command

I have the following code that creates a slash command. I want the embed to be updated every 10 seconds.
const embed = new EmbedBuilder()
.setAuthor({ name: track.title, iconURL: client.user.displayAvatarURL({ size: 1024, dynamic: true }) })
.setThumbnail(track.thumbnail)
.addFields(
{ name: "**volume**", value: `**${queue.volume}**` },
{ name: "**اtime**", value: `**${trackDuration}**` },
{ name: "**song**", value: `**${progress}**` },
{ name: "**repeat mode**", value: `**${methods[queue.repeatMode]}**` },
{ name: "**track**", value: `**${track.requestedBy}**` }
)
.setFooter({ text: inter.user.username, iconURL: inter.member.avatarURL({ dynamic: true }) })
.setColor("ff0000")
.setTimestamp();
I tried with setInterval but it didn't work.
You really need to add more information, but you could use setInterval().
I am just taking a guess on how you use your bot, interaction.editReply() is my guess that you send the embed as a reply, but you can change it to whatever you want, and the code within the setInterval will run once every 10 seconds.
EDIT: You also need to send the initial reply if you wish to edit it.
e.g.
const embed = new EmbedBuilder()
.setAuthor({ name: track.title, iconURL: client.user.displayAvatarURL({ size: 1024, dynamic: true }) })
.setThumbnail(track.thumbnail)
.addFields(
{ name: '**volume**', value: `**${queue.volume}**` },
{ name: '**اtime**', value: `**${trackDuration}**` },
{ name: '**song**', value: `**${progress}**` },
{ name: '**repeat mode**', value: `**${methods[queue.repeatMode]}**` },
{ name: '**track**', value: `**${track.requestedBy}**` }
)
.setFooter({ text: inter.user.username, iconURL: inter.member.avatarURL({ dynamic: true }) })
.setColor('ff0000')
.setTimestamp();
// You need to send the initial embed if you wish to edit it
interaction.reply({ embeds: [embed] });
setInterval(() => {
// Code to run every 10 seconds:
interaction.editReply({ embeds: [embed] });
// 10 seconds in milliseconds
}, 10000);

I get error while making a status command in discord.js

I was making a status command for my discord bot, that returns title, description, and some fields.
I ran into an error. I am using discord.js v13 and Node.js v16.14.2.
My code:
const { MessageEmbed, CommandInteraction, Client, version } = require("discord.js");
const { connection } = require("mongoose");
const os = require("os");
const quick = require('quick.db');
module.exports = {
name: "status",
asliases: [],
permissions: [],
description: "Displays the status of the client",
async execute(message, args, cmd, client, Discord, profileData) {
const messagePing = Date.now();
const endMessagePing = Date.now() - messagePing;
const status = [
"Disconnected",
"Connected",
"Connecting",
"Disconnecting"
];
const embed = new MessageEmbed()
.setColor("RANDOM")
.setTitle(`🌟 PDM Bot Status`)
.setThumbnail('https://imgur.com/IDhLmjc')
.setDescription("in the Skys with PDM Bot")
.addFields(
{ name: "🧠 Client", value: client.tag, inline: true },
{ name: "📆 Created", value: `<t:${parseInt(client.createdTimestamp / 1000, 10)}:R>`, inline: true },
{ name: "✅ Verified", value: "No, Please help us to get Verified :)", inline: true },
{ name: "👩🏻‍💻 Owner", value: 'Pooyan#9627', inline: true },
{ name: "📚 Database", value: 'Connected', inline: true },
{ name: "⏰ Up Since", value: `<t:${parseInt(client.readyTimestamp / 1000, 10)}:R>`, inline: true },
{ name: "🏓 Ping", value: `${endMessagePing}ms , for more information, use -ping command`, inline: true },
{ name: "📃 Commands", value: `45`, inline: true },
{ name: "🏨 Servers", value: `${client.guilds.cache.size}`, inline: true },
{ name: "👦 Users", value: `${client.guilds.cache.memberCont}`, inline: true },
);
message.channel.send({ embeds: [embed], ephemeral: true });
}
}
The error that I ran into because of the embed:
RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values must be non-empty strings.
at Function.verifyString (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\util\Util.js:416:41)
at Function.normalizeField (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\structures\MessageEmbed.js:544:19)
at C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\structures\MessageEmbed.js:565:14
at Array.map (<anonymous>)
at Function.normalizeFields (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\structures\MessageEmbed.js:564:8)
at MessageEmbed.addFields (C:\Users\Pooyan\Desktop\PDM Bot Main\node_modules\discord.js\src\structures\MessageEmbed.js:328:42)
at Object.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\commands\status.js:26:14)
at module.exports (C:\Users\Pooyan\Desktop\PDM Bot Main\events\guild\message.js:104:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
[Symbol(code)]: 'EMBED_FIELD_VALUE'
}
I read one or two similar questions/posts but it did not help.
Multiple properties here don't exist, including:
client.tag
client.createdTimestamp
client.guilds.cache.memberCount
Here are the things you should replace them with:
// client.tag
client.user.tag
// client.createdTimestamp
client.user.createdTimestamp
// client.guilds.cache.memberCount
client.guilds.cache.reduce((acc, g) => acc + g.memberCount, 0)

Guild is showing undefined discord.js

So I am building my bot using discord.js (v13).
Here is my code:
const { MessageEmbed } = require('discord.js')
const { SlashCommandBuilder } = require("#discordjs/builders")
const guild = require('../config.json');
module.exports = {
data: new SlashCommandBuilder()
.setName('server')
.setDescription('Display info about this server.'),
async execute(interaction) {
const exampleEmbed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`Server infomation on ${guild.name}`)
.setDescription('Tells Server Info')
.addFields(
{ name: 'Server Name', value: `${guild.name}` },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field titlve', alue: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.setTimestamp()
await interaction.reply({embeds: [exampleEmbed]}) ;
},
};
the above is an example of a slash command with an embed. In my embed, I am trying to print the guild name. Using guild.name. I don't get any error in the terminal but when I run the code in my discord server my bot shows undefined.
What am I doing wrong here and how do I fix it?
Edit: My Config.json:-
{
"token": "Test Test",
"clientId": "Client Id",
"guildId": "Guild Id"
}
P.S. I am kinda new to discord.js and javascript
The problem is that the variable guild is the content of the config.json file. If this file contains an object with the keys guildID and clientID only, guild.name will be undefined.
I think you want to use the guild where the interaction is coming from, in this case, it's interaction.guild:
async execute(interaction) {
const exampleEmbed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`Server infomation on ${interaction.guild.name}`)
.setDescription('Tells Server Info')
.addFields(
{ name: 'Server Name', value: `${interaction.guild.name}` },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.setTimestamp();
await interaction.reply({ embeds: [exampleEmbed] });
}

How can I stop a function from executing?

So I basically want to create a countdown for my discord ticket bot. If someone types in -close[...] the channel will be deleted after 10 seconds. But if the person who did this command types something in the channel the countdown will stop and the channel won't be deleted.
That works fine so far. But if I abort the countdown on every other message that I send to the channel the embed will be sent where it says "Countdown stopped" also if I type -close [...] again this message pops up but the channel will still be deleted after 10 seconds.
function closeTicket (_ticketid, channel, deleter, reason) {
var timer = setTimeout(function() {
channel.delete();
}, 10000);
channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: ``,
description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
fields: [{
name: "Thank you!",
value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
},
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}
});
logTicketClosed(_ticketid, deleter, reason);
client.on('message', message => {
if(message.channel === channel && message.author === deleter && timer != null) {
clearTimeout(timer);
timer = null;
message.channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: `Timer Stopped`,
description: "The timer has been stopped, the ticket will remain open.",
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}});
}
});
return 0;
}
I got it to work now! I defined a new variable called timer_running which would be set to true when the timer starts and to false when it stops. That way I got it to work now.
function closeTicket (_ticketid, channel, deleter, reason) {
var timer_running = false;
var timer = setTimeout(function() {
channel.delete();
}, 10000);
timer_running = true;
channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: ``,
description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
fields: [{
name: "Thank you!",
value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
},
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}
});
logTicketClosed(_ticketid, deleter, reason);
client.on('message', message => {
if(message.channel === channel && message.author === deleter && timer_running === true) {
clearTimeout(timer);
timer_running = false;
message.channel.send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: `Timer Stopped`,
description: "The timer has been stopped, the ticket will remain open.",
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "Support Ticket System © H4rry#6701"
}
}});
}
});
}

Check if a reacting user has a role

So, I was making a platform for a suggestion channel. The idea was that there is a voting, and when it reaches a such cap, it auto accepts or rejects. I was also making staff can approve or reject. Well, i had a problem in this.
An image of this platform (I censored out the nicknames):
The image
When it reaches the approve cap (10 white check marks), it is supposed to delete the embed and post a new one meaning that the suggestion is approved.
Reproducible sample of code:
client.on("messageReactionAdd", function (messageReaction, member) {
if (member.bot) return;
if (messageReaction.message.channel.id == "516263179446124555") {
if (messageReaction.emoji.name == "✅") {
if (messageReaction.count >= 10) {
messageReaction.message.channel.send("", {embed: {
title: "Suggestion Approved",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 3394611,
footer: {
text: "Why: The message got 10 ✅ reactions."
}
}})
}
messageReaction.message.delete();
}
if (messageReaction.emoji.name == "516258169035554817") {
if (messageReaction.count >= 8) {
messageReaction.message.channel.send("", {embed: {
title: "Suggestion sent to Staff",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 16764006,
footer: {
text: "Why: The message got 8 <:neutral:516258169035554817> reactions."
}
}})
}
messageReaction.message.guild.channels.get("517331518843125760").send("", {embed: {
title: "Suggestion to check",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 16764006,
footer: {
text: "Approving/disapproving this won't change the embed in <#516263179446124555>."
}
}})
messageReaction.message.delete();
}
if (messageReaction.emoji.name == "516258587845328906") {
if (messageReaction.count >= 7) {
messageReaction.message.channel.send("", {embed: {
title: "Suggestion Rejected",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 16724736,
footer: {
text: "Why: The message got 7 <:bad:516258587845328906> reactions."
}
}})
}
}
if (messageReaction.emoji.name == "☑") {
var staffMemberReacted = false;
messageReaction.message.guild.members.forEach(function(GuildMember) {
if (messageReaction.users.keyArray().includes(GuildMember.user) && (GuildMember.roles.has("501752627709870080") || GuildMember.roles.has("493436150019784704"))) {
staffMemberReacted = true;
}
})
console.log("reached manapprove")
if (staffMemberReacted) {
messageReaction.message.channel.send("", {embed: {
title: "Suggestion Approved",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 3394611,
footer: {
text: "Why: A owner or co-owner manually approved it."
}
}})
messageReaction.message.delete();
}
}
if (messageReaction.emoji.name == "517327626373824522") {
var staffMemberReacted = false;
messageReaction.message.guild.members.forEach(function(GuildMember) {
if (messageReaction.users.keyArray().includes(GuildMember.user) && (GuildMember.id || GuildMember.roles.find)) {
staffMemberReacted = true;
}
})
if (staffMemberReacted) {
messageReaction.message.channel.send("", {embed: {
title: "Suggestion Rejected",
description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
author: {
name: messageReaction.message.embeds[0].author.name,
icon_url: messageReaction.message.embeds[0].author.iconURL
},
color: 16724736,
footer: {
text: "Why: A owner or co-owner manually rejected it."
}
}})
messageReaction.message.delete();
}
}
}
})
When adding a console.log of it reaching, heroku logs don't put out anything. I was looking for the problem for over a hour.
Please never do this.
Discord.js has a guide on what to do in this situation https://discordjs.guide/#/popular-topics/reactions?id=awaiting-reactions
I would follow their example and use message.awaitReactions To sum it up, use the filter to set the possible reactions you will consider, makes everything much easier.
This is from that link.
message.react('👍').then(() => message.react('👎'));
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.reply('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});

Categories