I used presence update for my Discord bot so when me or my friends are getting live on Twitch, the bot would send a message in a channel and console log. But when we go live, the bot does the job, like, it sends the first message then it starts spamming. As you may expect, I don't have the knowledge in js as I am a beginner. So, can someone tell me how to fix this problem or if it can be fixed? Here is my code:
if (!newPresence.activities) return false;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
};
});
});
And here is the console.log spam:
Mini-Game#8953 is streaming at https://www.twitch.tv/murus.
Mini-Game#8953 is streaming at https://www.twitch.tv/murus.
Mini-Game#8953 is streaming at https://www.twitch.tv/murus.
Mini-Game#8953 is streaming at https://www.twitch.tv/murus.
Add a return after the console.log() inside the forEach
newPresence.activities.forEach((activity,idx,arr) => {
if (activity.type == "STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
return; //---> ADD this
};
});
Related
I'm trying to make a command when you mention the bot, it will send a message saying “Slash Command: /help”. I have tried to make it and I tried to find an answer but I can’t find anything that works for me.
Here is the code (I use the code in index.js):
client.on("messageCreate", (message) => {
if (message.author.bot) return false;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return false;
if (message.mentions.has(client.user.id)) {
message.channel.send("Hello there!");
}
});
I added the GatewayIntentBits.Guilds intent to my intents array. I don't get any errors, it just doesn’t work.
If you're only using the GatewayIntentBits.Guilds intent, you won't receive messages. You'll also need to add GatewayIntentBits.GuildMessages. It allows you to receive messages if the client/bot is mentioned.
If you also want to receive the message content when the bot is not mentioned, you'll also need to add GatewayIntentBits.MessageContent. However, if you only want to see if the bot is mentioned, adding GatewayIntentBits.GuildMessages to your intents array will be enough.
I recommend you use RegExp, it would be done like this:
client.on("messageCreate", (message) => {
if (message.author.bot) return;
if (message.content.includes("#here") || message.content.includes("#everyone") || message.type == "REPLY") return;
if (message.content.match(new RegExp(`^<#!?${client.user.id}>( |)$`))) {
message.channel.send("Hello there!");
}
});
The RegExp object is used for matching text with a pattern. So if it matches what you've typed (in this case the #bot ), send the message
I am building a discord bot where I will need to save a users message in a variable but I currently can not get message.content to come up in console.log as well as I can not get my bot to respond to if it reads a certain string in the discord channel.
var name = "Poker Bot";
var allIn = "PUSH ALL IN";
var fold = "FOLD";
var tripleBBRaise = "3 BB RERAISE"
let usersHand;
//starts the bot and sets activity to a funny quote. it also will give a command prompt notification that the bot is online
client.on("ready", () => {
console.log(`Bot is online: ${name} !`);
client.user.setActivity('Burning The Darn Casino Down');
});
//check discord chat to see if a user has posted.
client.on("messageCreate", message => {
/*test to see if the chat input works.if working the users message will be saved as a string and then written in the console*/
console.log(`The user has said: ${message} `);
//Users input will be a poker hand. If it is one of the 3 below then tell the user the given string
if (message.content.toLowerCase == '/AK' || message.content.toLowerCase == '/AA' || message.content.toLowerCase == '/KK'){
message.channel.send(`RECOMMENDED PLAY: ${allIn}`) + message.author
}
});
I answered majority of this on your original post in a comment since I saw your edit.
If not yet done, you must enable the Message Content intent on your bot on Discord Developer Portal - Applications (then select your Application, click the "Bot" button, scroll down to Priveleged Gateway Intents then turn on "Message Content", and, probably, depending on your plan, "Server Members")
If your bot ever reaches 100 servers, assuming it's public, you'll need to verify your bot to continue using intents.
I have got client and server socket.io code. Every message is being received except for one handler: when i send the following to the client:
socket.on('loginb', function(login) {
console.log(1);
var a = bedrijven.filter(function(e) {
return e.bedrijfsnaam == login.bnaam && e.pwd == login.pwd;
});
if (a[0] == null||a.length!=1||a==undefined) {
socket.emit('wow', false);
} else {
console.log(2,a);
socket.emit('wow', a[0]);
}
});
everything until "412" will be logged, but the message won't be received. This is how the listener looks at the client side:
socket.on('wow', function(login) {
console.log(26);
});
26 is never getting logged.
The strange thing is: the emitter and the listener are on the same place as other emitters and listeners i have, and work the same. However, this does not work while the rest does.
I also tried replacing socket.emit('wow', a[0]); by socket.emit('wow', 'string'); but no difference.
I found the answer thanks to #jfriend00's comment.
Indeed, the problem was that the client did not receive the socket message because the page refreshed after the submit. This was prevented by adding a return statement at the end of the submit function.
I am making a couple updates to my socket.io chat and I am trying to get audio every time a message is passed through. When I send the message I get Uncaught (in promise) DOMException
Here is the code that gets run when a message is passed through:
socket.on('new message', (data) => {
var messageE = document.createElement('div')
messageE.classList.add("well");
messageE.classList.add("msg");
messageE.innerHTML = '<strong>'+ data.user +'</strong>: ' + data.msg;
console.log(messageE)
chat.appendChild(messageE)
audio.play()
})
I may wont be able to respond till late afternoon.
A few seconds ago i got the same error but i fixed it. It is not the coolest solution but it works.
What i did telling the browser itself to play the audio and not directly from a socket.
Make a outside Variable(example socketCheck) that is on false. That will become true when the socket is called. The if will continue when it is on true on false it wont. When the sound got played turn the soundCheck on false again
Here is a example;
var soundCheck = false;
socket.on('new message', function(){
soundCheck = true;
});
if (soundCheck) {
sound.play();
sound = false;
}
So I recently decided to make a Discord bot, and had been going through some tutorials with no problem. However, when I tried to make a ping command (you know, the kind that says "Your ping is 137 ms"), I got numbers that didn't make sense.
Numbers such as -627 ms, 32 ms, 1001 ms, -10 ms, 238 ms. Basically anything and everything between -1000 and 1000.
Now correct me if I'm wrong, but I'm pretty sure that getting negative numbers means I'm sending the response before I even ask for it, and I'm almost certain I didn't write a precognitive bot :P
I spent some time online looking for a reason why I was getting strange ping numbers, but I came up with zilch. I thought to myself, "Hmmmm..... maybe I'm generating them wrong." So I looked at how other people generated their numbers with discord.js...
...aaaand I hit a wall. As far as I can tell, no one has ever tried generating ping numbers with discord.js. If they have, they haven't shared their precious findings online. At least, that's what it looks like to me. (if you have a link to such a tutorial or post, please send it to me)
I'm generating the numbers with ${Date.now() - message.createdTimestamp} This is the only way I have found to generate them, so if anyone has a better method, I would love to see it.
That snippet fits right in with the rest of my code (and yes, it is a very boring bot):
const Discord = require('discord.js');
const client = new Discord.Client();
const token = require('./token.json').token;
client.on('ready', () => {
console.log('Bot is up and running!');
});
var prefix = "?"
client.on('message', message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix + 'ping')) {
message.channel.sendMessage('Pong! Your ping is `' + `${Date.now() - message.createdTimestamp}` + ' ms`');
}
});
client.login(token);
If anybody could give me a reason why I'm getting negative numbers, or give me a better way to generate them, I would be extremely grateful. Thanks in advance.
The variation in ping you're experiencing is due to the variation between between your clock and the remote clock as well as the message id's used to calculate the timestamp not being all that accurate.
Before you jump to one meaning for ping you have to decide what you want to measure. If you're looking to measure the round trip time to send a message and get that same message back frond discord, you could potentially send a discord message, start a timer. then when the promise resolves check how much time has passed.
You could also use .ping as suggested in another answer for the heartbeat ping.
You could also ping the api endpoint though you would probably only be measuring your delay to cloudflare...
You may getting different results, because the Date.now is based on your computer's clock and message.createdTimestamp comes from Discord server (not your computer) clock.
The reason nobody's sharing the ping feature is because it's just reading a client object property client.ws.ping.
https://discord.js.org/#/docs/main/stable/class/WebSocketManager?scrollTo=ping
You can not get the actual ping of another user. But you can get pretty close.
You know that using the system's clock won't work for you because it's out of sync with the actual server.
Your bot can send a message and edit it after that. Then you have two timestamps:
var resMsg = await msg.channel.send('Ping is being appreciated... :bar_chart:');
resMsg.edit('Ping: ' + Math.round((resMsg.createdTimestamp - msg.createdTimestamp) - client.ws.ping))); //client.ping has been moved to the WebSocketManager under client.ws.ping
Subtracting the ping of the bot will remove it's latency to the API while submitting the message.
Hope I could help!
Try this:
client.on('message', message => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix + 'ping')) {
msg.channel.send({embed: {
color: 0x2ed32e,
fields: [{
name: "Pong",
value: "My Ping: " + Math.round(client.ws.ping) + ' ms' //client.ping has been moved to the WebSocketManager under client.ws.ping
}
],
}
})
}})
I hope this will help c:
client.on('messageCreate', message => {
if (message.content === `${config.PREFIX}ping`) {
message.channel.send("Calculating ...").then(m =>{
var ping = Math.round((m.createdTimestamp - client.ws.ping) - message.createdTimestamp ); //more closer to discord ping
setTimeout(() => {
m.edit(`**Your Ping Is: **${ping}ms`);
}, 3000);
});
}) //DISCORD.JS V13
I'm sorry that you're facing this issue with your Discord bot. I used your code to analyse the issue and have come up with this solution. You see how you have:
${Date.now() - message.createdTimestamp}
Well, all you have to do is swap around "Date.now()" and "message.createdTimestamp" around
What you will achieve from this is:
${message.createdTimestamp - Date.now()}
I tried this method, and it worked fine. It gave me the most accurate results.
Hope this answer helped you!