-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Softawii/develop
Develop
- Loading branch information
Showing
127 changed files
with
330 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
discord-bot/src/main/java/com/softawii/saciperere/config/SpringConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
discord-bot/src/main/java/com/softawii/saciperere/entity/ChannelCategory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
discord-bot/src/main/java/com/softawii/saciperere/listener/ChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.