Skip to content

Commit

Permalink
Merge pull request #19 from Softawii/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
FerroEduardo committed Aug 1, 2023
2 parents 6edec14 + e1ffff4 commit ce226bb
Show file tree
Hide file tree
Showing 127 changed files with 330 additions and 46 deletions.
2 changes: 1 addition & 1 deletion discord-bot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Required environment variables
```shell
DISCORD_TOKEN=token MODEL_API_URL=http://localhost:9000 CURUPIRA_RESET=true
DISCORD_TOKEN=token MODEL_API_URL=http://localhost:9000 THRESHOLD=0.5 CURUPIRA_RESET=true
```

## How to run
Expand Down
5 changes: 5 additions & 0 deletions discord-bot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ dependencies {
implementation 'com.github.Softawii:curupira:2658bebc30'
implementation 'com.github.Softawii:curupira:2658bebc30:all'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
implementation ('org.springframework.boot:spring-boot-starter-data-jpa:2.7.0') {
exclude module: 'spring-boot-starter-logging'
}
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'com.h2database:h2:2.1.214'
}

tasks.register('deploy') {
Expand Down
9 changes: 7 additions & 2 deletions discord-bot/src/main/java/com/softawii/saciperere/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.softawii.saciperere;

import com.softawii.saciperere.util.JDAUtil;
import com.softawii.saciperere.util.ModelUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import javax.security.auth.login.LoginException;

@SpringBootApplication
public class Main {
public static ApplicationContext applicationContext;

public static void main(String[] args) throws LoginException, InterruptedException {
// System.out.println(ModelUtil.makeQuestion("quanto se gastou em publicidade ano a ano?", 8));
applicationContext = SpringApplication.run(Main.class, args);
JDAUtil.initJDA();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.softawii.saciperere.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("com.softawii.saciperere.repository")
@PropertySource("classpath:application.properties")
@PropertySource(value = "${spring.config.location}", ignoreResourceNotFound = true)
public class SpringConfig {

private final Environment env;

public SpringConfig(Environment env) {
this.env = env;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.softawii.saciperere.entity", "com.softawii.saciperere.repository", "com.softawii.saciperere.listener");

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());

return em;
}


Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
properties.setProperty("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
return properties;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.softawii.saciperere.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Objects;

@Entity
public class ChannelCategory {
@Id
private Long channelId;
private Long categoryId;

public ChannelCategory() {
}

public ChannelCategory(Long channelId, Long categoryId) {
this.channelId = channelId;
this.categoryId = categoryId;
}

public Long getChannelId() {
return channelId;
}

public void setChannelId(Long channelId) {
this.channelId = channelId;
}

public Long getCategoryId() {
return categoryId;
}

public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChannelCategory that = (ChannelCategory) o;
return channelId.equals(that.channelId) && categoryId.equals(that.categoryId);
}

@Override
public int hashCode() {
return Objects.hash(channelId, categoryId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.softawii.saciperere.listener;

import com.softawii.saciperere.entity.ChannelCategory;
import com.softawii.saciperere.repository.ChannelCategoryRepository;
import com.softawii.saciperere.request.model.ModelResponseBody;
import com.softawii.saciperere.util.ModelUtil;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class ChatListener extends ListenerAdapter {

private final ChannelCategoryRepository repository;

public ChatListener(ChannelCategoryRepository repository) {
this.repository = repository;
}

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (event.getAuthor().isBot()) return;

if (event.isFromGuild()) {
User author = event.getAuthor();
Message message = event.getMessage();
long channelId = message.getGuildChannel().getIdLong();
Optional<ChannelCategory> channelCategoryOptional = repository.findById(channelId);
if (channelCategoryOptional.isPresent()) {
ChannelCategory channelCategory = channelCategoryOptional.get();
String content = message.getContentDisplay();
ModelResponseBody response = ModelUtil.makeQuestion(content, channelCategory.getCategoryId());
if (response.score() >= QuestionGroup.THRESHOLD) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.addField("Pergunta encontrada", response.question(), false);
embedBuilder.addField("Resposta", response.answer(), false);
message.replyEmbeds(embedBuilder.build())
.setActionRow(QuestionGroup.getFeedbackButtons(response.historyId(), author.getIdLong()))
.queue();
}
}
}
}
}
Loading

0 comments on commit ce226bb

Please sign in to comment.