Add ability to customize message

This commit is contained in:
Wieland Schöbl
2020-08-18 10:25:19 +02:00
parent 5969a2f221
commit 490a5dcd41
8 changed files with 128 additions and 45 deletions

21
.idea/workspace.xml generated
View File

@@ -2,10 +2,7 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment=""> <list default="true" id="1aabf22b-2f57-46ac-9973-367d8668ffd3" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Admin.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Admin.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Channels.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/Channels.kt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/OwnerCli.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/kotlin/de/wulkanat/OwnerCli.kt" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -283,26 +280,26 @@
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state x="740" y="238" key="FileChooserDialogImpl/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597605616287" /> <state x="740" y="238" key="FileChooserDialogImpl/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597605616287" />
<state width="1876" height="161" key="GridCell.Tab.0.bottom" timestamp="1597703826263"> <state width="1876" height="161" key="GridCell.Tab.0.bottom" timestamp="1597739482072">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state width="1006" height="588" key="GridCell.Tab.0.bottom/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329412" /> <state width="1006" height="588" key="GridCell.Tab.0.bottom/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329412" />
<state width="1876" height="161" key="GridCell.Tab.0.bottom/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597703826263" /> <state width="1876" height="161" key="GridCell.Tab.0.bottom/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597739482072" />
<state width="1876" height="161" key="GridCell.Tab.0.center" timestamp="1597703826263"> <state width="1876" height="161" key="GridCell.Tab.0.center" timestamp="1597739482072">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state width="1006" height="588" key="GridCell.Tab.0.center/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" /> <state width="1006" height="588" key="GridCell.Tab.0.center/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" />
<state width="1876" height="161" key="GridCell.Tab.0.center/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597703826263" /> <state width="1876" height="161" key="GridCell.Tab.0.center/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597739482072" />
<state width="1876" height="161" key="GridCell.Tab.0.left" timestamp="1597703826263"> <state width="1876" height="161" key="GridCell.Tab.0.left" timestamp="1597739482072">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state width="1006" height="588" key="GridCell.Tab.0.left/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" /> <state width="1006" height="588" key="GridCell.Tab.0.left/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" />
<state width="1876" height="161" key="GridCell.Tab.0.left/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597703826263" /> <state width="1876" height="161" key="GridCell.Tab.0.left/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597739482072" />
<state width="1876" height="161" key="GridCell.Tab.0.right" timestamp="1597703826263"> <state width="1876" height="161" key="GridCell.Tab.0.right" timestamp="1597739482072">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>
<state width="1006" height="588" key="GridCell.Tab.0.right/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" /> <state width="1006" height="588" key="GridCell.Tab.0.right/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@-1050.105.1050.1640" timestamp="1597351329411" />
<state width="1876" height="161" key="GridCell.Tab.0.right/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597703826263" /> <state width="1876" height="161" key="GridCell.Tab.0.right/0.0.1920.1040/1920.-213.2560.1400/-1050.105.1050.1640@0.0.1920.1040" timestamp="1597739482072" />
<state width="1006" height="588" key="GridCell.Tab.1.bottom" timestamp="1597366506508"> <state width="1006" height="588" key="GridCell.Tab.1.bottom" timestamp="1597366506508">
<screen x="0" y="0" width="1920" height="1040" /> <screen x="0" y="0" width="1920" height="1040" />
</state> </state>

View File

@@ -16,7 +16,8 @@ Add your Discord ID (not name), Bot token, and update frequency to the `admin.js
{ {
"adminId": 12345678910, "adminId": 12345678910,
"token": "AOGH@(AKnjsfjiJijaig3ijgG92jaij", "token": "AOGH@(AKnjsfjiJijaig3ijgG92jaij",
"updateMs":30000 "updateMs": 30000,
"watchingMessage": "for new Blogposts"
} }
``` ```
* add your servers to `servers.json` * add your servers to `servers.json`
@@ -25,17 +26,20 @@ Add your Discord ID (not name), Bot token, and update frequency to the `admin.js
{ {
"id": 15050067772322222, "id": 15050067772322222,
"mentionedRole": "everyone", "mentionedRole": "everyone",
"autoPublish":true "autoPublish": true,
"message": null
}, },
{ {
"id": 74050067772325222, "id": 74050067772325222,
"mentionedRole": null, "mentionedRole": null,
"autoPublish":false "autoPublish":false,
"message": null
}, },
{ {
"id": 74050067772325222, "id": 74050067772325222,
"mentionedRole": "74036067771625222", "mentionedRole": "74036067771625222",
"autoPublish":false "autoPublish":false,
"message": null
} }
] ]
``` ```

View File

@@ -4,7 +4,7 @@ plugins {
} }
group 'de.wulkanat' group 'de.wulkanat'
version '1.2' version '1.3'
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -13,6 +13,7 @@ object Admin {
val userId: Long val userId: Long
val token: String val token: String
val updateMs: Long val updateMs: Long
val message: String
var testModeEnabled: Boolean = false var testModeEnabled: Boolean = false
set(value) { set(value) {
@@ -36,6 +37,7 @@ object Admin {
userId = admin.adminId userId = admin.adminId
token = admin.token token = admin.token
updateMs = admin.updateMs updateMs = admin.updateMs
message = admin.watchingMessage
} }
var jda: JDA? = null var jda: JDA? = null

View File

@@ -26,14 +26,25 @@ object Channels {
for (channel_pair in channels) { for (channel_pair in channels) {
try { try {
val channel = jda!!.getTextChannelById(channel_pair.id) ?: continue val channel = jda!!.getTextChannelById(channel_pair.id) ?: continue
val customMessage = channel_pair.message?.message ?: ""
if (channel_pair.mentionedRole != null) { if (channel_pair.mentionedRole != null) {
val message = if (channel_pair.mentionedRole == "everyone") { val message = if (channel_pair.mentionedRole == "everyone") {
"New Blogpost @everyone" "@everyone $customMessage"
} else { } else {
"New Blogpost <@&${channel_pair.mentionedRole}>" "<@&${channel_pair.mentionedRole}> $customMessage"
}
channel.sendMessage(message).queue {
if (channel_pair.message?.pushAnnouncement == true) {
it.crosspost().queue()
}
}
} else if (channel_pair.message != null) {
channel.sendMessage(customMessage).queue {
if (channel_pair.message?.pushAnnouncement == true) {
it.crosspost().queue()
}
} }
channel.sendMessage(message).queue()
} }
channel.sendMessage(messageEmbed).queue { channel.sendMessage(messageEmbed).queue {
if (channel_pair.autoPublish) { if (channel_pair.autoPublish) {
@@ -87,7 +98,13 @@ object Channels {
"everyone" -> " @everyone" "everyone" -> " @everyone"
else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}" else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}"
} }
"**${channel.guild.name}**\n#${channel.name}${role}" val publish = if (it.autoPublish) " (publish)" else ""
"**${channel.guild.name}**\n#${channel.name}${role}${publish}${if (it.message == null) {
""
} else {
"\n*${it.message!!.message}*${if (it.message!!.pushAnnouncement) " (publish)" else ""}"
}
}"
} }
} }

View File

@@ -7,14 +7,22 @@ import java.io.File
data class DiscordChannel( data class DiscordChannel(
val id: Long, val id: Long,
var mentionedRole: String? = null, var mentionedRole: String? = null,
var autoPublish: Boolean = false var autoPublish: Boolean = false,
var message: CustomMessage? = null
)
@Serializable
data class CustomMessage(
var message: String,
var pushAnnouncement: Boolean = false
) )
@Serializable @Serializable
data class AdminFile( data class AdminFile(
val adminId: Long, val adminId: Long,
val token: String, val token: String,
val updateMs: Long val updateMs: Long,
val watchingMessage: String
) )
val SERVERS_FILE = File("servers.json") val SERVERS_FILE = File("servers.json")

View File

@@ -10,7 +10,7 @@ fun main() {
val builder = JDABuilder.createLight( val builder = JDABuilder.createLight(
Admin.token, Admin.token,
GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES) GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES)
.setActivity(Activity.watching("for new Blogposts")) .setActivity(Activity.watching(Admin.message))
.build() .build()
builder.addEventListener(AdminCli()) builder.addEventListener(AdminCli())

View File

@@ -50,7 +50,7 @@ class OwnerCli : ListenerAdapter() {
event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`") event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`")
} }
} else { } else {
event.message.channel.sendMessage("Added.").queue() event.message.channel.sendMessage("Channel not registered.").queue()
} }
} }
"ping" -> { "ping" -> {
@@ -84,31 +84,80 @@ class OwnerCli : ListenerAdapter() {
event.message.channel.sendMessage("Channel is not registered.").queue() event.message.channel.sendMessage("Channel is not registered.").queue()
} }
} }
"setMessage" -> {
val result = Channels.channels.find { it.id == channelId }
if (result != null) {
if (command.size > 1) {
val message = event.message.contentRaw.removePrefix("${prefix}setMessage").trim()
result.message = CustomMessage(message)
Channels.saveChannels()
event.message.channel.sendMessage("Set `$message` as message.").queue()
} else {
event.message.channel.sendMessage("Usage: `${prefix}setMessage [message]`")
}
} else {
event.message.channel.sendMessage("Channel is not registered.").queue()
}
}
"resetMessage" -> {
val result = Channels.channels.find { it.id == channelId }
if (result != null) {
result.message = null
Channels.saveChannels()
event.message.channel.sendMessage("Reset to no message.").queue()
} else {
event.message.channel.sendMessage("Channel is not registered.").queue()
}
}
"publishMessage" -> {
val result = Channels.channels.find { it.id == channelId }
if (result != null) {
if (result.message != null) {
if (command.size > 1 && listOf("on", "off").contains(command[1])) {
result.message?.pushAnnouncement = command[1] == "on"
Channels.saveChannels()
event.message.channel.sendMessage("Auto publish (message) is now ${command[1]}").queue()
} else {
event.message.channel.sendMessage("Usage: `${prefix}publishMessage [on|off]`")
}
} else {
event.message.channel.sendMessage("Channel has no custom message.").queue()
}
} else {
event.message.channel.sendMessage("Channel not registered.").queue()
}
}
"info" -> { "info" -> {
event.message.channel.sendMessage(EmbedBuilder() event.message.channel.sendMessage(
.setTitle("Server overview") EmbedBuilder()
.setColor(Color.GREEN) .setTitle("Server overview")
.setDescription(Channels.getServerNames(event.message.guild.idLong).joinToString("\n")) .setColor(Color.GREEN)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) .setDescription(Channels.getServerNames(event.message.guild.idLong).joinToString("\n"))
.build()).queue() .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()
).queue()
} }
"report" -> { "report" -> {
val errorReport = event.message.contentRaw.removePrefix("${prefix}report") val errorReport = event.message.contentRaw.removePrefix("${prefix}report")
Admin.error(event.message.guild.name, errorReport, event.author) Admin.error(event.message.guild.name, errorReport, event.author)
event.message.channel.sendMessage(EmbedBuilder() event.message.channel.sendMessage(
.setTitle("Error Report Received") EmbedBuilder()
.setColor(Color.RED) .setTitle("Error Report Received")
.setDescription(errorReport) .setColor(Color.RED)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) .setDescription(errorReport)
.build()).queue() .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()
).queue()
} }
"help" -> { "help" -> {
event.message.channel.sendMessage(EmbedBuilder() event.message.channel.sendMessage(
.setTitle("Help") EmbedBuilder()
.setColor(Color.YELLOW) .setTitle("Help")
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl) .setColor(Color.YELLOW)
.setDescription( .setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
""" .setDescription(
"""
**${prefix}add** **${prefix}add**
Add this channel to the notified list Add this channel to the notified list
**${prefix}remove** **${prefix}remove**
@@ -117,14 +166,20 @@ class OwnerCli : ListenerAdapter() {
[Community|Partner|Verified only] Auto publish the message if in an announcement channel [Community|Partner|Verified only] Auto publish the message if in an announcement channel
**${prefix}ping [none|everyone|roleName]** **${prefix}ping [none|everyone|roleName]**
What role to ping What role to ping
**${prefix}setMessage [message]**
Set a custom message to show
**${prefix}resetMessage**
Reset the message
**${prefix}info** **${prefix}info**
Show an overview about all channels registered on this server Show an overview about all channels registered on this server
**${prefix}report** **${prefix}report**
Report an issue to the Bot Admin (this will share your user name so they can contact you) Report an issue to the Bot Admin (this will share your user name so they can contact you)
**${prefix}help** **${prefix}help**
Show this message Show this message
""".trimIndent()) """.trimIndent()
.build()).queue() )
.build()
).queue()
} }
} }
} }