Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiducho committed Oct 18, 2024
2 parents a627929 + 1da1ecd commit e9f3018
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
name: Build
uses: Cadiducho/CadiBot/.github/workflows/gradle.yml@development
with:
jdk_version: 17
jdk_version: 21
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
name: Build
uses: Cadiducho/CadiBot/.github/workflows/gradle.yml@master
with:
jdk_version: 17
jdk_version: 21
deploy:
name: Deploy
needs: build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
description: "JDK version to build."
type: string
required: false
default: '17'
default: '21'

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
name: Build
uses: Cadiducho/CadiBot/.github/workflows/gradle.yml@development
with:
jdk_version: 17
jdk_version: 21
38 changes: 19 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
plugins {
id 'java'
id 'application'
id "io.freefair.lombok" version "8.4"
id "io.freefair.lombok" version "8.7.1"
}

group = 'com.cadiducho'
version = '3.5'
sourceCompatibility = JavaVersion.VERSION_17;
version = '3.5-pre'
sourceCompatibility = JavaVersion.VERSION_21;
mainClassName = 'com.cadiducho.bot.CadiBotServer'

repositories {
Expand All @@ -18,27 +18,27 @@ repositories {
}

dependencies {
implementation group: 'com.cadiducho', name: 'Zincite', version: '1.7'
implementation group: 'com.cadiducho', name: 'Zincite-JsonModule', version: '1.7'
implementation group: 'com.cadiducho', name: 'Zincite-MessageIDModule', version: '1.5'
implementation group: 'com.cadiducho', name: 'Zincite', version: '1.9'
implementation group: 'com.cadiducho', name: 'Zincite-JsonModule', version: '1.8'
implementation group: 'com.cadiducho', name: 'Zincite-MessageIDModule', version: '1.8'

implementation group: 'com.vdurmont', name: 'emoji-java', version: '5.1.1'
implementation group: 'commons-cli', name: 'commons-cli', version: '1.5.0'
implementation group: 'commons-cli', name: 'commons-cli', version: '1.9.0'

implementation group: 'com.squareup.moshi', name: 'moshi', version: '1.13.0'
implementation group: 'com.squareup.moshi', name: 'moshi-adapters', version: '1.13.0'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '3.3.0'
implementation group: 'com.zaxxer', name: 'HikariCP', version: '5.0.1' // Database
implementation group: 'org.flywaydb', name: 'flyway-core', version: '10.0.0' // Database migrations
implementation group: 'org.flywaydb', name: 'flyway-mysql', version: '10.0.0' // Database migrations
implementation group: 'com.squareup.moshi', name: 'moshi', version: '1.15.1'
implementation group: 'com.squareup.moshi', name: 'moshi-adapters', version: '1.15.1'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.12.0'
implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '3.4.1'
implementation group: 'com.zaxxer', name: 'HikariCP', version: '5.1.0' // Database
implementation group: 'org.flywaydb', name: 'flyway-core', version: '10.17.1' // Database migrations
implementation group: 'org.flywaydb', name: 'flyway-mysql', version: '10.17.1' // Database migrations

//Tests
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.1'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.10.1'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.1'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.7.0'
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.7.0'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.11.0'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.11.0'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.11.0'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.12.0'
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.12.0'
}

test {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
100755 → 100644
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
27 changes: 17 additions & 10 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -83,7 +85,9 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -130,26 +134,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -198,11 +205,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/cadiducho/bot/CadiBotServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.cadiducho.bot.modules.pole.PoleModule;
import com.cadiducho.bot.modules.purabulla.PuraBullaModule;
import com.cadiducho.bot.modules.treintaytres.TreintaYTres;
import com.cadiducho.bot.modules.refranero.Refranero;
import com.cadiducho.zincite.ZinciteBot;
import com.cadiducho.zincite.ZinciteConfig;
import com.cadiducho.zincite.modules.json.JsonModule;
import lombok.Getter;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -100,7 +102,12 @@ private void initCadibot(CommandLine cmd) {
System.err.println(ex.getMessage());
}

this.cadibot = new ZinciteBot(cmd.getOptionValue("token"), ownerId, VERSION);
ZinciteConfig config = ZinciteConfig.builder()
.token(cmd.getOptionValue("token"))
.ownerId(ownerId)
.version(VERSION)
.build();
this.cadibot = new ZinciteBot(config);

cadibot.getModuleManager().registerModule(new CoreModule());
cadibot.getModuleManager().registerModule(new JsonModule());
Expand All @@ -110,6 +117,7 @@ private void initCadibot(CommandLine cmd) {
cadibot.getModuleManager().registerModule(new DesmotivacionesModule());
cadibot.getModuleManager().registerModule(new TreintaYTres());
cadibot.getModuleManager().registerModule(new PuraBullaModule());
cadibot.getModuleManager().registerModule(new Refranero());

cadibot.startServer();
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/cadiducho/bot/modules/refranero/Refranero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.cadiducho.bot.modules.refranero;

import com.cadiducho.telegrambotapi.Update;
import com.cadiducho.telegrambotapi.exception.TelegramException;
import com.cadiducho.telegrambotapi.util.MoshiProvider;
import com.cadiducho.telegrambotapi.Chat;
import com.cadiducho.telegrambotapi.Message;
import com.cadiducho.telegrambotapi.User;
import com.cadiducho.zincite.ZinciteBot;
import com.cadiducho.zincite.api.command.CommandManager;
import com.cadiducho.zincite.api.module.ModuleInfo;
import com.cadiducho.zincite.api.module.ZinciteModule;
import com.cadiducho.zincite.api.command.BotCommand;
import com.cadiducho.zincite.api.command.CommandContext;
import com.cadiducho.zincite.api.command.CommandInfo;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import lombok.Getter;
import lombok.extern.java.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.time.Instant;
import java.util.*;
import java.util.logging.Level;

@Log
@ModuleInfo(name = "refranero", description = "Obtiene un refran del precioso idioma que es el Castellano")
public class Refranero implements ZinciteModule {


@Override
public void onLoad() {
log.info("Cargando módulo refranero");
CommandManager commandManager = ZinciteBot.getInstance().getCommandManager();
commandManager.register(new RefranCMD());
}

@CommandInfo(
aliases = {"/refran", "/refranero", "/sabiduria"},
description = "Obtiene un refran del precioso idioma que es el Castellano"
)
class RefranCMD implements BotCommand {
private final Random rand = new Random();

@Override
public void execute(Chat chat, User user, CommandContext commandContext, Integer integer, Message message, Instant instant) throws TelegramException {
List<String> respuestas = Respuestas.getInstance().getRespuestas();
String reply = respuestas.get(rand.nextInt(respuestas.size()));
getBot().sendMessage(chat.getId(), reply);
}
}
}
65 changes: 65 additions & 0 deletions src/main/java/com/cadiducho/bot/modules/refranero/Respuestas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.cadiducho.bot.modules.refranero;

import com.cadiducho.telegrambotapi.util.MoshiProvider;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Types;
import lombok.Getter;
import lombok.extern.java.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

@Log
final class Respuestas {

private static final String URL_RESPUESTAS = "https://raw.githubusercontent.com/Alonsistas/33/main/refranes.json";

private static final OkHttpClient client = new OkHttpClient();
private static final JsonAdapter<List<String>> adapter = MoshiProvider.getMoshi().adapter(Types.newParameterizedType(List.class, String.class));

@Getter
private List<String> respuestas;

private static Respuestas INSTANCE;

private Respuestas() {
if (INSTANCE != null) throw new UnsupportedOperationException("Cannot create singleton class twice");
INSTANCE = this;
try {
respuestas = obtenerRespuestas();
} catch (IOException e) {
log.log(Level.WARNING, "Error obteniendo respuestas del módulo refranero", e);
respuestas = new ArrayList<>(List.of("no hay refranes :("));
}

}

private static List<String> obtenerRespuestas() throws IOException {
List<String> lista = new ArrayList<>();
Request request = new Request.Builder()
.url(URL_RESPUESTAS)
.build();

try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
lista = adapter.fromJson(response.body().source());
}
return lista;
}

public void refrescar() throws IOException {
respuestas = obtenerRespuestas();
}

public static Respuestas getInstance() {
if (INSTANCE == null) {
INSTANCE = new Respuestas();
}
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
public class Reload33Cmd implements BotCommand {
@Override
public void execute(Chat chat, User user, CommandContext commandContext, Integer integer, Message message, Instant instant) throws TelegramException {
if(chat.getId() != -1001696570609L) getBot().sendMessage(chat.getId(), "No tienes permisos para ejecutar este comando");
if (chat.getId() != -1001696570609L)
getBot().sendMessage(chat.getId(), "No tienes permisos para ejecutar este comando");
else {
try{
try {
Respuestas.getInstance().refrescar();
getBot().sendMessage(chat.getId(), "Frases recargadas");
} catch (IOException e) {
Expand Down
Loading

0 comments on commit e9f3018

Please sign in to comment.