Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Question for "Event Setup" #71

Discussion options

For the eventsPath and related documentation, I am having trouble making my file immediately run, upon startup without an event listener shown in the docs being triggered. Will show below my normal setup, with a regular command handler. What would I need to make this work with CommandKit in TS by chance? Thank you for your time and efforts.

const { Events } = require('discord.js'); 
module.exports = {
 name: Events.ClientReady,
 once: true,
 execute(client) {
 console.log('Immediately runs w/o anything triggered');
 // Looking to have this work with CommandKit.
 },
};

https://commandkit.js.org/guide/event-file-setup

You must be logged in to vote

Hello.

I'm a bit confused when you mention "immediately run". Do you want to:

  1. Have some code run without any event (including "ready") be triggered.
  2. Have some code run when the bot has started up.

Here's how you can achieve both of the above:

For 1:

To run code without any event being triggered (including "ready"), CommandKit doesn't really provide any way to handle this situation since it's out of the scope of handling commands/events with Discord.js. You can achieve this pretty easily however by placing your code anywhere before logging in to your client/bot.

// File: src/index.ts
// ... rest of your code
function main() {
 console.log('This will run immediately when the app starts...

Replies: 1 comment 1 reply

Comment options

Hello.

I'm a bit confused when you mention "immediately run". Do you want to:

  1. Have some code run without any event (including "ready") be triggered.
  2. Have some code run when the bot has started up.

Here's how you can achieve both of the above:

For 1:

To run code without any event being triggered (including "ready"), CommandKit doesn't really provide any way to handle this situation since it's out of the scope of handling commands/events with Discord.js. You can achieve this pretty easily however by placing your code anywhere before logging in to your client/bot.

// File: src/index.ts
// ... rest of your code
function main() {
 console.log('This will run immediately when the app starts without waiting for the bot to start up');
}
main();
client.login('your-token-here');

For 2:

To run code immediately after your bot has gone online, you can make use of the "ready" event provided by Discord.js. If you're trying to handle this event using CommandKit:

  1. Create a folder inside your "events" folder with the name of the event - in this case it's "ready".
  2. Inside the new "ready" folder, create a file (you can name it anything you want) that will export a default function:
    // File: src/events/ready/on-startup.ts
    import type { Client } from 'discord.js';
     
    export default function (client: Client<true>) {
     console.log('This will run immediately after the bot has gone online');
    }

Let me know if this solves your issue.

You must be logged in to vote
1 reply
Comment options

Thank you for your reply. The second option you displayed, was the solution for me.

Answer selected by git-cameronbryce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /