Skip to content

Commit 8d42dbd

Browse files
Update Matchbot to JDA (#2)
* Update pom.xml * Update to work with JDA Update * upgrades * cleaned up config * update jda * use maven properties to fill in plugin details * fix match durations * show server name * Code Cleanup * Embed, README, and bot status fixes --------- Co-authored-by: Pear <20259871+TheRealPear@users.noreply.github.com>
1 parent 1e36bf3 commit 8d42dbd

File tree

9 files changed

+82
-55
lines changed

9 files changed

+82
-55
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This project's structure is a modified version of [Bolty](https://github.com/app
77

88
MatchBot will listen to [`MatchStartEvent`](https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/api/match/event/MatchStartEvent.java) and [`MatchFinishEvent`](https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/api/match/event/MatchFinishEvent.java) to populate a Discord [embed](https://javacord.org/wiki/basic-tutorials/embeds.html#creating-an-embed) with information about a started or finished match.
99

10-
MatchBot is built with [Javacord](https://javacord.org/), an awesome Java library for Discord bots.
10+
MatchBot is built with [JDA](https://jda.wiki/), an awesome Java library for Discord bots.
1111

1212
This bot runs on a single Minecraft server, and is not designed with proxies, networks, or multiple servers in mind.
1313

@@ -49,12 +49,18 @@ You can also find out how to get server, role or channel IDs [here](https://supp
4949
## Config
5050

5151
```yaml
52-
# Discord Stuff
53-
enabled: true # Enable discord bot?
52+
# MatchBot configuration file
53+
# Discord settings
54+
enabled: true # Enable Discord bot?
5455
token: "" # Discord bot token
55-
server: "" # ID of discord server
56+
server: "" # ID of Discord server
57+
match-channel: "" # ID of channel where match embeds will be sent
5658

57-
# ID of channel where match embeds will be sent
58-
match-channel: ""
59+
# Minecraft settings
60+
server-name: "" # Name of the server (useful for networks)
61+
62+
# Image URL to display in the embed's thumbnail if a map image is not found
63+
# Example: https://raw.githubusercontent.com/TBG1000/MapImages/main/map_image_not_found.png
64+
map-image-not-found: ""
5965
```
6066

pom.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<artifactId>MatchBot</artifactId>
77
<version>1.0.0-SNAPSHOT</version>
88
<name>MatchBot</name>
9-
<description>A minecraft to discord java plugin</description>
9+
<description>A Minecraft-to-Discord bot to display PGM match information in a channel.</description>
1010

1111
<!-- Repository Locations -->
1212
<repositories>
@@ -43,12 +43,18 @@
4343
<dependency>
4444
<groupId>net.dv8tion</groupId>
4545
<artifactId>JDA</artifactId>
46-
<version>5.0.0-beta.24</version>
46+
<version>6.2.0</version>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>club.minnced</groupId>
50+
<artifactId>opus-java</artifactId>
51+
</exclusion>
52+
</exclusions>
4753
</dependency>
4854
</dependencies>
4955
<build>
5056
<resources>
51-
<!-- Include the required plugin.yml and config.yml for Bukkit -->
57+
<!-- Include the required files for Bukkit -->
5258
<resource>
5359
<directory>${basedir}/src/main/resources</directory>
5460
<filtering>true</filtering>
@@ -71,7 +77,7 @@
7177
<plugin>
7278
<groupId>org.apache.maven.plugins</groupId>
7379
<artifactId>maven-shade-plugin</artifactId>
74-
<version>3.2.3</version>
80+
<version>3.6.1</version>
7581
<configuration>
7682
<createDependencyReducedPom>false</createDependencyReducedPom>
7783
<minimizeJar>true</minimizeJar>
@@ -123,7 +129,7 @@
123129
<plugin>
124130
<groupId>com.diffplug.spotless</groupId>
125131
<artifactId>spotless-maven-plugin</artifactId>
126-
<version>2.43.0</version>
132+
<version>3.1.0</version>
127133
<configuration>
128134
<ratchetFrom>origin/dev</ratchetFrom>
129135
<java>

src/main/java/me/tbg/match/bot/BotConfig.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class BotConfig {
99
private String token;
1010
private String serverId;
1111
private String matchChannel;
12-
private String fallbackMapImages;
12+
private String serverName;
1313
private String mapImageNotFound;
1414

1515
public BotConfig(Configuration config) {
@@ -21,7 +21,7 @@ public void reload(Configuration config) {
2121
this.token = config.getString("token");
2222
this.serverId = config.getString("server");
2323
this.matchChannel = config.getString("match-channel");
24-
this.fallbackMapImages = config.getString("fallback-map-images");
24+
this.serverName = config.getString("server-name");
2525
this.mapImageNotFound = config.getString("map-image-not-found");
2626
}
2727

@@ -41,9 +41,7 @@ public String getMatchChannel() {
4141
return matchChannel;
4242
}
4343

44-
public String getFallbackMapImages() {
45-
return fallbackMapImages;
46-
}
44+
public String getServerName() { return serverName; }
4745

4846
public String getMapImageNotFound() {
4947
return mapImageNotFound;

src/main/java/me/tbg/match/bot/DiscordBot.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
import net.dv8tion.jda.api.JDABuilder;
1515
import net.dv8tion.jda.api.EmbedBuilder;
1616
import net.dv8tion.jda.api.entities.Activity;
17-
import net.dv8tion.jda.api.entities.TextChannel;
1817
import net.dv8tion.jda.api.entities.Guild;
1918
import net.dv8tion.jda.api.entities.Message;
19+
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
2020
import net.dv8tion.jda.api.requests.GatewayIntent;
21+
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
22+
import net.dv8tion.jda.api.utils.AttachedFile;
23+
import net.dv8tion.jda.api.utils.FileUpload;
24+
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
25+
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
2126
import tc.oc.pgm.api.PGM;
2227
import tc.oc.pgm.api.Permissions;
2328
import tc.oc.pgm.api.map.Contributor;
@@ -79,12 +84,18 @@ public void disable() {
7984

8085
public void sendMatchEmbed(EmbedBuilder embed, Match match) {
8186
if (api != null) {
82-
api.getPresence().setActivity(Activity.playing(match.getMap().getName()));
87+
api.getPresence().setActivity(Activity.playing("Playing " + match.getMap().getName() + " on " + config.getServerName()));
88+
if (config.getServerName().isEmpty()) {
89+
api.getPresence().setActivity(Activity.playing("Playing " + match.getMap().getName()));
90+
}
8391
Guild guild = api.getGuildById(config.getServerId());
8492
if (guild != null) {
8593
TextChannel textChannel = guild.getTextChannelById(config.getMatchChannel());
8694
if (textChannel != null) {
87-
textChannel.sendMessageEmbeds(embed.build()).queue(message -> {
95+
File imgFile = new File(match.getMap().getSource().getAbsoluteDir().toFile(), "map.png");
96+
MessageCreateAction messageAction = (imgFile.exists()) ? textChannel.sendFiles(FileUpload.fromData(imgFile, "map.png")).setEmbeds(embed.build())
97+
: textChannel.sendMessageEmbeds(embed.build());
98+
messageAction.queue(message -> {
8899
matchMessageMap.put(Long.valueOf(match.getId()), message.getIdLong());
89100
});
90101
}
@@ -108,26 +119,22 @@ public void editMatchEmbed(long matchId, EmbedBuilder newEmbed) {
108119
}
109120

110121
public EmbedBuilder setEmbedThumbnail(MapInfo map, EmbedBuilder embed, DiscordBot bot) {
111-
try {
112-
embed.setThumbnail(bot.getMapImage(map));
113-
return embed;
114-
} catch (IOException e) {
115-
if (!bot.getConfig().getFallbackMapImages().isEmpty()) {
116-
String mapName = map.getName().replace(" ", "%20");
117-
embed.setThumbnail(bot.getConfig().getFallbackMapImages() + mapName + "/map.png");
118-
return embed;
119-
} else if (!bot.getConfig().getMapImageNotFound().isEmpty()) {
122+
File imgFile = new File(map.getSource().getAbsoluteDir().toFile(), "map.png");
123+
if (!imgFile.exists()) {
124+
if (!bot.getConfig().getMapImageNotFound().isEmpty()) {
120125
embed.setThumbnail(bot.getConfig().getMapImageNotFound());
121126
return embed;
122127
}
128+
return embed;
123129
}
130+
embed.setThumbnail("attachment://map.png");
124131
return embed;
125132
}
126133

127134
public String parseDuration(Duration duration) {
128135
long hours = duration.toHours();
129-
long minutes = duration.toMinutes();
130-
long seconds = duration.getSeconds();
136+
long minutes = duration.toMinutes() % 60;
137+
long seconds = duration.getSeconds() % 60;
131138

132139
StringBuilder result = new StringBuilder();
133140

@@ -176,12 +183,6 @@ public String getMatchDescription(Match match) {
176183
+ (playerCount == 1 ? " player" : " players") + "** online.";
177184
}
178185

179-
public BufferedImage getMapImage(MapInfo map) throws IOException {
180-
Path sourceDir = map.getSource().getAbsoluteDir();
181-
File pngFile = new File(sourceDir.toFile(), "map.png");
182-
return ImageIO.read(pngFile);
183-
}
184-
185186
public long getOnlineStaffCount(Match match) {
186187
// Adapted from
187188
// https://github.com/PGMDev/PGM/blob/dev/core/src/main/java/tc/oc/pgm/command/MapCommand.java

src/main/java/me/tbg/match/bot/MatchFinishListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public MatchFinishListener(DiscordBot bot) {
2525

2626
@EventHandler
2727
public void onMatchFinish(MatchFinishEvent event) {
28+
BotConfig config = bot.getConfig();
2829
Match match = event.getMatch();
2930
MapInfo map = match.getMap();
3031
ScoreMatchModule scoreModule = match.getModule(ScoreMatchModule.class);
@@ -34,7 +35,7 @@ public void onMatchFinish(MatchFinishEvent event) {
3435
Color winnerColor = getWinnerColor(event);
3536

3637
EmbedBuilder matchFinishEmbed =
37-
createMatchFinishEmbed(match, map, winner, winnerColor, scoreModule, teamModule);
38+
createMatchFinishEmbed(config, match, map, winner, winnerColor, scoreModule, teamModule);
3839

3940
bot.editMatchEmbed(Long.parseLong(match.getId()), matchFinishEmbed);
4041
}
@@ -58,6 +59,7 @@ private Color getWinnerColor(MatchFinishEvent event) {
5859
}
5960

6061
private EmbedBuilder createMatchFinishEmbed(
62+
BotConfig config,
6163
Match match,
6264
MapInfo map,
6365
String winner,
@@ -98,7 +100,7 @@ private EmbedBuilder createMatchFinishEmbed(
98100
.addField("Participants", String.valueOf(match.getParticipants().size()), true)
99101
.addField("Observers", String.valueOf(match.getDefaultParty().getPlayers().size()), true)
100102
.addField("Staff", String.valueOf(bot.getOnlineStaffCount(match)), true)
101-
.setFooter("Map tags: " + map.getTags().toString());
103+
.setFooter((!config.getServerName().isEmpty() ? "Server: " + config.getServerName() + " • " : "") + "Map tags: " + map.getTags().toString());
102104

103105
bot.setEmbedThumbnail(map, embed, bot);
104106

src/main/java/me/tbg/match/bot/MatchStartListener.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public MatchStartListener(DiscordBot bot) {
2323
public void onMatchStart(MatchStartEvent event) {
2424
Match match = event.getMatch();
2525
MapInfo map = match.getMap();
26-
EmbedBuilder matchStartEmbed = createMatchStartEmbed(match, map);
26+
EmbedBuilder matchStartEmbed = createMatchStartEmbed(match, map, bot.getConfig());
2727

2828
bot.setEmbedThumbnail(map, matchStartEmbed, bot);
2929
bot.sendMatchEmbed(matchStartEmbed, match);
@@ -33,7 +33,7 @@ public void onMatchStart(MatchStartEvent event) {
3333
match.getPlayers().size());
3434
}
3535

36-
private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map) {
36+
private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map, BotConfig config) {
3737
return new EmbedBuilder()
3838
.setColor(Color.WHITE.getRGB())
3939
.setTitle("Match #" + match.getId() + " has started!")
@@ -45,6 +45,8 @@ private EmbedBuilder createMatchStartEmbed(Match match, MapInfo map) {
4545
.addField("Pools", bot.getMapPools(match), true)
4646
.addField("Objective", map.getDescription(), false)
4747
.addField("Participants", String.valueOf(match.getParticipants().size()), true)
48-
.addField("Observers", String.valueOf(match.getDefaultParty().getPlayers().size()), true);
48+
.addField("Observers", String.valueOf(match.getDefaultParty().getPlayers().size()), true)
49+
.setFooter((!config.getServerName().isEmpty() ? "Server: " + config.getServerName() + " • " : "") + "Map tags: " + map.getTags().toString());
50+
4951
}
5052
}

src/main/resources/config.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
# Discord Stuff
2-
enabled: true # Enable discord bot?
1+
# MatchBot configuration file
2+
# Discord settings
3+
enabled: true # Enable Discord bot?
34
token: "" # Discord bot token
4-
server: "" # ID of discord server
5+
server: "" # ID of Discord server
6+
match-channel: "" # ID of channel where match embeds will be sent
57

6-
# ID of channel where match embeds will be sent
7-
match-channel: ""
8+
# Minecraft settings
9+
server-name: "" # Name of the server (useful for networks)
810

9-
# Fallback URL for map.png images
10-
# This will be used in case no map.png is found the map's directory
11-
# Example: https://raw.githubusercontent.com/TBG1000/MapImages/main/Maps/
12-
fallback-map-images: ""
13-
14-
# Image URL to display in the embed's thumbnail if no image is found
11+
# Image URL to display in the embed's thumbnail if a map image is not found
1512
# Example: https://raw.githubusercontent.com/TBG1000/MapImages/main/map_image_not_found.png
1613
map-image-not-found: ""
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: ${project.name}
2+
description: ${project.description}
3+
version: '${project.version}'
4+
api-version: '1.21.10'
5+
main: me.tbg.match.bot.MatchBot
6+
author: TBG1000
7+
website: https://github.com/TBG1000/MatchBot
8+
9+
dependencies:
10+
server:
11+
PGM:
12+
load: BEFORE
13+
required: true
14+
join-classpath: true

src/main/resources/plugin.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
name: MatchBot
2-
main: me.tbg.match.bot.MatchBot
3-
description: A Minecraft to Discord bot. Display PGM match information in Discord.
1+
name: ${project.name}
2+
description: ${project.description}
43
version: ${project.version}
4+
main: me.tbg.match.bot.MatchBot
55
author: TBG1000
6+
website: https://github.com/TBG1000/MatchBot
67
depend: [PGM]

0 commit comments

Comments
 (0)