Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add twitter listener #99

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/com/softawii/capivara/config/SpringConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
public JDA jda() {
JDA jda;
try {
JDABuilder builder = JDABuilder.create(discordToken, GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_VOICE_STATES, GatewayIntent.GUILD_EMOJIS_AND_STICKERS, GatewayIntent.GUILD_PRESENCES);
JDABuilder builder = JDABuilder.create(
discordToken,
GatewayIntent.GUILD_MEMBERS,
GatewayIntent.GUILD_VOICE_STATES,
GatewayIntent.GUILD_EMOJIS_AND_STICKERS,
GatewayIntent.GUILD_PRESENCES,
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.MESSAGE_CONTENT);
builder.setMemberCachePolicy(MemberCachePolicy.ALL);
builder.enableCache(CacheFlag.EMOJI, CacheFlag.ROLE_TAGS, CacheFlag.MEMBER_OVERRIDES, CacheFlag.STICKER);
jda = builder.build();
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/softawii/capivara/listeners/TwitterListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.softawii.capivara.listeners;

import net.dv8tion.jda.api.JDA;
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.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class TwitterListener extends ListenerAdapter {
private final String patternStrTwitter;
private final String patternStrX;
public TwitterListener(JDA jda) {
this.patternStrTwitter = "https://twitter.com/(\\w+)/status/(\\d+)";
this.patternStrX = "https://x.com/(\\w+)/status/(\\d+)";
jda.addEventListener(this);
}

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
FerroEduardo marked this conversation as resolved.
Show resolved Hide resolved
if (event.getAuthor().isBot()) return;

String message = event.getMessage().getContentRaw();

if (message.startsWith("https://twitter.com/")){
FerroEduardo marked this conversation as resolved.
Show resolved Hide resolved
message = fixEmbedTwitter(message, patternStrTwitter);
if (message == null) return;
event.getMessage().reply(message).mentionRepliedUser(false).queue();
}

if (message.startsWith("https://x.com/")){
message = fixEmbedTwitter(message, patternStrX);
if (message == null) return;
event.getMessage().reply(message).mentionRepliedUser(false).queue();
FerroEduardo marked this conversation as resolved.
Show resolved Hide resolved
}

}
private static String fixEmbedTwitter(String url, String patternStr) {
Pattern pattern = Pattern.compile(patternStr);
FerroEduardo marked this conversation as resolved.
Show resolved Hide resolved
Matcher matcher = pattern.matcher(url);

if (matcher.find()) {
String userName = matcher.group(1);
String remainingUrl = matcher.group(2);

return String.format("https://fxtwitter.com/%s/status/%s", userName, remainingUrl);
}
return null;
}
}
Loading