Fundamentals

Events

Basic usage

To simplify the creation of event files, the CLI provides you with an initialisation command.

dart run mineral make:event <MyEvent>

A question/answer game then appears, asking you to select an event from among those available in the database.

All available events are available here.

Basically, a class listening to events from your Discord server is composed as follows

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class MessageCreateHandler extends MineralEvent<T> {
  Future<void> handle(T event) async {
    // Your code for handling the MessageCreateEvent
  }
}

Here's an example of an event handler for each type of event mentioned in the Mineral Events library:

MessageCreateEvent

This event is triggered when a new message is created in a channel. It provides information about the created message, such as its content, author, timestamp, etc.

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class MessageCreateHandler extends MineralEvent<MessageCreateEvent> {
  Future<void> handle(MessageCreateEvent event) async {
    // Your code for handling the MessageCreateEvent
  }
}

ReadyEvent

This event is triggered when the bot has successfully connected to the Discord API and is ready to start processing events. It indicates that the bot is fully initialized and can perform tasks such as setting its presence status, loading data, or initializing other services.

You can use this event to perform any necessary setup or initialization tasks when the bot starts.

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class ReadyHandler extends MineralEvent<ReadyEvent> {
  Future<void> handle(ReadyEvent event) async {
    // Your code for handling the ReadyEvent
  }
}

GuildCreateEvent

This event is triggered when the bot joins a new guild (server). It provides information about the newly joined guild, such as its name, ID, owner, member count, etc.

You can use this event to implement functionality that needs to be executed when the bot joins a new guild, such as setting up default channels, assigning roles, or sending welcome messages.

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class GuildCreateHandler extends MineralEvent<GuildCreateEvent> {
  Future<void> handle(GuildCreateEvent event) async {
    // Your code for handling the GuildCreateEvent
  }
}

ChannelCreateEvent

This event is triggered when a new channel is created in a guild. It provides information about the created channel, such as its name, ID, type, etc.

You can use this event to implement functionality related to channel management, such as logging new channel creations, applying channel-specific settings, or updating channel lists.

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class ChannelCreateHandler extends MineralEvent<ChannelCreateEvent> {
  Future<void> handle(ChannelCreateEvent event) async {
    // Your code for handling the ChannelCreateEvent
  }
}

MemberJoinEvent

This event is triggered when a new member joins a guild. It provides information about the joined member, such as their username, discriminator, ID, etc.

You can use this event to implement functionality related to member management, such as assigning roles, sending welcome messages, or logging new member joins.

import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';

class GuildMemberAddHandler extends MineralEvent<MemberJoinEvent> {
  Future<void> handle(MemberJoinEvent event) async {
    // Your code for handling the GuildMemberAddEvent
  }
}

You can customize each event handler by adding your own logic and functionality inside the handle method. These examples demonstrate the basic structure of event handlers for different event types.

Previous
Environment project