> For the complete documentation index, see [llms.txt](https://cjays-organization.gitbook.io/nexora.ts/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cjays-organization.gitbook.io/nexora.ts/framework/service.md).

# Service

Base class for injectable helpers with a child logger. Use for tickets, moderation, economy, etc.

**Package:** `@nexora.ts/core`

## Example

```ts
import { Service, registerService, type ServiceContext } from '@nexora.ts/core';
import type { Nexora } from '@nexora.ts/core';

export class TicketService extends Service {
  constructor(ctx: ServiceContext) {
    super(ctx);
  }

  async open(userId: string, reason: string) {
    this.logger.info('Opening ticket', { userId, reason });
    // DB / channel logic…
    return { id: 'ticket_1' };
  }
}

// After bot is constructed:
export function registerTicketService(bot: Nexora) {
  registerService(bot.container, 'TicketService', () =>
    new TicketService({ logger: bot.logger, container: bot.container }),
  );
}
```

## Resolve in a command

```ts
import { SlashCommand, type CommandContext } from '@nexora.ts/core';
import type { TicketService } from '../services/tickets.js';

export default class TicketCommand extends SlashCommand {
  name = 'ticket';
  description = 'Open a support ticket';

  async execute(ctx: CommandContext) {
    const tickets = ctx.client /* or pass container via closure */;
    // Prefer resolving from bot.container in your app wiring:
    // const tickets = bot.container.resolve<TicketService>('TicketService');
    await ctx.reply('Ticket opened.');
  }
}
```

Typical pattern — register services at startup, resolve in command modules that receive `bot` or use a shared container export:

```ts
// src/index.ts
const bot = new Nexora({ config, commandsPath: './commands/**/*.ts', eventsPath: './events/**/*.ts' });
registerTicketService(bot);
await bot.start();
```

```ts
// commands/ticket.ts
import { bot } from '../bot.js'; // your shared instance
import type { TicketService } from '../services/tickets.js';

export default class TicketCommand extends SlashCommand {
  name = 'ticket';
  description = 'Open a ticket';

  async execute(ctx: CommandContext) {
    const tickets = bot.container.resolve<TicketService>('TicketService');
    await tickets.open(ctx.user.id, 'help');
    await ctx.reply('Ticket created.');
  }
}
```

## Related

* [Dependency injection](/nexora.ts/advanced/dependency-injection.md)
* [SlashCommand](/nexora.ts/framework/slash-command.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cjays-organization.gitbook.io/nexora.ts/framework/service.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
