mirror of
https://github.com/mii443/BungeeDiscordChat.git
synced 2025-08-22 15:05:29 +00:00
とりあえずコミット!
This commit is contained in:
@ -1,13 +1,74 @@
|
||||
package mc.mec.bungeediscordchat
|
||||
|
||||
import mc.mec.bungeediscordchat.commands.ReplyCommand
|
||||
import mc.mec.bungeediscordchat.commands.TellCommand
|
||||
import mc.mec.bungeediscordchat.japanize.JapanizeType
|
||||
import mc.mec.bungeediscordchat.japanize.Japanizer
|
||||
import net.md_5.bungee.api.ChatColor
|
||||
import net.md_5.bungee.api.ProxyServer
|
||||
import net.md_5.bungee.api.chat.TextComponent
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer
|
||||
import net.md_5.bungee.api.event.ChatEvent
|
||||
import net.md_5.bungee.api.plugin.Listener
|
||||
import net.md_5.bungee.api.plugin.Plugin
|
||||
import net.md_5.bungee.event.EventHandler
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
|
||||
class BungeeDiscordChat : Plugin(), Listener{
|
||||
companion object{
|
||||
private const val prefix = "§b§l[BungeeDiscordChat]§r"
|
||||
}
|
||||
|
||||
var dic = HashMap<String?, String?> ()
|
||||
var lunachat:Boolean = false
|
||||
var discord = DiscordBot()
|
||||
|
||||
class BungeeDiscordChat : Plugin() {
|
||||
override fun onEnable() {
|
||||
// Plugin startup logic
|
||||
for (command in arrayOf(
|
||||
"tell", "msg", "message", "m", "w", "t")) {
|
||||
proxy.pluginManager.registerCommand(
|
||||
this, TellCommand(this, command))
|
||||
}
|
||||
for (command in arrayOf("reply", "r")) {
|
||||
proxy.pluginManager.registerCommand(
|
||||
this, ReplyCommand(this, command))
|
||||
}
|
||||
|
||||
proxy.pluginManager.registerListener(this, this)
|
||||
}
|
||||
|
||||
override fun onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
fun onChat(e: ChatEvent) {
|
||||
val p = e.sender
|
||||
if (p !is ProxiedPlayer) return
|
||||
var message = removeColorCode(e.message)
|
||||
if (lunachat) {
|
||||
val jmsg = Japanizer.japanize(message, JapanizeType.GOOGLE_IME, dic)
|
||||
if (jmsg != "") message += "($jmsg)"
|
||||
}
|
||||
val chatMessage = "${e.sender}@${p.server.info.name}>${message}"
|
||||
for (player in ProxyServer.getInstance().players) {
|
||||
if (player.server.info.name != p.server.info.name) {
|
||||
sendMessage(player.uniqueId, chatMessage)
|
||||
}
|
||||
}
|
||||
if (!e.isCommand || !e.isProxyCommand) {
|
||||
discord.chat(chatMessage)
|
||||
}
|
||||
}
|
||||
|
||||
fun sendMessage(uuid: UUID ,text:String){
|
||||
ProxyServer.getInstance().getPlayer(uuid).sendMessage(TextComponent(text))
|
||||
}
|
||||
|
||||
private fun removeColorCode(msg: String?): String? {
|
||||
return ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', msg))
|
||||
}
|
||||
|
||||
|
||||
}
|
70
src/main/java/mc/mec/bungeediscordchat/ConfigFile.kt
Normal file
70
src/main/java/mc/mec/bungeediscordchat/ConfigFile.kt
Normal file
@ -0,0 +1,70 @@
|
||||
package mc.mec.bungeediscordchat
|
||||
|
||||
import com.google.common.io.ByteStreams
|
||||
import net.md_5.bungee.api.plugin.Plugin
|
||||
import net.md_5.bungee.config.Configuration
|
||||
import net.md_5.bungee.config.ConfigurationProvider
|
||||
import net.md_5.bungee.config.YamlConfiguration
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
|
||||
class ConfigFile(plugin: Plugin) {
|
||||
private var plugin: Plugin = plugin
|
||||
private var file:File
|
||||
private lateinit var config:Configuration
|
||||
|
||||
private val filePatch = "config.yml"
|
||||
init {
|
||||
// Directory
|
||||
if(!plugin?.dataFolder.exists())plugin?.dataFolder.mkdir()
|
||||
this.file = File(plugin?.dataFolder, filePatch)
|
||||
// File
|
||||
if(!file.exists()){
|
||||
try{
|
||||
file.createNewFile()
|
||||
try{
|
||||
val inputStream:InputStream = getResourceAsStream("config.yml")
|
||||
val outputStream: FileOutputStream = FileOutputStream(file)
|
||||
ByteStreams.copy(inputStream,outputStream)
|
||||
}catch (e: IOException){
|
||||
e.printStackTrace()
|
||||
plugin?.logger.warning("Unable to create storage file. $filePatch")
|
||||
}
|
||||
}catch (e: IOException){
|
||||
e.printStackTrace()
|
||||
plugin?.logger.info("failed to create config.yml")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun getResourceAsStream(patch: String): InputStream {
|
||||
return plugin?.getResourceAsStream(patch)
|
||||
}
|
||||
|
||||
fun getConfig():Configuration? {
|
||||
return try {
|
||||
this.config = ConfigurationProvider.getProvider(YamlConfiguration::class.java).load(this.file)
|
||||
this.config
|
||||
}catch (e:IOException){
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun saveConfig(){
|
||||
try {
|
||||
ConfigurationProvider.getProvider(YamlConfiguration::class.java).save(config, File(plugin?.dataFolder, "config.yml"))
|
||||
}catch (e:IOException){
|
||||
e.printStackTrace()
|
||||
plugin?.logger.severe("Couldn't save storage file!")
|
||||
}
|
||||
}
|
||||
|
||||
fun dataFolder():File{
|
||||
return plugin?.dataFolder
|
||||
}
|
||||
|
||||
}
|
87
src/main/java/mc/mec/bungeediscordchat/DiscordBot.kt
Normal file
87
src/main/java/mc/mec/bungeediscordchat/DiscordBot.kt
Normal file
@ -0,0 +1,87 @@
|
||||
package mc.mec.bungeediscordchat
|
||||
|
||||
import net.dv8tion.jda.api.AccountType
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.dv8tion.jda.api.JDABuilder
|
||||
import net.dv8tion.jda.api.entities.Guild
|
||||
import net.dv8tion.jda.api.entities.TextChannel
|
||||
import net.dv8tion.jda.api.events.ReadyEvent
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import javax.security.auth.login.LoginException
|
||||
|
||||
class DiscordBot() : ListenerAdapter() {
|
||||
var plugin: BungeeDiscordChat? = null
|
||||
|
||||
lateinit var jda: JDA
|
||||
var token:String? = null
|
||||
|
||||
var guild: Guild? = null;
|
||||
|
||||
var guildID:Long = 0
|
||||
var chatChannelID:Long = 0
|
||||
var systemChannelID:Long = 0
|
||||
|
||||
var chatChannel: TextChannel? = null
|
||||
var systemChannel:TextChannel? = null
|
||||
|
||||
|
||||
// チャットチャンネル出力
|
||||
fun chat(text:String){
|
||||
|
||||
if (text.indexOf("/") == 0)return
|
||||
|
||||
chatChannel?.sendMessage(text)?.queue()
|
||||
}
|
||||
// システム出力
|
||||
fun system(text:String){
|
||||
systemChannel?.sendMessage(text)?.queue()
|
||||
}
|
||||
|
||||
fun shutdown(){
|
||||
jda.shutdown()
|
||||
plugin?.logger?.info("discord shutdown")
|
||||
}
|
||||
|
||||
fun setup(){
|
||||
plugin?.logger?.info("discord setup")
|
||||
|
||||
if(token == null){
|
||||
plugin?.logger?.info("Discord token is not initialized.")
|
||||
return
|
||||
}
|
||||
try {
|
||||
|
||||
jda = JDABuilder(AccountType.BOT).setToken(token).addEventListeners(this).build()
|
||||
jda.awaitReady()
|
||||
|
||||
guild = jda.getGuildById(this.guildID);
|
||||
chatChannel = guild?.getTextChannelById(this.chatChannelID)
|
||||
systemChannel = guild?.getTextChannelById(this.systemChannelID)
|
||||
|
||||
} catch (e: LoginException) {
|
||||
e.printStackTrace()
|
||||
plugin?.logger?.info(e.localizedMessage)
|
||||
return
|
||||
}
|
||||
plugin?.logger?.info("discord setup done!")
|
||||
}
|
||||
|
||||
fun checkChannel(channel:TextChannel?){
|
||||
if(channel == null){
|
||||
plugin?.logger?.info("channel null")
|
||||
return
|
||||
}
|
||||
plugin?.logger?.info("cantalk:${channel.canTalk()}")
|
||||
|
||||
}
|
||||
|
||||
override fun onReady(event: ReadyEvent) {
|
||||
plugin?.logger?.info("Discord bot ready")
|
||||
}
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
val msg = event.message
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package mc.mec.bungeediscordchat.commands;
|
||||
|
||||
public class ReplyCommand {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package mc.mec.bungeediscordchat.commands;
|
||||
|
||||
public class TellCommand {
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
package mc.mec.bungeediscordchat.japanize;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user