Merge pull request #1009 from nubdub/issue-721

This commit is contained in:
Ruben Bermudez 2022-06-15 02:37:20 +02:00 committed by GitHub
commit d2c6e6439b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 48 deletions

View File

@ -2,23 +2,30 @@
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
public class MrGoodGuy implements AbilityExtension {
private AbilityBot extensionUser;
public MrGoodGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
public Ability nice() {
return Ability.builder()
.name("nice")
.privacy(PUBLIC)
.locality(ALL)
.action(ctx -> silent.send("You're awesome!", ctx.chatId())
.action(ctx -> extensionUser.silent().send("You're awesome!", ctx.chatId())
);
}
}
public class MrBadGuy implements AbilityExtension {
private AbilityBot extensionUser;
public MrBadGuy(AbilityBot extensionUser) { this.extensionUser = extensionUser; }
public Ability notnice() {
return Ability.builder()
.name("notnice")
.privacy(PUBLIC)
.locality(ALL)
.action(ctx -> silent.send("You're horrible!", ctx.chatId())
.action(ctx -> extensionUser.silent().send("You're horrible!", ctx.chatId())
);
}
}
@ -28,11 +35,11 @@ public class MrBadGuy implements AbilityExtension {
// Constructor for your bot
public AbilityExtension goodGuy() {
return new MrGoodGuy();
return new MrGoodGuy(this);
}
public AbilityExtension badGuy() {
return new MrBadGuy();
return new MrBadGuy(this);
}
// Override creatorId
@ -46,7 +53,7 @@ It's also possible to add extensions in the constructor by using the `addExtensi
public YourAwesomeBot() {
super(/* pass required args ... */);
addExtensions(new MrGoodGuy(), new MrBadGuy());
addExtensions(new MrGoodGuy(this), new MrBadGuy(this));
}
// Override creatorId

View File

@ -41,9 +41,13 @@ class ExtensionTest {
}
public static class ExtensionUsingBot extends AbilityBot {
/**
* Constructor for ExtensionUsingBot
*/
ExtensionUsingBot() {
// https://github.com/rubenlagus/TelegramBots/issues/834
super("", "", offlineInstance("testing"));
addExtension(new AbilityBotExtension("addedInConstructor"));
addExtension(new AbilityBotExtension("addedInConstructor", this));
}
@Override
@ -51,12 +55,22 @@ class ExtensionTest {
return 0;
}
/**
* Method for returning AbiltyExtension
* @return AbilityBotExtension instance
*/
public AbilityBotExtension methodReturningExtensionSubClass() {
return new AbilityBotExtension("returningSubClass");
// https://github.com/rubenlagus/TelegramBots/issues/834
return new AbilityBotExtension("returningSubClass", this);
}
/**
* Method for returning AbilityExtension
* @return AbiltyBotExtension instance
*/
public AbilityExtension methodReturningExtensionSuperClass() {
return new AbilityBotExtension("returningSuperClass");
// https://github.com/rubenlagus/TelegramBots/issues/834
return new AbilityBotExtension("returningSuperClass", this);
}
public Ability methodReturningAbility() {
@ -73,9 +87,18 @@ class ExtensionTest {
public static class AbilityBotExtension implements AbilityExtension {
private String name;
private AbilityBot extensionUser;
AbilityBotExtension(String name) {
/**
* https://github.com/rubenlagus/TelegramBots/issues/721
* Constructor for AbilityBotExtension
* @param name Name of the ability extension
* @param extensionUser The AbilityBot that uses this AbilityExtension
*/
AbilityBotExtension(String name, AbilityBot extensionUser) {
this.name = name;
// https://github.com/rubenlagus/TelegramBots/issues/834
this.extensionUser = extensionUser;
}
public Ability abc() {
@ -85,6 +108,8 @@ class ExtensionTest {
.locality(ALL)
.privacy(PUBLIC)
.action(ctx -> {
// https://github.com/rubenlagus/TelegramBots/issues/834
extensionUser.silent().send("This is a test message.", ctx.chatId());
})
.build();
}