Update wiki to show how to access AbilityBot.silent in AbilityExtension implemented class, update ExtensionTest to show change

This commit is contained in:
nubdub 2021-12-09 00:23:35 -05:00
parent 2663d01761
commit 7dd0711cdb
2 changed files with 58 additions and 48 deletions

View File

@ -2,41 +2,48 @@
You have around 100 abilities in your bot and you're looking for a way to refactor that mess into more modular classes. `AbillityExtension` is here to support just that! It's not a secret that AbilityBot uses refactoring backstage to be able to construct all of your abilities and map them accordingly. However, AbilityBot searches initially for all methods that return an `AbilityExtension` type. Then, those extensions will be used to search for declared abilities. Here's an example. You have around 100 abilities in your bot and you're looking for a way to refactor that mess into more modular classes. `AbillityExtension` is here to support just that! It's not a secret that AbilityBot uses refactoring backstage to be able to construct all of your abilities and map them accordingly. However, AbilityBot searches initially for all methods that return an `AbilityExtension` type. Then, those extensions will be used to search for declared abilities. Here's an example.
```java ```java
public class MrGoodGuy implements AbilityExtension { public class MrGoodGuy implements AbilityExtension {
public Ability nice() { private AbilityBot extensionUser;
return Ability.builder()
.name("nice") public MrGoodGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
.privacy(PUBLIC)
.locality(ALL) public Ability nice() {
.action(ctx -> silent.send("You're awesome!", ctx.chatId()) return Ability.builder()
); .name("nice")
} .privacy(PUBLIC)
.locality(ALL)
.action(ctx -> extensionUser.silent().send("You're awesome!", ctx.chatId())
);
}
} }
public class MrBadGuy implements AbilityExtension { public class MrBadGuy implements AbilityExtension {
public Ability notnice() { private AbilityBot extensionUser;
return Ability.builder()
.name("notnice") public MrBadGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
.privacy(PUBLIC) public Ability notnice() {
.locality(ALL) return Ability.builder()
.action(ctx -> silent.send("You're horrible!", ctx.chatId()) .name("notnice")
); .privacy(PUBLIC)
} .locality(ALL)
} .action(ctx -> extensionUser.silent().send("You're horrible!", ctx.chatId())
);
public class YourAwesomeBot implements AbilityBot { }
}
public class YourAwesomeBot implements AbilityBot {
// Constructor for your bot // Constructor for your bot
public AbilityExtension goodGuy() { public AbilityExtension goodGuy() {
return new MrGoodGuy(); return new MrGoodGuy(this);
} }
public AbilityExtension badGuy() { public AbilityExtension badGuy() {
return new MrBadGuy(); return new MrBadGuy(this);
} }
// Override creatorId // Override creatorId
} }
``` ```
It's also possible to add extensions in the constructor by using the `addExtension()` or `addExtensions()` method: It's also possible to add extensions in the constructor by using the `addExtension()` or `addExtensions()` method:
@ -45,10 +52,10 @@ It's also possible to add extensions in the constructor by using the `addExtensi
public class YourAwesomeBot implements AbilityBot { public class YourAwesomeBot implements AbilityBot {
public YourAwesomeBot() { public YourAwesomeBot() {
super(/* pass required args ... */); super(/* pass required args ... */);
addExtensions(new MrGoodGuy(), new MrBadGuy()); addExtensions(new MrGoodGuy(this), new MrBadGuy(this));
} }
// Override creatorId // Override creatorId
} }
``` ```

View File

@ -43,7 +43,7 @@ class ExtensionTest {
public static class ExtensionUsingBot extends AbilityBot { public static class ExtensionUsingBot extends AbilityBot {
ExtensionUsingBot() { ExtensionUsingBot() {
super("", "", offlineInstance("testing")); super("", "", offlineInstance("testing"));
addExtension(new AbilityBotExtension("addedInConstructor")); addExtension(new AbilityBotExtension("addedInConstructor", this));
} }
@Override @Override
@ -52,41 +52,44 @@ class ExtensionTest {
} }
public AbilityBotExtension methodReturningExtensionSubClass() { public AbilityBotExtension methodReturningExtensionSubClass() {
return new AbilityBotExtension("returningSubClass"); return new AbilityBotExtension("returningSubClass", this);
} }
public AbilityExtension methodReturningExtensionSuperClass() { public AbilityExtension methodReturningExtensionSuperClass() {
return new AbilityBotExtension("returningSuperClass"); return new AbilityBotExtension("returningSuperClass", this);
} }
public Ability methodReturningAbility() { public Ability methodReturningAbility() {
return Ability.builder() return Ability.builder()
.name("direct") .name("direct")
.info("Test ability") .info("Test ability")
.locality(ALL) .locality(ALL)
.privacy(PUBLIC) .privacy(PUBLIC)
.action(messageContext -> { .action(messageContext -> {
}) })
.build(); .build();
} }
} }
public static class AbilityBotExtension implements AbilityExtension { public static class AbilityBotExtension implements AbilityExtension {
private String name; private String name;
private AbilityBot extensionUser;
AbilityBotExtension(String name) { AbilityBotExtension(String name, AbilityBot extensionUser) {
this.name = name; this.name = name;
this.extensionUser = extensionUser;
} }
public Ability abc() { public Ability abc() {
return Ability.builder() return Ability.builder()
.name(name + "0abc") .name(name + "0abc")
.info("Test ability") .info("Test ability")
.locality(ALL) .locality(ALL)
.privacy(PUBLIC) .privacy(PUBLIC)
.action(ctx -> { .action(ctx -> {
}) extensionUser.silent().send("This is a test message.", ctx.chatId());
.build(); })
.build();
} }
} }
} }