From b9c325bb3c3cff57d9dc49378e652e279f99525d Mon Sep 17 00:00:00 2001 From: Xujiayao Date: Thu, 22 Jan 2026 23:08:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EDiscord=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E9=A2=91=E9=81=93ID=E6=88=96=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=BA=94=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/discord/DiscordManager.java | 45 +++++++++++++++++++ core/src/main/resources/lang/en_us.yml | 2 + core/src/main/resources/lang/zh_cn.yml | 2 + 3 files changed, 49 insertions(+) diff --git a/core/src/main/java/com/xujiayao/discord_mc_chat/server/discord/DiscordManager.java b/core/src/main/java/com/xujiayao/discord_mc_chat/server/discord/DiscordManager.java index e2df941a..e2d297f1 100644 --- a/core/src/main/java/com/xujiayao/discord_mc_chat/server/discord/DiscordManager.java +++ b/core/src/main/java/com/xujiayao/discord_mc_chat/server/discord/DiscordManager.java @@ -6,6 +6,7 @@ import com.xujiayao.discord_mc_chat.utils.config.ModeManager; import com.xujiayao.discord_mc_chat.utils.i18n.I18nManager; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; @@ -113,6 +114,50 @@ public class DiscordManager { return false; } + /** + * Sends a message to a Discord channel. + * + * @param channelIdentifier The ID or Name of the channel. + * @param content The message content. + */ + public static void sendMessage(String channelIdentifier, String content) { + TextChannel channel = getTextChannel(channelIdentifier); + if (channel != null) { + channel.sendMessage(content).queue(); + } + } + + /** + * Retrieves a TextChannel by its ID or name. + * + * @param identifier The ID or name of the channel. + * @return The TextChannel if found, null otherwise. + */ + private static TextChannel getTextChannel(String identifier) { + TextChannel tc; + + // Try search by name + // Return first result. Use with caution if multiple channels have the same name. + List channels = jda.getTextChannelsByName(identifier, true); + if (!channels.isEmpty()) { + tc = channels.getFirst(); + } else { + // Try parsing as ID + tc = jda.getTextChannelById(identifier); + if (tc == null) { + LOGGER.error("discord.manager.channel_not_found", identifier); + return null; + } + } + + if (!tc.canTalk()) { + LOGGER.error("discord.manager.channel_cannot_talk", identifier); + return null; + } + + return tc; + } + /** * Shuts down the Discord bot. */ diff --git a/core/src/main/resources/lang/en_us.yml b/core/src/main/resources/lang/en_us.yml index 1ba1bf0a..6d06bb85 100644 --- a/core/src/main/resources/lang/en_us.yml +++ b/core/src/main/resources/lang/en_us.yml @@ -54,6 +54,8 @@ discord: registering_commands: "Registering Discord DMCC commands. This may take a while... (should be within 1 minute)" commands_success: "Discord DMCC commands registered successfully!" commands_failed: "Failed to register Discord DMCC commands." + channel_not_found: "Discord text channel not found: \"{}\"" + channel_cannot_talk: "Cannot send messages in Discord text channel: \"{}\"" command: reply_failed: "Failed to send reply message to Discord command sender." reply_failed_detail: "This is expected behavior for reload/shutdown commands." diff --git a/core/src/main/resources/lang/zh_cn.yml b/core/src/main/resources/lang/zh_cn.yml index f2807220..a696201c 100644 --- a/core/src/main/resources/lang/zh_cn.yml +++ b/core/src/main/resources/lang/zh_cn.yml @@ -54,6 +54,8 @@ discord: registering_commands: "正在注册 Discord DMCC 命令。这可能需要一段时间...(理应在 1 分钟以内)" commands_success: "Discord DMCC 命令注册成功!" commands_failed: "注册 Discord DMCC 命令失败。" + channel_not_found: "未找到 Discord 文字频道:\"{}\"" + channel_cannot_talk: "无法在 Discord 文字频道中发送消息:\"{}\"" command: reply_failed: "无法向 Discord 命令发送者发送回复消息。" reply_failed_detail: "对于 reload/shutdown 命令,这是预期的行为。"