Skip to content

Commit

Permalink
add event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ElinaZoldnere committed Jul 4, 2024
1 parent 7be15d7 commit 3e6b9be
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
11 changes: 0 additions & 11 deletions src/main/java/com/my/discordbot/Bot.java

This file was deleted.

14 changes: 7 additions & 7 deletions src/main/java/com/my/discordbot/BotInitializer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.my.discordbot;

import net.dv8tion.jda.api.JDABuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import net.dv8tion.jda.api.JDABuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class BotInitializer {

@Autowired
private Bot bot;
private EventListener eventListener;

@Value("")
@Value("${DISCORD_BOT_TOKEN}")
private String discordBotToken;

public void initializeBot() {
JDABuilder.createLight(discordBotToken)
.addEventListeners(bot)
.addEventListeners(eventListener)
.build();
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/my/discordbot/DiscordBotApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package com.my.discordbot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DiscordBotApplication {
public class DiscordBotApplication implements CommandLineRunner {

@Autowired
private BotInitializer botInitializer;

public static void main(String[] args) {
SpringApplication.run(DiscordBotApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
botInitializer.initializeBot();
}

}
36 changes: 36 additions & 0 deletions src/main/java/com/my/discordbot/EventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.my.discordbot;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EventListener extends ListenerAdapter {

@Autowired
private ProcessConversionToJpg processConversion;

@Override
public void onMessageReceived(MessageReceivedEvent event) {
Message message = event.getMessage();
MessageChannel channel = event.getChannel();

if (channel instanceof TextChannel) {
if (!message.getAuthor().isBot() && message.getAttachments().size() > 0) {
message.getAttachments().forEach(attachment -> {
if (attachment.getFileName().endsWith(".heic")) {
processConversion.process(attachment, channel);
}
});
}
}
}

}


22 changes: 22 additions & 0 deletions src/main/java/com/my/discordbot/ProcessConversionToJpg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.my.discordbot;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ProcessConversionToJpg {

@Value("")
private String cloudFunctionUrl;

@Value("")
private String secretToken;

@Value("")
private String ownerId;

public void process(Message.Attachment attachment, MessageChannel channel) {}

}
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# sensitive properties
# sensitive properties, stored alternatively in .env

# DISCORD_BOT_TOKEN=${DISCORD_BOT_TOKEN}
# OWNER_ID=${OWNER_ID}
# CLOUD_FUNCTION_URL=${CLOUD_FUNCTION_URL}
Expand Down

0 comments on commit 3e6b9be

Please sign in to comment.