Working with signals
Learn how to effectively use signals in Sunar to handle various events and interactions in your discord bot. This guide covers the basics of creating, managing, and responding to signals for a more interactive and responsive bot experience.
What are signals?
Signals are event emitters that allow different parts of your bot to communicate with each other in a decoupled manner. By using signals, you can easily manage and respond to events without tightly coupling your code components. This promotes cleaner code architecture and easier maintenance.
Creating a signal
Begin by defining a new signal using Sunar. Signals in Sunar are used to signify specific events or states within your bot's operation. For example, you can create a signal named ready to indicate when your bot has finished initializing and is ready to start processing commands.
import { Signal, Signals } from 'sunar';
const signal = new Signal(Signals.ClientReady, { once: true });The once option indicates that the signal should only be executed once.
Handle the execution
Now, to manage what happens when a signal is emitted, we use the execute mutator. This involves passing our signal as the first parameter and providing a callback function that will be executed when the signal is triggered.
import { Signal, Signals, execute } from 'sunar';
const signal = new Signal(Signals.ClientReady, { once: true });
execute(signal, () => {
console.log('Bot is ready!');
});Export the signal
To ensure Sunar can properly load and utilize the signal you've defined, it must be exported from its file.
import { Signal, Signals, execute } from 'sunar';
const signal = new Signal(Signals.ClientReady, { once: true });
execute(signal, () => {
console.log('Bot is ready!');
});
export { signal };The file names do not affect the behavior of your bot. You can use any name you prefer, as long as it matches the glob pattern specified when loading the Sunar modules.
Common signal examples
Here are some practical examples of signals you might want to use in your bot:
Message creation
import { Signal, Signals, execute } from 'sunar';
const signal = new Signal(Signals.MessageCreate);
execute(signal, (message) => {
// Ignore bot messages to prevent loops
if (message.author.bot) return;
// Example: Log all messages sent in guilds
if (message.guild) {
console.log(`${message.author.tag}: ${message.content}`);
}
});
export { signal };Guild member join
import { Signal, Signals, execute } from 'sunar';
const signal = new Signal(Signals.GuildMemberAdd);
execute(signal, async (member) => {
// Send welcome message
const channel = member.guild.systemChannel;
if (channel) {
channel.send(`Welcome ${member}! 👋`);
}
});
export { signal };Error handling
import { Signal, Signals, execute } from 'sunar';
const signal = new Signal(Signals.Error);
execute(signal, (error) => {
console.error('An error occurred:', error);
// Add your error reporting logic here
});
export { signal };Find all available events in the discord.js documentation.
How is this guide?
Last updated on
Load modules
Modules in Sunar encapsulate related commands, signals and components, enabling efficient organization and management of your bot's capabilities. Learn how to leverage Sunar's modular architecture to streamline development and scalability.
Interactions handling
Learn how to effectively manage interactions between your bot and users using Sunar. From responding to slash commands, context menu clicks, button presses, to select menu choices, Sunar offers robust mechanisms to handle diverse interactions within Discord.