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.
Related
I am new to coding a discord bot with JS and I am not sure why I am getting an error when I am trying to make a collection for a library of other .js files. I am instead greeted with the image below:enter image description here
Broken code:
https://sourceb.in/x2EFkdb5sV
Line 14.
I tried defining Collections using another question on this site with similar problems:"Collection is not defined" when trying to create a discord bot .
It wasnt the answer I was looking for as it deviates from making a collection.
You must require the class Collection from discord.js to be able to use the Collection class.
At the beggining of your code, change const { Client, GatewayIntentBits } = require('discord.js'); to const { Client, GatewayIntentBits, Collection } = require('discord.js');
In addition, its Collection, not Collections (there is no s). So you will have to write new Collection(); instead of new Collections();.
I am yet to code this but actually struggling to understand how to do this
There is a private server with paid membership. I am a member. There are many channels on that server. I am particularly interested in a specific channel. Once someone posts a message in that channel, I want to immediately extract that message from a different program automatically and then process it in downstream systems. How to do that?
I know I'm a bit late to this question, but I'm going to tell you the secrets the Discord elites don't want you to know: This is completely doable, and many people do it. It's called a "self-bot".
Disclaimer: As the other answer stated, this is against ToS. You probably won't be caught, but I'd keep this on the down-low. Definitely don't go around telling this to everyone you meet.
Ok, now here's the actual coding part:
Discord.js, the most common library for discord bots in JavaScript, no longer supports self-bots, so you'll need to use an older version of discord.js. The official old versions have some unresolved bugs when used with modern discord, so I like to go with discord.js.v11.patch. Using this patched package will fix any weird bugs you get. Note: this is still discord.js v11, so if you need documentation, be sure to look at the v11 docs.
Ok, so after you run npm install discord.js.v11.patch (or npm install -g discord.js.v11.patch if you want to install it globally), you'll need to start writing code. Everything is basically the same as any old discord.js bot, but this is v11 so some stuff might be different. Here's some code to get you started. It should do everything you want it to do:
const discord = require('discord.js.v11.patch');
const client = new discord.Client();
const USER_TOKEN = 'XXXXXXXXXXXXXX'; // change this to your token
const CHANNEL_ID = 'XXXXXXXXXXXXXX'; // change this to the chanel you want to listen to.
client.on('ready', () => {
console.log('bot is running');
});
client.on('message', msg => {
if (msg.channel.id != CHANNEL_ID) return;
const message_text = msg.content;
console.log(message_text); // just an example
// send message_text somewhere to process it.
});
client.login(USER_TOKEN);
Now, all you need to do is change USER_TOKEN to your discord token and CHANNEL_ID to the ID of the channel you want to listen to.
To get your token, I recommend using this gist. It is safe and minimal. If you are worried about accessing your token, don't be. As long as you don't give it to anyone else you're fine.
To get the channel ID, simply turn on developer mode, then right-click on the channel you want to listen to. You should see a Copy ID menu button at the bottom of the context menu.
You can only do this if you have permissions to add an actual bot to that server, or have a friend that can do this for you. Using your own account as a bot would be possible, but is against discord ToS and would get your account banned.
I have a discord.js self-bot, and I'm trying to make it create a server, any help would be great.
I've tried messing around with client.user but I didn't find anything
Also if you cant create a server using discord.js (because its not meant for selfbots) im willing to use a external library (request, axios etc..)
I'm using discord.js-selfbot (discord.js v12)
client.on('message', message => {
if (message.content === '!createserver') {
// make server here
message.reply('Created server!')
}
})
While I don't condone self botting due to terms, but the answer is very easily findable on the documentation for discord.js, right here assuming it exactly copies v12.
The documentation for the endpoint is available here if the copied package does not work. Or just install a working version of it using the actual discord.js package, like npm i discord.js#12.5.3.
Just be careful with self botting, for obvious reasons.
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.
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*/})