Creating a class for events

Hello, I am trying to make a class for events. I have came up with the following, and it doesn't work I have provided my eslint errors below:
export default abstract class Event {
name: Events
run: any

constructor (name: Events) {
const event = name
this.name = event
this.run = (client: client, ...args: ClientEvents[event]) => any
}
}
export default abstract class Event {
name: Events
run: any

constructor (name: Events) {
const event = name
this.name = event
this.run = (client: client, ...args: ClientEvents[event]) => any
}
}
I want the class to inherit the parameters when given an event from the Events enum from the discord.js package. e.g:
messageCreate: [message: Message];

// ################

export default new Event(
Events.MessageCreate,
async (client, message: Message) {
// code here
}
}
messageCreate: [message: Message];

// ################

export default new Event(
Events.MessageCreate,
async (client, message: Message) {
// code here
}
}
The eslint errors that I am recieving:
// Type 'event' cannot be used as an index type. ts(2538)
// 'event' refers to a value, but is being used as a type here. Did you mean 'typeof event'? ts(2749)
// Type 'event' cannot be used as an index type. ts(2538)
// 'event' refers to a value, but is being used as a type here. Did you mean 'typeof event'? ts(2749)
8 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Jerimiah
JerimiahOP2y ago
I made this that doesn't give me any errors but doesn't function how I would like it to
export default class Event {
name: Events
run: (client: client, ...event: ClientEvents[]) => Promise<void>
}
export default class Event {
name: Events
run: (client: client, ...event: ClientEvents[]) => Promise<void>
}
Kinect3000
Kinect30002y ago
Events only exists as a type, not a value usable in js
Jerimiah
JerimiahOP2y ago
Sorry for not updating I have resolved this issue
export default class Event<T extends keyof ClientEvents> {
public name: T
public run: (client: Client, ...event: ClientEvents[T]) => void

public constructor (name: Event<T>['name'], run: Event<T>['run']) {
this.name = name
this.run = run
client.on(this.name, (...args: ClientEvents[T]) => { this.run(client, ...args) })
}
}
export default class Event<T extends keyof ClientEvents> {
public name: T
public run: (client: Client, ...event: ClientEvents[T]) => void

public constructor (name: Event<T>['name'], run: Event<T>['run']) {
this.name = name
this.run = run
client.on(this.name, (...args: ClientEvents[T]) => { this.run(client, ...args) })
}
}
Kinect3000
Kinect30002y ago
Not rly a fan of how the event registers itself, would prefer if the caller did that
Jerimiah
JerimiahOP2y ago
As in like when the event is imported?
Kinect3000
Kinect30002y ago
Yea
Jerimiah
JerimiahOP2y ago
I made that adjustment thanks for the input Also a more cleaner implementation:
export default class Event<T extends keyof ClientEvents> {
public constructor (
public name: T,
public run: (client: client, ...event: ClientEvents[T]) => void | Promise<void>
) { }
}
export default class Event<T extends keyof ClientEvents> {
public constructor (
public name: T,
public run: (client: client, ...event: ClientEvents[T]) => void | Promise<void>
) { }
}
Want results from more Discord servers?
Add your server