Skip to content

Commit fb4a86a

Browse files
Implement greetings listener for messaging
1 parent 73611c6 commit fb4a86a

5 files changed

Lines changed: 149 additions & 2 deletions

File tree

src/main/java/technobot/TechnoBot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public TechnoBot() throws LoginException {
6767
buttonListener,
6868
musicListener,
6969
new StarboardListener(),
70-
new LevelingListener(this));
70+
new LevelingListener(this),
71+
new GreetingListener());
7172
}
7273

7374
/**

src/main/java/technobot/data/GuildData.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.jetbrains.annotations.NotNull;
66
import technobot.TechnoBot;
77
import technobot.data.cache.Config;
8-
import technobot.data.cache.Suggestion;
98
import technobot.handlers.*;
109

1110
import java.util.HashMap;

src/main/java/technobot/handlers/GreetingHandler.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,69 @@ public void removeGreet() {
5050
bot.database.greetings.updateOne(filter, Updates.unset("greeting"));
5151
}
5252

53+
/**
54+
* Gets the greet message.
55+
*
56+
* @return string greet message.
57+
*/
58+
public String getGreet() {
59+
return greetings.getGreeting();
60+
}
61+
62+
/**
63+
* Set a farewell message for this server.
64+
*
65+
* @param msg the message to send on member leave.
66+
*/
67+
public void setFarewell(String msg) {
68+
greetings.setFarewell(msg);
69+
bot.database.greetings.updateOne(filter, Updates.set("farewell", msg));
70+
}
71+
72+
/**
73+
* Remove farewell message from this server.
74+
*/
75+
public void removeFarewell() {
76+
greetings.setGreeting(null);
77+
bot.database.greetings.updateOne(filter, Updates.unset("farewell"));
78+
}
79+
80+
/**
81+
* Gets the farewell message.
82+
*
83+
* @return string farewell message.
84+
*/
85+
public String getFarewell() {
86+
return greetings.getFarewell();
87+
}
88+
89+
/**
90+
* Set a join DM message for this server.
91+
*
92+
* @param msg the message to send on member leave.
93+
*/
94+
public void setJoinDM(String msg) {
95+
greetings.setJoinDM(msg);
96+
bot.database.greetings.updateOne(filter, Updates.set("join_dm", msg));
97+
}
98+
99+
/**
100+
* Remove join DM message from this server.
101+
*/
102+
public void removeJoinDM() {
103+
greetings.setJoinDM(null);
104+
bot.database.greetings.updateOne(filter, Updates.unset("join_dm"));
105+
}
106+
107+
/**
108+
* Gets the join DM message.
109+
*
110+
* @return string farewell message.
111+
*/
112+
public String getJoinDM() {
113+
return greetings.getJoinDM();
114+
}
115+
53116
/**
54117
* Set the welcome channel.
55118
*
@@ -68,6 +131,20 @@ public void removeChannel() {
68131
bot.database.greetings.updateOne(filter, Updates.unset("welcome_channel"));
69132
}
70133

134+
/**
135+
* Gets the ID of the welcome channel.
136+
*
137+
* @return long id of the welcome channel.
138+
*/
139+
public Long getChannel() {
140+
return greetings.getWelcomeChannel();
141+
}
142+
143+
/**
144+
* Getter for greetings config POJO object.
145+
*
146+
* @return instance of greetings config POJO.
147+
*/
71148
public Greetings getConfig() {
72149
return greetings;
73150
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package technobot.listeners;
2+
3+
import net.dv8tion.jda.api.entities.MessageChannel;
4+
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
5+
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
6+
import net.dv8tion.jda.api.hooks.ListenerAdapter;
7+
import org.jetbrains.annotations.NotNull;
8+
import technobot.data.GuildData;
9+
import technobot.handlers.GreetingHandler;
10+
import technobot.util.placeholders.Placeholder;
11+
import technobot.util.placeholders.PlaceholderFactory;
12+
13+
/**
14+
* Listens for member join and leave to manage greetings.
15+
*
16+
* @author TechnoVision
17+
*/
18+
public class GreetingListener extends ListenerAdapter {
19+
20+
/**
21+
* Triggers every time a user joins a guild.
22+
* Sends a greeting message to the guild's welcome channel.
23+
*/
24+
@Override
25+
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
26+
GreetingHandler greetingHandler = GuildData.get(event.getGuild()).greetingHandler;
27+
// Get welcome channel
28+
Long channelID = greetingHandler.getChannel();
29+
if (channelID == null) return;
30+
31+
// Get greet message
32+
String message = greetingHandler.getGreet();
33+
if (message == null || message.isEmpty()) return;
34+
35+
// Send greet message
36+
Placeholder placeholder = PlaceholderFactory.fromEvent(event).get();
37+
MessageChannel channel = event.getGuild().getChannelById(MessageChannel.class, channelID);
38+
if (channel != null) {
39+
channel.sendMessage(placeholder.parse(message)).queue();
40+
}
41+
42+
// Send join DM message
43+
String joinDM = greetingHandler.getJoinDM();
44+
if (joinDM != null) {
45+
event.getUser().openPrivateChannel().flatMap(dm -> dm.sendMessage(placeholder.parse(joinDM))).queue(null, fail -> { });
46+
}
47+
}
48+
49+
/**
50+
* Triggers every time a user leaves a guild.
51+
* Sends a farewell to the guild's welcome channel.
52+
*/
53+
@Override
54+
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
55+
GreetingHandler greetingHandler = GuildData.get(event.getGuild()).greetingHandler;
56+
if (greetingHandler.getChannel() == null || greetingHandler.getFarewell() == null) return;
57+
58+
// send farewell message
59+
MessageChannel channel = event.getGuild().getChannelById(MessageChannel.class, greetingHandler.getChannel());
60+
if (channel != null) {
61+
Placeholder placeholder = PlaceholderFactory.fromEvent(event).get();
62+
channel.sendMessage(placeholder.parse(greetingHandler.getFarewell())).queue();
63+
}
64+
}
65+
}

src/main/java/technobot/util/placeholders/PlaceholderFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ private static PlaceholderFactory assemble(Placeholder placeholder) {
102102
placeholder.add("username", user.getAsTag());
103103
placeholder.add("user_id", user.getId());
104104
placeholder.add("user_mention", "<@!" + user.getId() + ">");
105+
placeholder.add("mention", "<@!" + user.getId() + ">");
106+
placeholder.add("avatar", user.getEffectiveAvatarUrl());
105107
placeholder.add("user_avatar", user.getEffectiveAvatarUrl());
106108
placeholder.add("user_icon", user.getEffectiveAvatarUrl());
107109
});
@@ -132,6 +134,8 @@ private static PlaceholderFactory assemble(Placeholder placeholder) {
132134
@SuppressWarnings("UnusedReturnValue")
133135
default @NotNull PlaceholderFactory withGuildCapabilities(@NotNull Guild guild) {
134136
accept(placeholder -> {
137+
placeholder.add("server", guild.getName());
138+
placeholder.add("guild", guild.getName());
135139
placeholder.add("guild_name", guild.getName());
136140
placeholder.add("guild_id", guild.getId());
137141
placeholder.add("guild_icon", guild.getIconUrl());
@@ -155,6 +159,7 @@ private static PlaceholderFactory assemble(Placeholder placeholder) {
155159
});
156160
}
157161
accept(placeholder -> {
162+
placeholder.add("channel", channel.getName());
158163
placeholder.add("channel_name", channel.getName());
159164
placeholder.add("channel_id", channel.getId());
160165
placeholder.add("channel_mention", "<#" + channel.getId() + ">");

0 commit comments

Comments
 (0)