Showing the number of folders in a given place - javascript

why is my code not working? I want to do something like that when the bot turns on I get a message to the console about the number of servers it is in.
const serversFolders = readdirSync(dirServers)
const servers = parseInt(serversFolders)
table.addRow(servers)
console.log(table.toString())

If you want to display the number of servers your bot is in simply do:
console.log(client.guilds.cache.size);

Related

I need help reassigning a variable in a bot menu

I am trying to build a setup menu for my bot that the server owner can use to configure the bot. The menu is triggered when the owner types =setup. I want the bot to reply with several embed messages asking the user questions in order to correctly configure the bot.
This is my first Discord.js project so I am unaware of the syntax but trying to learn. I have a constant variable called prefix assigned to = when the bot is implemented into the server.
The first prompt on the bot menu asks the user to change the prefix to anything they want. I need help understanding how to reassign my original constant variable to the new prefix they are requesting.
var PREFIX = '=';
bot.on('message', message=>{
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'setup':
const embed = new Discord.RichEmbed()
.setTitle('Step 1 of 1')
.setDescription('Set your Prefix')
.setColor(0xF1C40F)
message.channel.sendEmbed(embed);
//I want the user to now enter their own PREFIX and have the
//bot save their responce as the new PREFIX
break;
}
})
What I want to happen is when the user types their desired prefix, the bot will reassign prefix in the code, and delete the bots question and the users response and begin to prompt them with the next question.
depending if your bot is going to be in multiple servers with different prefixes:
If so then you need a database to save the prefix for each server and
then get it when a user sends a message from that server
If not I would use a json file to store the prefix, then have node
edit the file when it needs to change
Or look at https://discordjs.guide/keyv/ there is a great tutorial there to do what you want
You can't prompt the user to respond (from what I know), you'll need to wait for the user to write another message and analyze it.
A message is linked to a user, so when a user initiates the command to change the prefix, you want to make sure that the same user changed the prefix.
This untested but should be close to the solution you're looking for.
let prefix = '=';
let expectingResponseFrom = null;
bot.on('message', message=>{
// same user sent a response
if(expectingResponseFrom !== null && expectingResponseFrom === message.user.id){
expectingRepsonseFrom = null;
prefix = message.content.trim();
return;
}
const regex = new RegExp(`^${prefix}([^\s]+)`, 'g');
cosnt command = regex.exec(message.content)[1] || '';
switch(command){
case 'setup':
const embed = new Discord.RichEmbed()
.setTitle('Step 1 of 1')
.setDescription('Set your Prefix')
.setColor(0xF1C40F)
message.channel.sendEmbed(embed);
// memorize user who initiated a prefix change
expectingResponseFrom = message.user.id;
break;
}
})
The regex allows for a better (my opinion) way of getting the command
console.log(
(/^=([^\s]+)/g).exec("=hello should not get this"),
(/^=([^\s]+)/g).exec("="),
(/^=([^\s]+)/g).exec("= should not get this")
)
You should definitely use a database to store prefixes and other server settings, it's more efficient and stable than using a JSON file for example.
I suggest you use mongoose and/or just MongoDB if you're unsure as to what to use.

Javascript Unique Array for each Socket User

i have a Problem!
When two users playing a game and they click very near at the same time, all two users get what only the 2nd user must get.
Is it possible to make an array by his userid or something?
I also have for anti spam like that
anti_spam[socket.request.session.passport.user.id]
But thats a json
If i try the same or array, i get error SyntaxError: Unexpected token [
How can i make sure, that each user get only his own items and not items from another user when opening at the same time?
I use sockets, here is a unique userid socket.request.session.passport.user.id
This is the array
var WinItem = [];
And if two users start like milliseconds same time, than the second overwrite the first...
! CODE NOT TESTED!
Wait wait wait wait...
So I only know ws (Node.js) but there you first get a onConnection event.
In that function you add a UID to the connection element. Then if you receive a message from the client you should have you're connection object and therefore an UID. You now can store the won item in the connection object (if you want) maybe like so:
ws.onConnection = con => {
con.UID = generateUID();
con.inventory = [];
con.onMessage = msg => {
[..STUFF/CODE/YAY...]
con.inventory.push(item);
con.send("You've won wooohoo");
});
});
Did you mean something like this??
Otherwise please be more specific about what you want.
(You can also store the stuff somewhere else together with the UID but that would add quiet some code)

Return the channel ID of a channel mentioned in a command

The title says all. I'm making a Discord bot in node.js, and one part that I'm trying to add is a .setup command, to make the bot less dependent on manually changing the value of client.channels.get().send(), allowing it to be more easily set up in the future.
Anyway, in my case, I'm trying to have the user reply to a message with a channel mention (like #welcome for example), and have the bot return that ID and save it to a variable.
Currently, I have this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.mentions.channels.first().id;
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
});
}
message.mentions.channels returns undefined.
I know that the message.mentions.channels.first().id bit works when its a user instead of a channel, is there a different way of getting a channel mention in a message?
Here is the most easy and understandable way i found :
message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))
But it not really what we want we could use <#####9874829874##><<<547372>> and it will work. So i figured out this :
message.guild.channels.cache.get(args[0].substring(2).substring(0,18))
I like to just remove all non-integers from the message, as this allows for both IDs and mentions to be used easily, and it will return the ID for you from that, as mentions are just IDs with <# and > around them. To do this you would use
message.content.replace(/\D/g,'')
With this, your code would look like this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.content.replace(/\D/g,'');
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
});
}
From this you will always get an ID. And we can surround the ID to trun it into a mention.

Can users themselves set a description for their RichEmbed

I'm developing a discord bot using discord.js. With my bot users have their own profiles. So they go !profile #me and their profiles come up. I'm wondering if there is a way for the user to use a command, say "!setDescription I like Discord". And when they use the command to view their profile "!profile #me" their profile embed will have the description they wrote, is this possible? And if so how could I incorporate this into my code?
Thank you in advance for any help!
You can store their description and then reuse it later:
When the user runs !setDescription I like Discord you save I like Discord in an object like this
//the user is stored as "message.author"
//the argument "I like Discord" is stored as "text"
var desc = {};
desc[message.author.id] = text; //desc -> {"id": "I like Discord"}
When the user runs !profile #me you use your desc object to get their description
//the user is stored as "message.author"
embed.setDescription(desc[message.author.id]);
Keep in mind that if you save the data into a variable, when you turn it off all the descriptions will be lost. To avoid that you can save the object into a JSON file or wherever you prefer.

Displaying all users in Discord channel

Right now I am working on a Discord bot and am attempting to list all users that are currently connected to the "general" voice channel.
My main issue right now is that my code is able to realize the number of people in the voice channel, but all of the "member" objects are undefined. This is both the console outputs and code. I have searched everywhere and can't seem to find anything.
This is the output from the console, the three "undefined" logs are the current users in the voice channel:
This is the code I have written:
For convience, this is also the code...
var chan = bot.channels['363589387411259396'];
var mems = chan.members;
for (var x in mems) {
console.log(x.userID);
}
return 'ANYTHING';
Any inputs helps, thanks!
Guild.members returns a Collection<Snowflake, GuildMember> object, where Snowflake is the GuildMember's ID.
Your main issue is this:userID property does not exist. What you should be looking for is GuildMember.id or simply printing out the Snowflake.
(Since it does not exist, printing out GuildMember.userID would result in printing out undefined)
Also, you can do your loop as what Venkata mentioned in the comments.

Categories