Discord Bot Rewrite, Upgrades & Plugin Migration
- Replace deprecated Core Javacord implementation with a modern JDA 5.x Kotlin plugin
- Introduce event-driven architecture via ServerConfigEvent, ServerStartupEvent, ServerShutdownEvent, and CommandEvent
- Implemented global event posting functionality in Player class.
- Refactored PluginService to handle potential ClassNotFoundException more gracefully.
- Improved CustomPrintStream to better identify the caller context in log messages.
- Refactor server configuration into modular groups ("core" and "discord") with backward compatibility for legacy flat keys
This commit is contained in:
1 parent
87fece4854
commit
fbc3595da1
49 files changed
+1151
-665
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.Constants
|
||||
import com.rs2.game.players.PlayerHandler
|
||||
import net.dv8tion.jda.api.entities.Activity
|
||||
|
||||
object DiscordActivity {
|
||||
|
||||
private var lastActivityText: String? = null
|
||||
|
||||
fun getCurrentActivityText(): String {
|
||||
return if (DiscordConfig.displayPlayerCount) {
|
||||
val count = PlayerHandler.getPlayerCount()
|
||||
if (count == 1) "1 Player Online" else "$count Players Online"
|
||||
} else {
|
||||
Constants.WEBSITE_LINK
|
||||
}
|
||||
}
|
||||
|
||||
fun updateActivity(force: Boolean = false) {
|
||||
try {
|
||||
val jda = DiscordBot.jda ?: return
|
||||
val activityText = getCurrentActivityText()
|
||||
if (force || activityText != lastActivityText) {
|
||||
jda.presence.activity = Activity.playing(activityText)
|
||||
println("Discord Activity Updated: $activityText")
|
||||
lastActivityText = activityText
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Could not set Discord activity: $e")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.Constants
|
||||
import com.rs2.game.players.Client
|
||||
import com.rs2.game.players.PlayerHandler
|
||||
import net.dv8tion.jda.api.JDA
|
||||
import net.dv8tion.jda.api.JDABuilder
|
||||
import net.dv8tion.jda.api.OnlineStatus
|
||||
import net.dv8tion.jda.api.Permission
|
||||
import net.dv8tion.jda.api.entities.Activity
|
||||
import net.dv8tion.jda.api.entities.Member
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent
|
||||
import plugin.integration.discord.commands.*
|
||||
import plugin.integration.discord.commands.admin.*
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.ScheduledExecutorService
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object DiscordBot {
|
||||
|
||||
var commandPrefix: String = "::w" + Constants.WORLD
|
||||
var jda: JDA? = null
|
||||
|
||||
private var activityScheduler: ScheduledExecutorService? = null
|
||||
|
||||
fun isAdmin(member: Member?): Boolean {
|
||||
return member?.hasPermission(Permission.ADMINISTRATOR) == true
|
||||
}
|
||||
|
||||
fun findOnlinePlayer(name: String): Client? {
|
||||
return PlayerHandler.players.filterNotNull().firstOrNull {
|
||||
it.playerName != null && it.playerName.equals(name, ignoreCase = true)
|
||||
} as? Client
|
||||
}
|
||||
|
||||
fun init() {
|
||||
val currentToken = DiscordConfig.botToken
|
||||
if (currentToken.isNotEmpty()) {
|
||||
Executors.newSingleThreadExecutor { r ->
|
||||
Thread(r, "DiscordBot-Init").apply { isDaemon = true }
|
||||
}.execute {
|
||||
try {
|
||||
val listeners = mutableListOf<Any>(
|
||||
Online(),
|
||||
Players(),
|
||||
GameKick(),
|
||||
MoveHome(),
|
||||
Update()
|
||||
)
|
||||
if (Constants.WORLD == 1) {
|
||||
listeners.addAll(
|
||||
listOf(
|
||||
Commands(),
|
||||
AdminCommands(),
|
||||
Forum(),
|
||||
Issues(),
|
||||
Link(),
|
||||
Website(),
|
||||
Purge(),
|
||||
WelcomeMessage()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val builder = JDABuilder.createDefault(currentToken)
|
||||
.enableIntents(
|
||||
GatewayIntent.GUILD_MESSAGES,
|
||||
GatewayIntent.DIRECT_MESSAGES,
|
||||
GatewayIntent.MESSAGE_CONTENT,
|
||||
GatewayIntent.GUILD_MEMBERS
|
||||
)
|
||||
.setStatus(OnlineStatus.ONLINE)
|
||||
.setActivity(Activity.playing(DiscordActivity.getCurrentActivityText()))
|
||||
.addEventListeners(*listeners.toTypedArray())
|
||||
|
||||
val builtJda = builder.build()
|
||||
builtJda.awaitReady()
|
||||
jda = builtJda
|
||||
startActivityScheduler()
|
||||
DiscordActivity.updateActivity(force = true)
|
||||
println("Discord Bot Successfully Connected & Online as: ${builtJda.selfUser.asTag}")
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Failed to start Discord Bot (JDA 5): ${e.message}")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println("Discord Token Not Set So Bot Not Loaded")
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun startActivityScheduler() {
|
||||
if (activityScheduler == null || activityScheduler!!.isShutdown) {
|
||||
val scheduler = Executors.newSingleThreadScheduledExecutor { r ->
|
||||
Thread(r, "DiscordActivityUpdater").apply { isDaemon = true }
|
||||
}
|
||||
activityScheduler = scheduler
|
||||
scheduler.scheduleAtFixedRate({
|
||||
try {
|
||||
DiscordActivity.updateActivity()
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Error updating Discord activity: ${e.message}")
|
||||
}
|
||||
}, 10, 15, TimeUnit.SECONDS)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun stopActivityScheduler() {
|
||||
activityScheduler?.shutdownNow()
|
||||
activityScheduler = null
|
||||
}
|
||||
|
||||
fun shutdown() {
|
||||
stopActivityScheduler()
|
||||
jda?.let {
|
||||
try {
|
||||
it.shutdown()
|
||||
println("Discord Bot Disconnected.")
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Error disconnecting Discord Bot: ${e.message}")
|
||||
}
|
||||
jda = null
|
||||
}
|
||||
}
|
||||
|
||||
fun sendMessage(channelName: String, msg: String) {
|
||||
try {
|
||||
jda?.let { currentJda ->
|
||||
val channels = currentJda.getTextChannelsByName(channelName, true)
|
||||
if (channels.isNotEmpty()) {
|
||||
channels[0].sendMessage(msg).queue()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Error sending message to channel $channelName: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
fun sendLog(msg: String) {
|
||||
try {
|
||||
if (DiscordConfig.logChannelId.isNotEmpty()) {
|
||||
val channel = jda?.getTextChannelById(DiscordConfig.logChannelId)
|
||||
channel?.sendMessage(msg)?.queue()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Error sending Discord log message: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.event.EventContext
|
||||
import com.rs2.event.EventSubscriber
|
||||
import com.rs2.event.SubscribesTo
|
||||
import com.rs2.event.impl.CommandEvent
|
||||
import com.rs2.game.players.Player
|
||||
|
||||
@SubscribesTo(CommandEvent::class)
|
||||
class DiscordCommandSubscriber : EventSubscriber<CommandEvent> {
|
||||
|
||||
override fun subscribe(context: EventContext, player: Player?, event: CommandEvent) {
|
||||
if (player == null) {
|
||||
return
|
||||
}
|
||||
|
||||
when (event.name.lowercase()) {
|
||||
"link" -> {
|
||||
val args = event.arguments
|
||||
if (args.isNotEmpty()) {
|
||||
player.discordCode = args[0]
|
||||
player.packetSender.sendMessage("Your Account has now been linked with Discord User ID:")
|
||||
player.packetSender.sendMessage(player.discordCode)
|
||||
} else {
|
||||
player.packetSender.sendMessage("Usage: ::link <discord-user-id>")
|
||||
}
|
||||
context.breakSubscriberChain()
|
||||
}
|
||||
"stuck" -> {
|
||||
DiscordBot.sendLog("${player.playerName} used ::stuck at X/Y: ${player.absX}/${player.absY}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.event.EventContext
|
||||
import com.rs2.event.EventSubscriber
|
||||
import com.rs2.event.SubscribesTo
|
||||
import com.rs2.event.impl.ServerConfigEvent
|
||||
import com.rs2.game.players.Player
|
||||
|
||||
/**
|
||||
* Dedicated subscriber that registers and loads all Discord configuration keys
|
||||
* from the active server configuration file via [ServerConfigEvent].
|
||||
*/
|
||||
@SubscribesTo(ServerConfigEvent::class)
|
||||
class DiscordConfig : EventSubscriber<ServerConfigEvent> {
|
||||
|
||||
companion object {
|
||||
var botToken: String = ""
|
||||
var welcomeChannelId: String = ""
|
||||
var logChannelId: String = ""
|
||||
var displayPlayerCount: Boolean = false
|
||||
}
|
||||
|
||||
override fun subscribe(context: EventContext, player: Player?, event: ServerConfigEvent) {
|
||||
event.group("discord") { g ->
|
||||
g.registerString("token", botToken, "discord_bot_token") { botToken = it }
|
||||
g.registerString("welcome_channel_id", welcomeChannelId, "discord_bot_welcome_channel_id") { welcomeChannelId = it }
|
||||
g.registerString("log_channel_id", logChannelId, "discord_bot_log_channel_id") { logChannelId = it }
|
||||
g.registerBoolean("player_count", displayPlayerCount, "discord_bot_player_count") { displayPlayerCount = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.event.EventContext
|
||||
import com.rs2.event.EventSubscriber
|
||||
import com.rs2.event.SubscribesTo
|
||||
import com.rs2.event.impl.ServerShutdownEvent
|
||||
import com.rs2.game.players.Player
|
||||
|
||||
@SubscribesTo(ServerShutdownEvent::class)
|
||||
class DiscordShutdownSubscriber : EventSubscriber<ServerShutdownEvent> {
|
||||
|
||||
override fun subscribe(context: EventContext, player: Player?, event: ServerShutdownEvent) {
|
||||
try {
|
||||
DiscordBot.shutdown()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.event.EventContext
|
||||
import com.rs2.event.EventSubscriber
|
||||
import com.rs2.event.SubscribesTo
|
||||
import com.rs2.event.impl.ServerStartupEvent
|
||||
import com.rs2.game.players.Player
|
||||
|
||||
@SubscribesTo(ServerStartupEvent::class)
|
||||
class DiscordStartupSubscriber : EventSubscriber<ServerStartupEvent> {
|
||||
|
||||
override fun subscribe(context: EventContext, player: Player?, event: ServerStartupEvent) {
|
||||
try {
|
||||
DiscordBot.init()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package plugin.integration.discord
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
|
||||
class WelcomeMessage : ListenerAdapter() {
|
||||
|
||||
override fun onGuildMemberJoin(event: GuildMemberJoinEvent) {
|
||||
try {
|
||||
if (DiscordConfig.welcomeChannelId.isNotEmpty()) {
|
||||
val channel = event.guild.getTextChannelById(DiscordConfig.welcomeChannelId)
|
||||
channel?.sendMessage("Hello ${event.user.asMention}, and welcome to the ${Constants.SERVER_NAME} Discord server!")?.queue()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
System.err.println("Failed to send welcome message: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
import java.awt.Color
|
||||
|
||||
class Commands : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::commands", ignoreCase = true) || content.equals("::command", ignoreCase = true)) {
|
||||
val embed = EmbedBuilder()
|
||||
.setTitle("⚔️ ${Constants.SERVER_NAME} Discord Commands")
|
||||
.setColor(Color(0x4A, 0x90, 0xE2))
|
||||
.setDescription("Available commands for the ${Constants.SERVER_NAME} Discord bot:")
|
||||
.addField(
|
||||
"🌐 Global Commands",
|
||||
"""
|
||||
`::commands` — Displays this list of available commands
|
||||
`::link` — Instructions to link your Discord account in-game
|
||||
`::forum` / `::forums` — Link to the official community forums
|
||||
`::website` / `::site` — Link to the official 2006Scape website
|
||||
`::issues` / `::bugs` — Link to report bugs and issues on GitHub
|
||||
`::admincommands` — View administrator commands (Staff only)
|
||||
""".trimIndent(),
|
||||
false
|
||||
)
|
||||
.addField(
|
||||
"🌍 World Commands (Prefix: `::w<world_id>`)",
|
||||
"""
|
||||
*Target a specific world using `::w1`, `::w2`, etc.:*
|
||||
`::w<id> online` — Check if that world is online (e.g. `::w1 online`)
|
||||
`::w<id> players` — View player and staff counts (e.g. `::w1 players`)
|
||||
`::w<id> gamekick <player>` — Kick your character (requires linked account)
|
||||
""".trimIndent(),
|
||||
false
|
||||
)
|
||||
.setFooter("World commands require the world number prefix (e.g. ::w1 for World 1, ::w2 for World 2)")
|
||||
.build()
|
||||
|
||||
event.channel.sendMessageEmbeds(embed).queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
|
||||
class Forum : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::forum", ignoreCase = true) || content.equals("::forums", ignoreCase = true)) {
|
||||
event.channel.sendMessage("${Constants.WEBSITE_LINK}/forums/index.php").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
|
||||
class Issues : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::issues", ignoreCase = true) || content.equals("::bugs", ignoreCase = true)) {
|
||||
event.channel.sendMessage("https://github.com/2006-Scape/2006Scape/issues").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
|
||||
class Link : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::link", ignoreCase = true)) {
|
||||
event.channel.sendMessage("${event.author.asMention}, Please check your DM's to continue.").queue()
|
||||
event.author.openPrivateChannel().queue { privateChannel ->
|
||||
privateChannel.sendMessage("Please copy/paste the following in-game to link your Discord account: \n ```::link ${event.author.id}```").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class Online : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("${DiscordBot.commandPrefix} online", ignoreCase = true)) {
|
||||
event.channel.sendMessage(":tada: ${Constants.SERVER_NAME} World:${Constants.WORLD} is Online! :tada:").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import com.rs2.game.players.PlayerHandler
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class Players : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("${DiscordBot.commandPrefix} players", ignoreCase = true)) {
|
||||
val count = PlayerHandler.getPlayerCount()
|
||||
val staff = PlayerHandler.getNonPlayerCount()
|
||||
if (count != 1) {
|
||||
event.channel.sendMessage("There are currently $count players online ($staff staff online).").queue()
|
||||
} else {
|
||||
event.channel.sendMessage("There is currently $count player online ($staff staff online).").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package plugin.integration.discord.commands
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
|
||||
class Website : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::website", ignoreCase = true) || content.equals("::site", ignoreCase = true)) {
|
||||
event.channel.sendMessage(Constants.WEBSITE_LINK).queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package plugin.integration.discord.commands.admin
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.EmbedBuilder
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
import java.awt.Color
|
||||
|
||||
class AdminCommands : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
if (content.equals("::admincommands", ignoreCase = true) || content.equals("::admincommand", ignoreCase = true)) {
|
||||
if (DiscordBot.isAdmin(event.member)) {
|
||||
val embed = EmbedBuilder()
|
||||
.setTitle("🛡️ ${Constants.SERVER_NAME} Administrator Commands")
|
||||
.setColor(Color(0xE7, 0x4C, 0x3C))
|
||||
.setDescription("Staff commands for server administration:")
|
||||
.addField(
|
||||
"Commands",
|
||||
"""
|
||||
`::purge <1-50>` — Purges specified amount of messages from Discord channel
|
||||
`::w<id> gamekick <player>` — Disconnects a player from specified world
|
||||
`::w<id> movehome <player>` — Teleports player to Lumbridge
|
||||
`::w<id> update <seconds>` — Triggers server update countdown on specified world
|
||||
""".trimIndent(),
|
||||
false
|
||||
)
|
||||
.setFooter("World-specific commands require the world number prefix (e.g. ::w1 or ::w2)")
|
||||
.build()
|
||||
|
||||
event.channel.sendMessageEmbeds(embed).queue()
|
||||
} else {
|
||||
event.channel.sendMessage("You do not have permission to perform this command.").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package plugin.integration.discord.commands.admin
|
||||
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class GameKick : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
val prefix = "${DiscordBot.commandPrefix.lowercase()} gamekick"
|
||||
val lower = content.lowercase()
|
||||
|
||||
if (!lower.startsWith(prefix)) {
|
||||
return
|
||||
}
|
||||
|
||||
val playerToKick = content.substring(prefix.length).trim()
|
||||
if (playerToKick.isEmpty()) {
|
||||
event.channel.sendMessage("Usage: `${DiscordBot.commandPrefix} gamekick <player-name>`").queue()
|
||||
return
|
||||
}
|
||||
|
||||
val client = DiscordBot.findOnlinePlayer(playerToKick)
|
||||
if (client == null) {
|
||||
event.channel.sendMessage("Player **$playerToKick** is not online.").queue()
|
||||
return
|
||||
}
|
||||
|
||||
if (DiscordBot.isAdmin(event.member) || event.author.id == client.discordCode) {
|
||||
event.channel.sendMessage("${client.playerName} was kicked by ${event.author.effectiveName}.").queue()
|
||||
client.disconnected = true
|
||||
client.logout(true)
|
||||
} else {
|
||||
event.channel.sendMessage("You do not have permission to kick this player.").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package plugin.integration.discord.commands.admin
|
||||
|
||||
import com.rs2.Constants
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class MoveHome : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
val prefix = "${DiscordBot.commandPrefix.lowercase()} movehome"
|
||||
val lower = content.lowercase()
|
||||
|
||||
if (!lower.startsWith(prefix)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!DiscordBot.isAdmin(event.member)) {
|
||||
event.channel.sendMessage("You do not have permission to perform this command.").queue()
|
||||
return
|
||||
}
|
||||
|
||||
val playerToMove = content.substring(prefix.length).trim()
|
||||
if (playerToMove.isEmpty()) {
|
||||
event.channel.sendMessage("Usage: `${DiscordBot.commandPrefix} movehome <player-name>`").queue()
|
||||
return
|
||||
}
|
||||
|
||||
val client = DiscordBot.findOnlinePlayer(playerToMove)
|
||||
if (client == null) {
|
||||
event.channel.sendMessage("Player **$playerToMove** is not online.").queue()
|
||||
return
|
||||
}
|
||||
|
||||
event.channel.sendMessage("${client.playerName} has been moved to Lumbridge.").queue()
|
||||
client.playerAssistant.movePlayer(Constants.RESPAWN_X, Constants.RESPAWN_Y, 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package plugin.integration.discord.commands.admin
|
||||
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class Purge : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
val lower = content.lowercase()
|
||||
if (lower == "::purge" || lower.startsWith("::purge ")) {
|
||||
if (DiscordBot.isAdmin(event.member)) {
|
||||
val messagesStr = content.substring(7).trim()
|
||||
val messagesToPurge = messagesStr.toIntOrNull()
|
||||
if (messagesToPurge == null || messagesToPurge <= 0) {
|
||||
event.channel.sendMessage("Usage: `::purge <1-50>`").queue()
|
||||
return
|
||||
}
|
||||
if (messagesToPurge > 50) {
|
||||
event.channel.sendMessage("Can't purge more than 50 messages at once.").queue()
|
||||
return
|
||||
}
|
||||
val channel = event.channel
|
||||
if (channel is GuildMessageChannel) {
|
||||
channel.getHistoryBefore(event.messageId, messagesToPurge).queue({ history ->
|
||||
channel.purgeMessages(history.retrievedHistory)
|
||||
channel.sendMessage("Purging $messagesToPurge Messages.").queue()
|
||||
}, { e ->
|
||||
System.err.println("Failed to retrieve history for purge: ${e.message}")
|
||||
})
|
||||
}
|
||||
} else {
|
||||
event.channel.sendMessage("You do not have permission to perform this command.").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package plugin.integration.discord.commands.admin
|
||||
|
||||
import com.rs2.game.players.PlayerHandler
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter
|
||||
import plugin.integration.discord.DiscordBot
|
||||
|
||||
class Update : ListenerAdapter() {
|
||||
|
||||
override fun onMessageReceived(event: MessageReceivedEvent) {
|
||||
if (event.author.isBot) {
|
||||
return
|
||||
}
|
||||
val content = event.message.contentRaw
|
||||
val prefix = "${DiscordBot.commandPrefix.lowercase()} update"
|
||||
val lower = content.lowercase()
|
||||
|
||||
if (!lower.startsWith(prefix)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!DiscordBot.isAdmin(event.member)) {
|
||||
event.channel.sendMessage("You do not have permission to perform this command.").queue()
|
||||
return
|
||||
}
|
||||
|
||||
val secondsStr = content.substring(prefix.length).trim()
|
||||
if (secondsStr.isEmpty()) {
|
||||
event.channel.sendMessage("Usage: `${DiscordBot.commandPrefix} update <seconds>`").queue()
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
val seconds = secondsStr.toInt()
|
||||
PlayerHandler.updateSeconds = seconds
|
||||
PlayerHandler.updateAnnounced = false
|
||||
PlayerHandler.updateRunning = true
|
||||
PlayerHandler.updateStartTime = System.currentTimeMillis()
|
||||
event.channel.sendMessage("Server update will begin in $seconds seconds.").queue()
|
||||
} catch (e: NumberFormatException) {
|
||||
event.channel.sendMessage("Please specify a valid number of seconds: `${DiscordBot.commandPrefix} update <seconds>`").queue()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user