#ts ๐
112 messages ยท Page 1 of 1 (latest)
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
Well, "type"
This is my model
const mongoose = require("mongoose");
const schema = new mongoose.Schema({
guildId: {
type: String, unique: true
},
userId: {
type: String
},
level: {
type: Number,
default: 0
},
exp: {
type: Number,
default: 0
},
weeklyExp: {
type: Number,
default: 0
},
});
module.exports = mongoose.model("levelling", schema);
Was gonna use it in something like this
/**
* @param {GuildMember} member
* @param {TextChannel} channel
* @param {type} settings
*/
calculateXptoAdd(member, channel, settings = []) {
//code
}
What I'm gonna pass will look something like this:
[
{ type: 'role', id: '1045819599955963906', amount: '5' },
{ type: 'channel', id: '1092045790270197770', amount: '1' },
{ type: 'role', id: '977826521165205524', amount: '100' }
]
That schema and your variable you pass have no resemblance, so how did you expect that to work?
I was just giving that for context ๐
Probably wasnโt necessary
I honestly wanna move to ts at this rate but it will be painful to rewrite my bot in it when I donโt know much of it yet
You wonโt have to rewrite it fully. First step is just installing typescript, tsc init to get a tsconfig and add allowJs: true. Then you can start gradually renaming to .ts and adding types to parameters (and then fixing any issues your js code had but you never knew about because you didnโt have ts)
How will that work? Like do I have to compile everything?
Since I bet you canโt really type with jsdoc like I want to
You can
But ts is better
As always ๐
How will working with ts and js work
djs is typed with JSDoc after all
๐thatโs literally all you need
How can I type an array that will have this stuff
Yh but if I add a ts file, how will I run the bot? As node . Wont work right? Since Iโll have to compile the ts to js for those ts files
https://github.com/discordjs/discord.js/blob/778df451663d04cd1fb5818ef4dcdd01a7b90cc1/packages/discord.js/src/managers/ApplicationCommandManager.js#L150 for reference, this is how you can type an array of a type you define in another JSDoc comment before (scroll up to find how)
Yes, tsc will transpile those for you. Ts is only for development, you run js files either way
Unless you use deno or bun to run ts directly.
so node . Will work or wonโt?
So djs has structures for things and uses typedef with jsdoc?
After running tsc (and configuring the main entry point in package.json correctly) yes. Better to set a npm script to run though
Can also use tsc --watch to have it transpile on every file save
Ah, is that where people have like a dist folder of just the fully compiled code and run that
Yes. Thatโs what tsc does for you
Ahh gotcha
Then to run itโll just be node ./dist/index.js
If Iโm in the main folder
Best way is to put ./dist/index.js in your package.json as main, then itโll just be node .
Ahhhhhhh
I didnโt know you could do that ๐
Alright thanks
Iโll clone my main bot folder on my laptop and give it a go later or tomorrow or whatever
The hard thing will be to learn how to type mongo documents but the site has a guide on how to use ts with mongoose
Afaik mongoose is typed so you wouldnโt even need to do anything once you use it in typescript itโll just work
Idk if yoy need to or not but I think you need to add stuff into the thing
Idk the words
Err
Nvm
ts ๐
Its typed yes , but the schema if you do it in TS directly it will not look like the one in js
example in ts :
import type { blacklistUsers } from '#type/index.js';
import { model, Schema } from 'mongoose';
const blacklist = new Schema<blacklistUsers>({
guildID: { type: String },
ID: { type: String },
reason: { type: String },
});
export default model<blacklistUsers>('userBlacklist', blacklist);
// The following stuff is in another file
export interface blacklistUsers {
guildID: string;
ID: string;
reason?: string;
}
Yh I saw this ๐ , why would you do it in another file though?
Or do you not have to
As they also say to extent the interface
err
import { HydratedDocument } from 'mongoose';
interface IUser {
name: string;
email: string;
avatar?: string;
}
const user: HydratedDocument<IUser> = new User({
name: 'Bill',
email: 'bill@initech.com',
avatar: 'https://i.imgur.com/dM7Thhn.png'
});
Just for me being cleaner , all my interfaces for my db is in 1 file
fair
do you know about this stuff btw
or is it not neccesary to use this HydratedDocument thing
i used mongoose with TS so , i know a bit XD
i never used that form of writing tho const user: HydratedDocument<IUser> = new User({}) , i used it like i sent it earlier
//blacklist.ts
import type { blacklistUsers } from '#type/index.js'; //(Interface import)
import { model, Schema } from 'mongoose';
const blacklist = new Schema<blacklistUsers>({
guildID: { type: String },
ID: { type: String },
reason: { type: String },
});
export default model<blacklistUsers>('userBlacklist', blacklist);
// ------------------- database.d.ts
export interface blacklistUsers {
guildID: string;
ID: string;
reason?: string;
}
// ------------------- blacklist slash command
import blacklistUser from '#database/blacklistUser.js'; //(You import your schema made earlier)
...
await blacklistUser.create({
guildID: interaction.guild.id,
ID: interaction.user.id,
reason: reason,
});
๐
so just use normal interface's rather then the hydrated doc crap
you can yea
idek what the hydrated thing does you see
as i can see i think it replace the .create({})
i just think, im not sure about that
why do i get this error even though it's literally the correct path
Did you module.exports (export default (for TS)) correctly
AH
fuck this is gonna be fun
im so stupid
LMAO
(Recommendation) You should use ts-node instead of having a dist since its faster and you dont need to replace .ts in filenames with .js every time
i just compile everything
I'll be honest I used to rm -rf my ./dist then tsc ๐
it's what Qjuh said to do
Is it efficient though?
It takes like a few seconds
yh takes a few secs
idm that
you import from src/Level/Level
but you show dist/Level/Level
yh that's my non compiled stuff, probably should have shown the other one lol
i9 was right
was still using module.exports ๐๐คฃ
Oh , yeah it doesnt help xD
They use tsc
I used to debug that for hours ๐
๐ญ
ts-node for dev is ok but slow for prod
So I should run this every time I start my bot?:
rm -rf ./dist;tsc;node dist/src/index.js
I'll probably shorten it some way or another
no you do tsc watch
What does that do?
I've always wondered
With ts
How do you make like, a new class or something for Events
So you can do something like (probs bad ts all together thouggh ๐)
export new Event('interactionCreate) {}
type of thing
remember all js code is valid ts
And all ts code is valid js ||when compiled||
yh but i actually dk how to do that type of thing with js ๐
i just think it looks neat
So you can do something like (probs bad ts all together thouggh ๐)
export new Event('interactionCreate', async (some params) => {
//code
})
What the hell is export new Event
Where did default go, lol
missed default
Alright
constructor(eventName: string, callback: Function)
Dont need default if theres more than 1 thing in the module tho if i recall that correctly
I do it for safety ngl
u need to give it a name
Thanks for helping people ๐