Check if you can’t change channel name - discord.js

I have a bot command to change the channel name. I know that you can only change the channel name through discord.js 2 times every 10 minutes. I want to send a message when it”s rate limited, but every time I do the command it doesn’t send anything.
message.channel.setName("Name").then(() => message.reply("Changed name")).catch(() => message.reply("I am being rate limited"))

To detect a ratelimit you can use the Client#rateLimit event which returns a RateLimitData object.
client.on('rateLimit', data => {
console.error('Rate Limit Hit!');
console.log(data);
});

Related

Discord js getting a bot's message

Well i need to do this:
on a channel a bot announces "btc" price (not real)
im trying to get the price and send the price to a specificed channel
My code
sa.on("message", (message) => {
if (message.content.startsWith("🟠")) {
if (message.author.id == "974187708933083157") {
client.channels.get('955536998989447249').send(`${message.content}`);
} else message.channel.send(``);
}
})
So if a message that starts with 🟠 the bot needs to get the price and send it to the channel (955536998989447249)
But it not working my bot is working fine but not getting price and sendimg it
Firstly don't try sending empty message message.channel.send(``);, it will just throw an error.
The main problem, is that client.channels (ChannelManager) doesn't have a "get" method.
Assuming you are using djs v13, to get a channel, you can get it from cache with:
client.channels.cache.get('955536998989447249').send("sth")
however this might not always work if the channel is not currently cached.
Other option is to fetch it, though it is an asynchronous operation, so it looks a bit different:
client.channels.fetch('955536998989447249').then(channel => channel.send("sth"))

Avoid rate limit for changing voice channel name discord js 13

I'm trying to create a slash command using discord.js v13 to change the names of voice channels. I am using this code to do this :
module.exports = {
data: new SlashCommandBuilder()
.setName('name')
.setDescription('Set name for your voice channel')
.addStringOption(option => option.setName('name').setDescription('Enter your name').setRequired(true)),
async execute(interaction) {
const name = interaction.options.getString('name');
if (!interaction.member.voice.channel) await interaction.reply('Error not in a voice channel!');
else {
await interaction.member.voice.channel.setName(name);
await interaction.reply('Done!');
}
},
};
This code is fine and makes the job done. But as you know I can change the voice channel's name only 2 times per 10 minutes because of the limit rate. So if a user tries to change the voice channel's name for the third time, I won't get any error on the console, and discord js will queue this request for later and will do it after 10 minutes. But the user gets this error on discord: This interaction failed.
I want to check if there was a rate limit for my request, and if is, don't send the request and just reply to the user. Is this possible?
There is no inherent functionality that is able to handle the situation in the way you want it to, but the problem is soluble using regular old JavaScript. For example, you could use an integer to indicate how many times the command has been used and use setTimeout() to decrement it 10 minutes after the command was called. That way you can check if the int is equal to 2 in which case you skip the .setName().
There are undoubtedly other ways to implement the same or similar behavior, but, to answer your question, unfortunately the discordjs/voice library does not provide any simple way to do it.

Trying to delete all messages sent by a user with a certain role unless the message is !verify

I'm trying to make a verification system. Members are supposed to type !verify. If they don't type that, I want my bot to delete the sent message.
module.exports = {
client.on('message', (message) => {
if (message.member.roles.cache.has('793205873949278279')){
if (!message.content === '!verify') {
message.delete({ timeout: 1 });
}
}
})
}
'message.member.roles' returns an array of role objects the message author has.
If you'd like to find out whether the user has the corresponding role or not, you would first want to get the role using its ID (or name if preferred) and only then check if the user has it.
const role = message.guild.roles.cache.get('role id here') // Gets the role object using it's ID
if (!message.member.roles.cache.has(role)) return // The command will not be executed if the user does not have the corresponding role
One more thing to note: It seems like you're trying to delete the message after one millisecond - Which let's be honest is quite useless since you'll never notice the difference if it's 0 or 1 milliseconds - So my advice is to either delete it after 1000 milliseconds (1 second) if that's what you wanted to do, or not set a timeout at all.

When a member joins a server, i want to ping them then delete it (JS)

I would like to ping a user in a channel (to alert them to it) then delete the message.
I have seen this on many large discord servers, they use custom bots for it so I think it wouldn't be too hard!
Inside your guildMemberAdd event get, the channel you want to send the ping in, then do channel.send(`${member.user}`).
Here, member is the argument you gave to the callback function in the event. member.user will ping them in that channel.
send() method returns the message you sent as a promise, which means you can just catch that message and delete it like this: send().then(message => message.delete()).
You can provide timeout as an optional parameter to the delete() method if you want to delete the message after a specific period of time and not instantly. This is what the whole code will look like:
bot.on('guildMemberAdd', (member) => {
const channel = member.guild.channels.cache.get('id'); // get the channel using the id
channel.send(`${member.user}`)
.then(message => message.delete());
}

how to send a message to every channel in every guild?

similar to this question however I want to be able to send it to every channel it has access to!
inside the on message event after I verify myself by ID and the issued command I was using this code:
const listedChannels = [];
msg.guild.channels.forEach(channel => {
//get all channels
client.channels.get(channel.id).send("you like bred? (message) ");
//send a message to every channel in this guild
});
however I get the error that .send is not a function...
I have been told to use .send after getting the ID of the channels
If you are looping through all of the channels, you simpily need to send your content to the channel, which you've already gotten from msg.guild.channels.forEach(channel => {//code}).
Replace what you have inside the .forEach block with;
channel.send("You like bred? (message)");
Although this will send You like bred? (message)
If you're trying to get a response back, perhaps look at this answer that explains collecting responses via reactions to a discord message.
The following explanation pertains only to v11 (stable).
Client.channels is a Collection of Channels your bot is watching. You can only send messages to text channels, and this Collection will include DM channels as well. For this reason, we can use Collection.filter() to retrieve a new Collection of only text channels within a guild. Finally, you can iterate over the channels and call TextChannel.send() on each. Because you're dealing with Promises, I'd recommend a Promise.all()/Collection.map() combination (see hyperlinked documentation).
For example...
// assuming "client" is your Discord Bot
const channels = client.channels.filter(c => c.guild && c.type === 'text');
Promise.all(channels.map(c => c.send('Hello, world!')))
.then(msgs => console.log(`${msgs.length} successfully sent.`))
.catch(console.error);
You can use client.channels for this. Check if channel type is guild text channel and then try send a message.
client.channels.forEach(channel => {
if(channel.type === 'text') channel.send('MSG').catch(console.error)
})

Resources