mirror of
https://github.com/System-End/Discord-MC-Chat.git
synced 2026-04-19 22:05:11 +00:00
新增Discord消息发送功能,支持通过频道ID或名称发送消息,并添加相应的错误提示
This commit is contained in:
parent
3874b6f04f
commit
b9c325bb3c
3 changed files with 49 additions and 0 deletions
|
|
@ -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<TextChannel> 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.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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."
|
||||
|
|
|
|||
|
|
@ -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 命令,这是预期的行为。"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue