Skip to content

Commit

Permalink
Merge pull request #40 from nyxx-discord/next
Browse files Browse the repository at this point in the history
Release 4.0.0
  • Loading branch information
l7ssha authored Mar 15, 2022
2 parents d2f87ed + 7fa7a9b commit c203484
Show file tree
Hide file tree
Showing 32 changed files with 4,466 additions and 2,557 deletions.
174 changes: 123 additions & 51 deletions CHANGELOG.md

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ void main() {
// ======= Registering a command ======== //
// ====================================== //

// To register a command, we must first create an instance of the `Command` class.
// To register a command, we must first create an instance of the `ChatCommand` class.
// This class represents a single command, slash command or not, and once added to the bot will
// automatically be ready to use.
//
// As our first example, let's create a ping command that simply replies with `pong`.
Command ping = Command(
ChatCommand ping = ChatCommand(
// The first parameter is the command name.
// Command names must obey certain rules, if they don't an error will be thrown.
// ChatCommand names must obey certain rules, if they don't an error will be thrown.
// Generally using lower case letters and dashes (`-`) instead of spaces or underscores will
// avoid any problems.
'ping',
Expand All @@ -122,16 +122,16 @@ void main() {

// The third parameter is the function that will be executed when the command is ran.
//
// The first parameter to this function must be a `Context`. A `Context` allows you to access
// The first parameter to this function must be a `IChatContext`. A `IChatContext` allows you to access
// various information about how the command was run: the user that executed it, the guild it
// was ran in and a few other useful pieces of information.
// `Context` also has a couple of methods that make it easier to respond to commands.
// `IChatContext` also has a couple of methods that make it easier to respond to commands.
//
// Since a ping command doesn't have any other arguments, we don't add any other parameters to
// the function.
(Context context) {
(IChatContext context) {
// For a ping command, all we need to do is respond with `pong`.
// To do that, we can use the `Context`'s `respond` method which responds to the command with
// To do that, we can use the `IChatContext`'s `respond` method which responds to the command with
// a message.
context.respond(MessageBuilder.content('pong!'));
},
Expand Down Expand Up @@ -167,8 +167,8 @@ void main() {

// We have to use the variable name `throwGroup` since `throw` is a reserved keyword. Note that
// the variable name does not change how the group behaves at all though.
Group throwGroup = Group(
// Similarly to `Command`, the `Group` constructor's first two arguments are the group's name
ChatGroup throwGroup = ChatGroup(
// Similarly to `ChatCommand`, the `ChatGroup` constructor's first two arguments are the group's name
// and description.
'throw',
'Throw an objet',
Expand All @@ -180,10 +180,10 @@ void main() {
// method here and the `throw die` command later.
children: [
// We create a command in the same way as we created the `ping` command earlier.
Command(
ChatCommand(
'coin',
'Throw a coin',
(Context context) {
(IChatContext context) {
bool heads = Random().nextBool();

context.respond(
Expand All @@ -193,12 +193,12 @@ void main() {
],
);

// The other way to add a command to a group is using the `Group`'s `addCommand` method,
// The other way to add a command to a group is using the `ChatGroup`'s `addCommand` method,
// similarly to how we added the `ping` command to the bot earlie.
throwGroup.addCommand(Command(
throwGroup.addCommand(ChatCommand(
'die',
'Throw a die',
(Context context) {
(IChatContext context) {
int number = Random().nextInt(6) + 1;

context.respond(MessageBuilder.content('The die landed on the $number!'));
Expand All @@ -220,7 +220,7 @@ void main() {
// ====== Using command arguments ======= //
// ====================================== //

// Command argumens are another powerful tool that allow you to get user input when using
// ChatCommand arguments are another powerful tool that allow you to get user input when using
// commands.
// Adding arguments to your commands in nyxx_commands is simple, just add the argument as a
// parameter to your `execute` function and nyxx_commands will do the rest, including:
Expand All @@ -230,13 +230,13 @@ void main() {
//
// As an example, let's implement a `say` command that simply repeats what the user input.

Command say = Command(
ChatCommand say = ChatCommand(
'say',
'Make the bot say something',
// As mentioned earlier, all we need to do to add an argument to our command is add it as a
// parameter to our execute function. In this case, we take an argument called `message` and of
// type `String`.
(Context context, String message) {
(IChatContext context, String message) {
context.respond(MessageBuilder.content(message));
},
);
Expand Down Expand Up @@ -289,14 +289,14 @@ void main() {
//
// As an example, let's create a `nick` command that changes a user's nickname.

Command nick = Command(
ChatCommand nick = ChatCommand(
'nick',
"Change a user's nickname",
// Setting the type of the `target` parameter to `IMember` will make nyxx_commands convert user
// input to instances of `IMember`.
(Context context, IMember target, String newNick) async {
(IChatContext context, IMember target, String newNick) async {
try {
await target.edit(nick: newNick);
await target.edit(builder: MemberBuilder()..nick = newNick);
} on IHttpResponseError {
context.respond(MessageBuilder.content("Couldn't change nickname :/"));
return;
Expand Down Expand Up @@ -357,7 +357,7 @@ void main() {
// converter from scratch for this, since no existing converter can be mapped to a `Shape`.

// To create the converter, we instanciate the `Converter` class.
//Note that the variable is fully
// Note that the variable is fully
// typed, the typed generics on `Converter` are filled in. This allows nyxx_commands to know what
// the target type of this converter is.
Converter<Shape> shapeConverter = Converter<Shape>(
Expand All @@ -369,7 +369,7 @@ void main() {
// The first parameter to the function is an instance of `StringView`. `StringView` allows you
// to manipulate and extract data from a `String`, but also allows the next converter to know
// where to start parsing its argument from.
// The second parameter is the current `Context` in which the argument is being parsed.
// The second parameter is the current `IChatContext` in which the argument is being parsed.
(view, context) {
// In our case, we want to return a `Shape` based on the user's input. The `getQuotedWord()`
// will get the next quoted word from the input.
Expand Down Expand Up @@ -433,10 +433,10 @@ void main() {
commands.addConverter(dimensionConverter);

// Let's create a command to test our converter out:
Command favouriteShape = Command(
ChatCommand favouriteShape = ChatCommand(
'favourite-shape',
'Outputs your favourite shape',
(Context context, Shape shape, Dimension dimension) {
(IChatContext context, Shape shape, Dimension dimension) {
String favourite;

switch (shape) {
Expand Down Expand Up @@ -483,7 +483,7 @@ void main() {
// ===== Using optional arguments ======= //
// ====================================== //

// Optional argument allow you to allow the user to input something while providing a default
// Optional arguments allow you to allow the user to input something while providing a default
// value.
// Like converters, optional arguments are easy to use: just make the parameter in your `execute`
// function optional.
Expand All @@ -496,16 +496,16 @@ void main() {
//
// As an example:
// ```dart
// (Context context, [String? a, String? b, String? c]) {}
// (IChatContext context, [String? a, String? b, String? c]) {}
// ```
// In this case, `b` having a value does not guarantee `a` has a value. As such, it is always
// better to provide a default for your optional parameters instead of making them nullable.

// As an example for using optional arguments, let's create a command with an optional argument:
Command favouriteFruit = Command(
ChatCommand favouriteFruit = ChatCommand(
'favourite-fruit',
'Outputs your favourite fruit',
(Context context, [String favourite = 'apple']) {
(IChatContext context, [String favourite = 'apple']) {
context.respond(MessageBuilder.content('Your favourite fruit is $favourite!'));
},
);
Expand Down Expand Up @@ -533,24 +533,24 @@ void main() {
//
// As an example, we'll create a command with a cooldown:

Command alphabet = Command(
ChatCommand alphabet = ChatCommand(
'alphabet',
'Outputs the alphabet',
(Context context) {
(IChatContext context) {
context.respond(MessageBuilder.content('ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
},
// Since this command is spammy, we can use a cooldown to restrict it's usage:
// Since this command is spammy, we can use a cooldown to restrict its usage:
checks: [
CooldownCheck(
CooldownType.user,
CooldownType.user | CooldownType.guild, // Make the cooldown per user per guild
Duration(seconds: 30),
)
],
);

commands.addCommand(alphabet);

// At this point, if you run the file you will get an `alphabet` command appear in the slash
// At this point, if you run the file you will see an `alphabet` command appear in the slash
// command menu. Executing it once will run fine, however trying to execute it again less that 30
// seconds later will cause the command to fail with an exception. After 30 seconds have passed,
// the command can be used again.
Expand All @@ -575,11 +575,11 @@ void main() {
// You can see the implementation of `filterInput` at the bottom of this file.
const Converter<String> nonEmptyStringConverter = CombineConverter(stringConverter, filterInput);

Command betterSay = Command(
ChatCommand betterSay = ChatCommand(
'better-say',
'A better version of the say command',
(
Context context,
IChatContext context,
@UseConverter(nonEmptyStringConverter) String input,
) {
context.respond(MessageBuilder.content(input));
Expand Down Expand Up @@ -612,7 +612,7 @@ enum Dimension {
// ---------- Global functions ---------- //
// -------------------------------------- //

String? filterInput(String input, Context context) {
String? filterInput(String input, IContext context) {
if (input.isNotEmpty) {
return input;
}
Expand Down
44 changes: 22 additions & 22 deletions example/example_clean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ void main() {

client.connect();

Command ping = Command(
ChatCommand ping = ChatCommand(
'ping',
'Checks if the bot is online',
(Context context) {
(IChatContext context) {
context.respond(MessageBuilder.content('pong!'));
},
);

commands.addCommand(ping);

Group throwGroup = Group(
ChatGroup throwGroup = ChatGroup(
'throw',
'Throw an objet',
children: [
Command(
ChatCommand(
'coin',
'Throw a coin',
(Context context) {
(IChatContext context) {
bool heads = Random().nextBool();

context.respond(
Expand All @@ -62,10 +62,10 @@ void main() {
],
);

throwGroup.addCommand(Command(
throwGroup.addCommand(ChatCommand(
'die',
'Throw a die',
(Context context) {
(IChatContext context) {
int number = Random().nextInt(6) + 1;

context.respond(MessageBuilder.content('The die landed on the $number!'));
Expand All @@ -74,22 +74,22 @@ void main() {

commands.addCommand(throwGroup);

Command say = Command(
ChatCommand say = ChatCommand(
'say',
'Make the bot say something',
(Context context, String message) {
(IChatContext context, String message) {
context.respond(MessageBuilder.content(message));
},
);

commands.addCommand(say);

Command nick = Command(
ChatCommand nick = ChatCommand(
'nick',
"Change a user's nickname",
(Context context, IMember target, String newNick) async {
(IChatContext context, IMember target, String newNick) async {
try {
await target.edit(nick: newNick);
await target.edit(builder: MemberBuilder()..nick = newNick);
} on IHttpResponseError {
context.respond(MessageBuilder.content("Couldn't change nickname :/"));
return;
Expand Down Expand Up @@ -139,10 +139,10 @@ void main() {

commands.addConverter(dimensionConverter);

Command favouriteShape = Command(
ChatCommand favouriteShape = ChatCommand(
'favourite-shape',
'Outputs your favourite shape',
(Context context, Shape shape, Dimension dimension) {
(IChatContext context, Shape shape, Dimension dimension) {
String favourite;

switch (shape) {
Expand Down Expand Up @@ -174,25 +174,25 @@ void main() {

commands.addCommand(favouriteShape);

Command favouriteFruit = Command(
ChatCommand favouriteFruit = ChatCommand(
'favourite-fruit',
'Outputs your favourite fruit',
(Context context, [String favourite = 'apple']) {
(IChatContext context, [String favourite = 'apple']) {
context.respond(MessageBuilder.content('Your favourite fruit is $favourite!'));
},
);

commands.addCommand(favouriteFruit);

Command alphabet = Command(
ChatCommand alphabet = ChatCommand(
'alphabet',
'Outputs the alphabet',
(Context context) {
(IChatContext context) {
context.respond(MessageBuilder.content('ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
},
checks: [
CooldownCheck(
CooldownType.user,
CooldownType.user | CooldownType.guild,
Duration(seconds: 30),
)
],
Expand All @@ -202,11 +202,11 @@ void main() {

const Converter<String> nonEmptyStringConverter = CombineConverter(stringConverter, filterInput);

Command betterSay = Command(
ChatCommand betterSay = ChatCommand(
'better-say',
'A better version of the say command',
(
Context context,
IChatContext context,
@UseConverter(nonEmptyStringConverter) String input,
) {
context.respond(MessageBuilder.content(input));
Expand All @@ -227,7 +227,7 @@ enum Dimension {
threeD,
}

String? filterInput(String input, Context context) {
String? filterInput(String input, IContext context) {
if (input.isNotEmpty) {
return input;
}
Expand Down
Loading

0 comments on commit c203484

Please sign in to comment.