Detritus
A wheels-attached, pure-TypeScript library for the Discord API.
Installation
Detritus is distributed via npm. A high-level wrapper over the Discord API is provided
in this package, detritus-client
. Low-level wrappers over Discord's REST API and Gateway
are provided through detritus-client-rest
and
detritus-client-socket
.
$ npm i detritus-client
$ yarn add detritus-client
Usage
Detritus is operated through the Clients classes:
ShardClient
provides a base client for connecting to the Discord API and receiving events.ClusterClient
provides a client that creates ShardClient
classes inside of it for easier shardingCommandClient
wraps over the ClusterClient
or ShardClient
to provide support for bot commands.ClusterManager
provides a manager that'll spawn in multiple ClusterClient
processes for big shardings
More Examples are provided under the examples/
directory.
Command Client Sample
const { CommandClient } = require('detritus-client');
const token = '';
const commandClient = new CommandClient(token, {
prefix: '..',
});
commandClient.add({
name: 'ping',
run: (context, args) => {
return context.reply('pong!');
},
});
commandClient.add({
name: 'owner',
onBefore: (context) => context.client.isOwner(context.userId),
onCancel: (context) => context.reply('This command is only available to the bot owner.'),
run: async (context) => {
await context.reply('You are the owner of the bot!');
},
});
(async () => {
const client = await commandClient.run();
console.log(`Client has loaded with a shard count of ${client.shardCount}`);
})();
Shard Client Sample
const { ShardClient } = require('detritus-client');
const token = '';
const client = new ShardClient(token, {
gateway: {
loadAllMembers: true,
},
});
client.on('guildCreate', async ({fromUnavailable, guild}) => {
if (fromUnavailable) {
console.log(`Guild ${guild.name} has just came back from being unavailable`);
} else {
console.log(`Joined Guild ${guild.name}, bringing us up to ${client.guilds.length} guilds.`);
}
});
client.on('messageCreate', async ({message}) => {
if (message.content === '!ping') {
const reply = await message.reply('pong!, deleting message in 5 seconds...');
setTimeout(async () => {
await reply.delete();
}, 5000);
}
});
(async () => {
await client.run();
console.log('Successfully connected to Discord!');
console.log(`Currently have ${client.guilds.length} guilds in cache.`);
client.gateway.setPresence({
activity: {
name: 'with Detritus',
type: 0,
},
status: 'dnd',
});
})();
Cluster Client Sample
const { ClusterClient } = require('detritus-client');
const token = '';
const cluster = new ClusterClient(token, {
gateway: {
presence: {
activity: {
name: 'with Detritus ClusterClient',
type: 0,
},
status: 'dnd',
},
},
});
cluster.on('guildCreate', async ({fromUnavailable, guild, shard}) => {
if (fromUnavailable) {
console.log(`Shard #${shard.shardId}:`, `Guild ${guild.name} has just came back from being unavailable`);
} else {
console.log(`Shard #${shard.shardId}:`, `Joined Guild ${guild.name}, bringing us up to ${client.guilds.length} guilds.`);
}
});
cluster.on('messageCreate', async ({message, shard}) => {
if (message.content === '!ping') {
const reply = await message.reply(`pong on shard #${shard.shardId}!, deleting message in 5 seconds...`);
setTimeout(async () => {
await reply.delete();
}, 5000);
}
});
(async () => {
await cluster.run();
console.log(`Successfully launched shards ${cluster.shardStart} to ${cluster.shardEnd} with a shardCount of ${cluster.shardCount}`);
})();
Contributing
Detritus is licensed under the BSD-2 license; see the LICENSE.
To contribute, please first open an issue describing your requested changes,
and then open a pull request.