As the title explain, i have a function on discord.js (v12) bot which takes care of welcoming users, and imposing a specific role on them, this:
client.on("guildMemberAdd", member => {
member.roles.add('604250195001081859');
member.guild.channels.cache.get("707323130523418686").send(`Hi ${member.user} blabla`);
console.log(member.user.id + ' is in da house');
});
Now, I have a problem: every time someone join server it's like this event is triggered many times over and over again, flooding the channel.
Obviously this does not happen if I set "client.once" but in that case the message is given only at the first access of a user, the second does not receive it anymore.
I've been looking everywhere for answers, but I seem to be the only run into this issues, I hope someone can help me.
p.s.
I want to clarify haven't other active instances of the bot, and that it is hosted on a small vultr's vps.
Ok after a bit i found the problem... if you run into this check in entire project to find this method call:
client.login(<token>)
If it's called within a keepalive function, or in any case in a function called several times after/ouside the server startup, it creates a new instance of the bot and each of them will respond to events, effectively creating a flood of messages (in case of chat messages, but any operation inside the function is repeated for the number of instances created).
In short, the login() method only needs to be called once to avoid creating new instances.
And Yes, You can throw me all the rotten tomatoes you want now.
This happens to me a lot go to the discord dev portal and generate a new token and paste the new token into your code. For some reason its running multiple instances of your bot at the same time and generating a new token should fix that issue. I know you said you dont have multiple instances running but you will not be able to see the instances, its a weird bug.
Related
I was trying to do a simulation of distribution center model using the tips from anylogic to our own, cause they work in similar ways but diferent products and warehouses, so the thing is when a truck came to our center we have to unload in a determinated area thats why we use the "Muelle" agent to determine all the parameters involved in this operation, and when we call this new recourse unit in the resource pool for the unload process the error appears
Code use in a resource pool:
I got the same problem y two resource pool's
Problems
Im relatively new in this program and dont know how to fix this cause for the java description, i tought the variable was already defined cause i name it and was giving every value for each parameter so i dont understand.
I attach the file so if someone requires and can inquire more to solve this ill appreciate it so much
https://drive.google.com/file/d/1uBQyLr_LyjN9J07y_J_44ha4CSskVP8y/view?usp=sharing
If Muelle is an agent, you cannot create/instantiate them like Java objects (i.e. new Muelle(...).
You must use the agent constructor that AnyLogic provides: For every agent population, you get add_myPopulation(...) and remove_MyPopulation(...) methods. You must use those.
So create an empty (?) agent population and use that. Then, call add_MyPopulationOfMuelle(...)
Check the help for more info on agent creation and populations
I am currently writing a custom support ticket tool for a Discord with over 70k members, which spawns one category and three child-channels when a moderator investigates a ticket/report.
When the moderator is finished with the report, the created category and channels self-clean and get removed again. Though in some cases, these channels are still visible for the moderator even though they are, in fact, deleted.
I presume this is a caching issue on the client-side as every time the solution seems to be to reload the Discord client (ctrl+r). I am therefore wondering if there is something I can do code-wise that would avoid such issue.
Moderation happens around the clock and I can see this getting both annoying and looks like bad design for the customer if these channels keep on growing exponentially over-time.
A snippet of the self-cleaning function can be found below:
module.exports.cleanChannels = async (client, guild_id, channel) => {
await client.guilds.cache.get(guild_id).channels.cache.get(channel.parentID).children.forEach(channel => channel.delete())
await client.guilds.cache.get(guild_id).channels.cache.get(channel.parentID).delete()
}
I will mark this question as resolved. I am confident that we are looking at a client-side caching issue which is not solvable through the Discord bot.
The only seemingly alternative is to take a wholly different approach. By using a continuous feed as opposed to spawning and deleting private channels, its possible to circumvent the caching issue presented above.
I have something wierd too because the api can read a channel but when I try to do something special with it like deleting it will result in a error.
This channel should be deleted but is still in the api for some reason and cannot delete this channel annymore... its very annoying to say the least.
There's a bit of someone else's code I am trying to add functionality to. It's using websockets to communicate with a server which I will most likely not be able to change (the server runs on a 3$ micro-controller...)
The pattern used, for instance when uploading data to the server, consists in using global variables, then sending a series of messages on the socket, as well as having an 'onmessage' which will handle the response. This seems clumsy, given that it assumes that there is only ever one socket call made at a time (I think the server guarantees that in fact). The messages sent by the server can be multiple, and even figuring out when the messages are finished is fiddly.
I am thinking of making things so that I have a better handle on things, mostly w.r.t. being able to know when the response has arrived (and finished), going to patterns like
function save_file(name, data, callback) {
}
And perhaps at some point I can even turn them into async functions.
So couple of ideas:
- is there some kind of identifier that I could find in the websocket object that might allow me to better string together request and response?
- short of that, what is the right pattern? I started using custom events, that allows me to much better tie the whole process, where I can supply a callback by attaching it to the event, but even doing removeEventListener is tricky because I need to keep reference to every single listener to make sure I can remove them later.
Any advice anyone?
I know these types of question come up fairly often, but I need help with a wait-like mechanism in JavaScript. I know setTimeout-based solutions are going to come up, but I'm not sure how to pull it off in my case.
I'm writing an API that uses a WebSocket internally. There's a connect() method that sets up the WebSocket, and I need to make it not return until after the WebSocket is set up. I'd like it to return a value for whether or not the connection was successful, but that's not the main problem.
The issue I'm hitting is that after a user calls connect(), they may call another method that relies on the WebSocket to be properly set up. If it's called too early, an error is thrown stating that the object is not usable.
My current solution is setting a "connected" flag when I've determined a successful connection and in each method checking for it in each method. If it's not connected, I add the method call to a queue that is ran through by the same code that sets the flag. This works, but it introduces that style of code all over my methods and also seems misleading from the user-perspective, since the call of those functions is deferred. Also, if there is other user code that relies on those calls being completed before it gets to them, it won't behave as expected.
I've been racking my brain with how to handle this case. The easiest solution is to just find a way to block returning from connect until after the WebSocket is set up, but that's not really the JavaScript way. The other option was to make them provide the rest of their code in a callback, but that seems like a weird thing to do in this case. Maybe I'm over-thinking it?
Edit: To better illustrate my problem, here's a example of what the user could do:
var client = new Client(options);
client.connect();
client.getServerStatus();
The getServerStatus() method would be using the WebSocket internally. If the WebSocket is not set up yet, the user will get that not usable error.
Todays Javascript does not really work like that unfortunately. In the future (ECMA6) there may be new language features that address this issue more directly. However for now you are stuck with the currently accepted method of handling asynchronous events, which is limited to callbacks. You may also want to explore 'promises' to handle 'callback hell' however you will need a library for this.
And yes it does seem strange to have callbacks everywhere, especially for someone new to web programming, however it is really the only way to go about it at this stage (assuming you want a cross-browser friendly solution).
"Wait" is almost the keyword you are looking for. Actually, it's yield that does this. See e.g. MDN's documentation.
There's a connect() method that sets up the WebSocket, and I need to make it not return until after the WebSocket is set up
That isn't going to happen unless you rewrite the javascript execution engine.
Either the code trying to send data will need to check the socket state (I'd go with encapsulating the socket in a object, supplying a method which sets a member variable on the open/close events and poll the state of that member variable from the external code). Alternatively you could add messages and call backs to a queue and process the queue when the socket connects.
Is there a way of checking the number of currently subscribed clients to a certain publish function? Problem is that I have different groups where every group has its own unique ghash.
When a user chooses to leave a group and enters a new one, this ghash changes and THE SAME publish function is subscribed, although with a different ghash of course.
So I am looking for a way to check how many clients are subscribed to each group/ghash at a time (at the server side). I've been fiddeling around all day with stuff like this but it does not work that well to be honest. I am also listening for the "unsub" event of sockets and all that but still ... this is all buggy as hell.
If some one's interested in my whole code, you can find it here! (I found it too long to paste it here into my post.)
I really hope someone can help! :-)
cheers, P
EDIT: Or in other words: Is there a way to count the number of clients currently connected to a sockjs websocket where all these websockets were called with the same params?
=========================================================================
EDIT 2:
New version: LINK
For some reason this is not working at all ... No inserts are made because the ghash provided to the subscription is NEVER equal to any of the actual socket subscriptions (--> see lin 20: ghash is never equal to ghash2). I just don't understand how this is possible? the whole subscription function is called each time the Session ghash changes. How can this var never be equal to the param submitted to the actual socket (submission)? (it's always also a ghash, but always a ghash of another group).
I am really lost here! :-(
I now see you are doing straight old node style socket.io programming. I've done similar things in node projects. This is maybe the real question. On the docs for Meteor they don't even use the word socket. Maybe someone else would get into that new question with you, but this question about tracking subscribers is answered by this answer.
I think meteor is a new world, and will handle such stuff for you, if you adapt to its way of thinking. For example, make a collection of messages, with a field for chatroom. Each client picks their chatroom, finds those messages.find({chatroom:'box5'}), and displays them. A new message automatically goes to every client that is listening to that chatroom. Let Meteor use sockets for you.
Answer to counting clients subscribed to something:
Pseudo code:
Make an object to hold the counts of each subscription signature
counts = {}
on signup, Make a string that represents the subscription uniquely, add it to your counting object.
counts['params as string'] += 1;
on signout
counts['params as string'] -= 1;
The logic to know when no one is still subscribed is this:
done = (0 == counts['params as string'] )
Apparently as I know as of now it is not possible to do this.
I did some research and tried many things but for some reason sometimes multiple websockets are opened for trasnfering the same data to the same client. --> counting the number of clients connected is impossible via this approach.
Just triggering an event when my ghash changes is also not good enough as a close of the browser window would not trigger it.
I think having a functionality to count the number of clients "viewing the same data changes" (can't think of a better way to put it) would be awesome. Maybe some meteor core dev can just put his/her 2 cents in here so we know if this is even possible at all.
I hope someone can come up with a solution at some point .. I can't! :(
My user-status package tracks the number of clients connected to a Meteor app by tracking the number of subscriptions to a global publish function. You may be able to draw some inspiration from it. It's not granular at the per-publication level, but you can certainly do the same thing for publications that you are interested in.
https://github.com/mizzao/meteor-user-status
The main points to note are
each open session will call the subscription (users may have more than one tab open)
each time a user logs in our out, the subscription will update
you can read the per-session id in the publish function
you can listen to the close event for the SockJS socket for browser tabs being closed, etc.
I don't think it would be too hard to do this for groups; I am doing the same thing for another project.