I am trying to shard my bot in discord.js. Client.js is my bot.js file
I have this code in my index.js file to shard
const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./structures/Client.js', {
token: "token",
})
manager.on('shardCreate', shard => console.log(`Launched shard ${shard.id}`));
manager.spawn();
But I keep getting this error:
(node:27636) UnhandledPromiseRejectionWarning: Error [SHARDING_READY_DIED]: Shard 0's process exited before its Client became ready.
I need some help on sharding it properly
There are a couple problems I would like to address that might be causing your issue
Your bot is attempting to be ran before it receives the ready event (#MrMythical)
The file is being recognized as a Class and not a script (#Logan Devine)
Running before the "ready" event
When your bot is receiving data from Discord and starting up, it's not ready to start executing code. Discord has to do stuff on their end to make sure that you receive the correct data you're supposed to receive. That's why the ready event was created. If your bot tries to execute code before the ready event is emitted, it will exit. Likely, this is what is occcuring with your bot. It's trying to run code before Discord has sent the ready event.
To fix this, it's fairly simple. Just place this in your Client.js file and the bot will listen for the ready event
// Replace <client> with whatever variable your Client is
<client>.on("ready", async () => {
console.log("Online!")
})
Recognizing as a Class
You've named the file with a capital letter. This is typically done when creating a Class. However, you're trying to run a script. To fix this, just rename your file with a lowercase letter. If you want to keep the "Client" name on it, just change the uppercase "C" to a lowercase "c". This should resolve the issue
Other Issues
There is one last issue I would like to address. You might be referencing a file that has bad code. Ensure that the Client.js inside the structures folder has the correct code because you may be accessing the wrong file.
Related
I have tried using the client.on('guildDelete', () => {remove roles and other things}) , but everytime the bot is removed from my test server it says "Missing Access" (even though my bot has adminstrative privelages on my test server, and all other features work). I am assuming this is because the event is registered after the bot is removed from the server, which would make sense and is why I am tring to find away around that. Any and all help is appreciated.
Full source code: https://github.com/blb7103/sw-bot
Precisely what you said -- the event fires when the bot has officially left the server, and therefore cannot access/edit anything within that server.
There are no workarounds to this.
So I'm trying to have my bot restart ENTIRELY on encountering an error. The reason why I don't just let it sift through connection errors is because, whenever I encounter an internet issue, code starts repeating multiple times since the original node process hasn't been terminated, which technically I could fix but other connections to external apis stop working too. So ignoring fixing singular issues, I just want to restart entirely.
What I'm doing currently is using node child_process, with this function:
spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit"
});
process.exit();
I do know stdio inherit does nothing, since its exiting the parent process, but it doesn't really change anything to put it to ignore so i've just left it. Basically this works in theory, if I use a command to execute this, i can do it over and over and over and it will work fine, singular discord client, no repeats, it's up, i just can't monitor it since my original terminal is disconnected, and I can use a command to exit the current process so it's not stuck since I don't have a terminal to ctrl-c. But once put in practice, executing the function in bot.on("error") by disconnecting my internet seems to work, it ends the first process, but upon regaining internet there is no client connected.
My guess here: bot.on("error") will not be re-executed in the next process due to no discord client being made.
So I don't know if I'm making this too complicated or if I need to add a lot more. If this is the best way to do it then all I would need to solve is to wait until I have internet back and then make a new process or something like that. I'm not educated in fiddling with node so if any answers could be beginner friendly (mainly for node) i'd really appreciate it.
bot.on("error", (err) => {
process.exit(0)
});
Should work, it'll restart the bot when there's an error.
Unsure what you mean by
My guess here: bot.on("error") will not be re-executed in the next process due to no discord client being made.
As long as you bot it in the same code as your startup, it'll restart the bot.
If you use a batch-file to run your bot simply add :a before the node . and goto a at the end.
I'm using the JavaScript version of the botframework. I've followed the documentation to enable telemetry logging in Application Insights. When I access the logs I can see that custom events are being logged.
The issue is that the bot specific identifiers, such as user_Id, session_Id and conversation_Id are not being logged. This can be seen in the screen capture below
In the applicationInsightsTelemetryClient.js file there is a function called addBotIdentifiers. As far as I can tell, it is this function that is responsible for adding the bot specific identifiers.
The first lines of the function look like this:
function addBotIdentifiers(envelope, context) {
if (context.correlationContext && context.correlationContext.activity) {
Inspecting this function shows that the context argument is always null.
This leads me to my questions.
Why is it null?
Any suggestions on what I need to do to have it set appropriately?
Update
In digging into this further it appears the code starting at line 26 in the applicationInsightsTelemetryClient.js file isn't being called. Could this be the cause of the missing context later on in the addBotIdentifiers function?
Looks like the documentation has a missing line in step 7. We will correct the document ASAP. Meanwhile, please add the below in your index.js following https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/javascript_nodejs/21.corebot-app-insights/index.js#L113
// Enable the Application Insights middleware, which helps correlate all activity
// based on the incoming request.
server.use(restify.plugins.bodyParser());
Further investigation has shown what the issue was. It's not immediately obvious.
I compared the code in my index.js file with the one in the 21.corebot-app-insights BotBuilder sample.
Note that the setup of the Restify server happens after the creation of the adapter bot adapter. It is also after the configuration of the main dialog and the middleware.
In my code the setup of the Restify server and the bot adatper / dialogs was intermingled. This appears to have been the cause of the problem.
The main lesson here for me, and for anyone who stumbles across this post later, is that the setup of the Restify server should be at the end of the index.js file. To ensure all of the bot framework is setup first.
I am trying to make a discord bot. However, i am stuck on a part where i have to check every hour if a discord username is equal to the contents of an array. If it is, I have to make the bot remove a role. However, doing so would require using the discord.js module, which I don't know how to add into the code (I already have the bot set up in index.js).
I've tried to require discord.js-commando but it returns:
"Error: A client must be specified"
This is the place of the problem:
class checkDet extends commando.Command {
This is where I want to use the discord.js module:
var guild = bot.guilds.get("GUILD ID HERE");
for (let [k, v] in Object.entries(guild.members)) {
if (v.user.username == userName)
{
v.removeRoles(v.roles).then(console.log).catch(console.error);
}
}
Multiple solutions. Solution 1: if you're just using Discord.JS and not the whole Commando module, try using const Discord = require('discord.js'); at the top of that file and use
it as normal!
Another method would be to pass the whole commando module through. There's many ways to do it, however the easiest is probably to add let commando; at the top of the file, then create a module.exports.define function to set commando to the first argument. Then in the main bot file, run require('path/to/my/command.js').define(commando);.
Or even simpler, if all you need is a recursive check, why not put it in the main bot.js file, or even another file? Either invoke the file including the commando object, or just run a setInterval in the original file with the normal code.
I want to check the time difference between when a command is executed and when the parameter specified in the command states it should end. I have an idea of how to do this, but if i merely stick a loop in my index.js which is the main file that I have the command group names stored will the loop continue to run without it stopping the bot from receiving input from people typing a command in a discord channel (like !flip)? How would I go about making it so it won't stop the other input if it does?
For this you will need to make use of async as otherwise node will wait for the loop to finish.
Try putting
<client>.on('message', async message => { /*code*/})