I was wondering if there is a way to block the sending of multiple messages at the same time from different people in a channel discord. Whether his silk with discord.js or discord.py. In itself, I want the bot to take into account the first message sent and ignore others sent at the same time
cordially,
Quentin S.
One solution would be to track the timestamp of the last message listened to, and then ignore any messages sent before a set amount of time has elapsed. See this example...
let lastMessageTime;
const sec = 2; // Seconds to wait between messages.
client.on('message', message => {
if (!message.guild || message.author.bot) return;
if (lastMessageTime && message.createdTimestamp - (sec * 1000) < lastMessageTime) return;
else lastMessageTime = message.createdTimestamp;
// Continue with 'message' event.
});
Related
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);
});
So I'm trying to make the bot send a message at a specific time (Hour, Minute, Second), at a specific channel. However, its really not doing what its programmed to do.
Right now, the time set is 15:25:00, however, its not sending the message on that time.
client.on("message", message => {
var day = new Date();
var hr = day.getHours();
var min = day.getMinutes();
var sec = day.getSeconds();
if (hr == 15) {
if (min == 25){
if (sec == 0) {
client.channels.get("704015571531857950").send("Test")
}
}
}
});
Always look at the parent function :) Your if conditions are in client.on("message", message => { ... }), meaning you only check the time when someone sends a message, only allowing the bot to send the message if someone sends a message exactly at 15:25:00. Also, to send a message to a specific channel, your method is outdated, here is the new solution:
client.channels.fetch("704015571531857950").then((channel) => {
channel.send("Test");
});
setInterval every 10 seconds is bad for prefromance, I would suggest doing some math and using setTimeout, although setTimeout might be a bit off, don't think it would be too much.
like vrecusko said your method is outdated, but using fetch isn't the only/best option.
client.channels.cache.get("704015571531857950").then(channel => {
channel.send("Message");
});
Can you explain what you are doing further? Is there a command where you set the time to send a message? Or is this like a message you want the bot to send every day at the same time?
javascript is still vague to my knowledge, and I cannot be precise on the sensibility in the following code I have manufactured. I am programming a discord bot that categorizes messages into a variety of channels, and the question has approached me to how I can program the bot by deleting follow up bot messages after 2 user messages were sent subsequently after the bot message.
if (bot.on(client, user.msg = '2'));{
msg.channel('message' = msg.delete)
}
Help is to be requested and I hope my query makes sense in the context I am implying, Regards, Coder
P.S: How do I change the bot messages text color as well?
No offense, but your code was completely faulty, so I just made some new code.
bot.on('message', aysnc message => {
var messagenumber = 0; //Makes variable for the number of messages sent
if(message.member.user.username === bot.user.username) return; //If the person who posts a message is the bot, it doesn't the code below.
if(messagenumber = 1) return bulkDelete(100), messagenumber = 0 //Checks if the variable is 1, and if it is, this means it's the second time a message posted, so it deletes messages (Limit is 100) then resets variable.
messagenumber = 1; //Makes variable 1 (This is to make it know it is the the second time a message is posted for the next time).
});
I didn't test this code, so it may not work properly, but if it does work I hope it helps you with your bot.
For the P.S (Changing text color): You can use these (Replace "NoKeyWordsHere" with your message). I don't recommend this, cause it'd take a long time to add it to every message sent. (You can use these on your messages too)
In the Discord bot I'm creating, I'd like to make it so that the bot waits lets say 60 seconds before sending another reply to the same command or "includes" word. For instance, if someone says ping below, I want the bot to wait a minute until the next one is sent. I do not want the bot to hold it and put it into a queue or anything of that sort, I just want it to ignore the command for a minute until the next one happens.
if (message.content.includes("ping")) {
message.reply("pong!");
}
If I understand your question correctly, you would want to make a boolean variable and use setTimeout.
The global function setTimeout waits for the provided amount of milliseconds, in this case 60000 (60 seconds), then changes the variable to true again so it can be run.
var pingEnabled = true;
if (ping command run && pingEnabled) {
reply("Pong!");
pingEnabled = false;
setTimeout(() => { pingEnabled = true }, 60000);
}
most of this is pseudocode and you may need to adapt it to your situation.
//function fires after special command every 100 milliseconds
function searchSpeaking(roleSpeak){
//checks every channel; giving you member-channel and its chanId-id
bot.channels.forEach((channel, chanId) => {
//filters out text channels
if (channel.type == 'voice'){
//checks every member in voice channel; giving you guildMember-user and his/her id
channel.members.forEach((guildMember, memberId) => {
//debug
console.log(guildMember.nickname, guildMember.speaking)
console.log("-----------------------------------")
//activates when user speaks DOESN'T WORK
if (guildMember.speaking){
//adds 'score' it means how mayn milli seconds user talks
scoreboard[memberId] += 100
console.log("user is speaking!")
//if user talks for over 30s = 30 000 milliseconds and has option to talk
if ((scoreboard[memberId] > 30 * 1000) && guildMember.roles.has(roleSpeak.id)){
//reset his score and remove his option to speak
scoreboard[memberId] = 0
guildMember.removeRole(roleSpeak)
}
}
//if user isnt talking because his option/role to talk was removed
else if (!(guildMember.roles.has(roleSpeak.id))){
//we messure how much time he/she has been muted/without talking option
scoreboard[memberId] += 100
//if he/she was muted for a minute he/she will get his option/role to talk back
if (scoreboard[id] > 60 * 1000){
//reset score and give back role
scoreboard[memberId] = 0
guildMember.addRole(roleSpeak)
}
}
})
}
})
}
So in the line under a comment ( // ) stating that this doesn't work is an if statement that never passes even tho users are speaking in channels. It is console.loging as false as well (obvious). So i can't figure out why as i understand the docs it should turn to true every time member speaks. I will just say this again (as it says in first code comment) this function fires every 100ms = 0.1s and gets a role it is supposed to change based on score further explained in code comments.
Thanks for any help working with .speaking!
With the current API, there is no way to know if a user is talking in a voice channel without joining the voice channel.
You would need to join a voice channel, get a list of the users that are talking, and then switch to another channel (if there are any members in that channel).
You would probably get rate limited very quickly using this method.
Only way without joining the channels, would be getting the list of members in voice channels, and filter those with microphone muted. You still wouldn't know if they are really talking.
If you would want to join the channel and "listen" to the speaking, you could use the event guildMemberSpeaking. This would activate whenever a user starts/stops talking in the voice channel you are.