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.
Related
So, I am new to JavaScript. I am making a Discord bot because it's the only thing I would really use JavaScript for at the moment and I want to learn something. Whenever I try to show something like an id it says undefined.
client.on("channelCreate", function(channel){
console.log(channel.type+` channel, `+channel.name+`, was created by `+`${channel.owner_id}`+` in the category `+channel.parent_id);
});
I want to make a basic log section for my bot, and I am just starting. I want it to be like
00:00:00 CREATE > [Text / Voice] Channel was created by [Creator] under category [category]!
I know the exact format isn't implemented into the code, I test a lot of the function before I make it pretty lol. But it comes out like this (using the code I have now)
text channel, s, was created by undefined in the category undefined
I'm so confused because I like detail so I want to have who created it, when, and also the category! So how would I do that?
Also, would there be a way to send messages and stuff from the console?
Like if I typed this in the console
[channel or user] > [the message]
#testchannel > Announcing new video or something idk
and have it send to the channel? any help is appreciated!
edit: I forgot to mention that I have the owner_id part in ${var} format because I was testing to see if that would make it work, but I never switched it back lol
Changing the channel.parent.name would work but there isn't any way to know who created the channel , here's the code -
client.on("channelCreate", function(channel){
console.log(channel.type+` channel, `+channel.name+`,`+` in the category `+channel.parent.name);
});
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);
With my discord bot, i am trying to create a welcome message.
I got the following working:
Sending a message when someone joins
Getting the name of the person who joins
Now the problem I have is with doing a # or # in that message. I use the following code for the message:
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #:page_with_curl:rules and assign yourself a role at #:mortar_board:role-assignment`;
But I have also tried:
Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment
Both of them do not what i want them do to. I am getting the following result from this code:
What you can see there is that the #MEE6 and the 2 channel # is not blue, and not clickable. But when I copy the full text, past it and send it in the chat it does show blue.
The result I would like to have is the result of when I copy, paste and send it. That is the following:
Here is a little more of the code:
const welcomeChannel = client.channels.cache.get('738678076174630922');
const welcomeMessage = `Welcome #${member.user.username} to the server! Please look through the #📃rules and assign yourself a role at #🎓role-assignment`;
welcomeChannel.send(welcomeMessage)
What should I do to fix this?
const welcomeMessage = `Welcome ${member}! Please look through the <#id-of-rules-channel-here> and assign yourself a role at <#id-of-role-assignment-channel-here>`
You can auto-embed Discord classes like channels and members if you already have them stored in a variable (like you do for member), or you can use their IDs in text directly in the form <#id> for channels or <#id> for users.
So I have a discord bot that stores users in-game username and puts it into a text file.
var name1 = {
discordID: 438081715576242176,
apexNAME: test1
}
var name2 = {
discordID: 438081715576242176,
apexNAME: test2
}
like this.
If the user writes !stats then if their discordID matches the writers discordID in discord then send apexNAME.
If you were storing with a text file, you would need to store it plainly and use split and joins to return after retrieving it with node fs.
My advice is to check out the sections of the discord.js guide relating persisting data
https://discordjs.guide/keyv/#installation
In my bot i use sequelize, there is an example of using it on that page aswell.
I have my current Discord Bot project using JavaScript however i cannot find a way to store the value itself when done through a command such as !setname ___ hoping for a response of that being repeated back when I use !myname.
I know how to do it by storing it temporarily through:
bot.on('message', function (user, userID, channelID, message, evt) {
if(message == "!myname") {
bot.sendMessage({
to: channelID,
message: 'Your name is ' + user.username;
});
}
}
However i cant figure out how to do it through the ability to call for it whenever you want rather than getting an immediate response. For example setting my name now but being able to have it output when i use the command instead of having command that sets the value then outputs it in an immediate string
Would recommend a simple key-value dictionary style in the current instance.
(Though you could store it to a text-file or database later if you prefer the data to be persistent across different sessions.)
Instance based:
Based on this post on stackoverflow...
You can create a global dictionary at the top var usernameChangeDict = {};
Whenever the user invokes !setname, just do:
usernameChangeDict[user.id] = Their_new_name_here
Fetch it back using usernameChangeDict[user.id], should return null or empty if they did not set their name before.
Persistent data:
Could either store it in a text file on your local computer, or do a database as Zooly mentioned.(Though personally, a database would be an overkill if your bot is not going to be used by a wide range of people.)
Text file
Read from this post, it contains details on how to write onto a text file.
This post talks about reading from a text file.
I would recommend storing it in a JSON format to make your life easier, since JSON.parse exists.
Most likely you would end up storing in a format like this:
{ "23125167744": "someone", "USER_ID": "username" }
Access it by parsedJson["USER_ID"].
SQLite
This tutorial would probably explain how to set your SQLite for javascript better than I do.
Once its all setup, just store the userID and username, you can just do:
SELECT username FROM yourTable WHERE userID = ?;
and
INSERT INTO yourTable (username, userID) VALUES (?, ?);
For inserting and selecting into database respectively.