Skip to content

Commit

Permalink
Add ability to customize message
Browse files Browse the repository at this point in the history
  • Loading branch information
Theaninova committed Aug 18, 2020
1 parent 5969a2f commit 490a5dc
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 45 deletions.
21 changes: 9 additions & 12 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Add your Discord ID (not name), Bot token, and update frequency to the `admin.js
{
"adminId": 12345678910,
"token": "AOGH@(AKnjsfjiJijaig3ijgG92jaij",
"updateMs":30000
"updateMs": 30000,
"watchingMessage": "for new Blogposts"
}
```
* add your servers to `servers.json`
Expand All @@ -25,17 +26,20 @@ Add your Discord ID (not name), Bot token, and update frequency to the `admin.js
{
"id": 15050067772322222,
"mentionedRole": "everyone",
"autoPublish":true
"autoPublish": true,
"message": null
},
{
"id": 74050067772325222,
"mentionedRole": null,
"autoPublish":false
"autoPublish":false,
"message": null
},
{
"id": 74050067772325222,
"mentionedRole": "74036067771625222",
"autoPublish":false
"autoPublish":false,
"message": null
}
]
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'de.wulkanat'
version '1.2'
version '1.3'

repositories {
mavenCentral()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/de/wulkanat/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Admin {
val userId: Long
val token: String
val updateMs: Long
val message: String

var testModeEnabled: Boolean = false
set(value) {
Expand All @@ -36,6 +37,7 @@ object Admin {
userId = admin.adminId
token = admin.token
updateMs = admin.updateMs
message = admin.watchingMessage
}

var jda: JDA? = null
Expand Down
25 changes: 21 additions & 4 deletions src/main/kotlin/de/wulkanat/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ object Channels {
for (channel_pair in channels) {
try {
val channel = jda!!.getTextChannelById(channel_pair.id) ?: continue
val customMessage = channel_pair.message?.message ?: ""

if (channel_pair.mentionedRole != null) {
val message = if (channel_pair.mentionedRole == "everyone") {
"New Blogpost @everyone"
"@everyone $customMessage"
} 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 {
if (channel_pair.autoPublish) {
Expand Down Expand Up @@ -87,7 +98,13 @@ object Channels {
"everyone" -> " @everyone"
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 ""}"
}
}"
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/de/wulkanat/DataIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import java.io.File
data class DiscordChannel(
val id: Long,
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
data class AdminFile(
val adminId: Long,
val token: String,
val updateMs: Long
val updateMs: Long,
val watchingMessage: String
)

val SERVERS_FILE = File("servers.json")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/de/wulkanat/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun main() {
val builder = JDABuilder.createLight(
Admin.token,
GatewayIntent.GUILD_MESSAGES, GatewayIntent.DIRECT_MESSAGES)
.setActivity(Activity.watching("for new Blogposts"))
.setActivity(Activity.watching(Admin.message))
.build()

builder.addEventListener(AdminCli())
Expand Down
97 changes: 76 additions & 21 deletions src/main/kotlin/de/wulkanat/OwnerCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class OwnerCli : ListenerAdapter() {
event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`")
}
} else {
event.message.channel.sendMessage("Added.").queue()
event.message.channel.sendMessage("Channel not registered.").queue()
}
}
"ping" -> {
Expand Down Expand Up @@ -84,31 +84,80 @@ class OwnerCli : ListenerAdapter() {
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" -> {
event.message.channel.sendMessage(EmbedBuilder()
.setTitle("Server overview")
.setColor(Color.GREEN)
.setDescription(Channels.getServerNames(event.message.guild.idLong).joinToString("\n"))
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()).queue()
event.message.channel.sendMessage(
EmbedBuilder()
.setTitle("Server overview")
.setColor(Color.GREEN)
.setDescription(Channels.getServerNames(event.message.guild.idLong).joinToString("\n"))
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()
).queue()
}
"report" -> {
val errorReport = event.message.contentRaw.removePrefix("${prefix}report")
Admin.error(event.message.guild.name, errorReport, event.author)
event.message.channel.sendMessage(EmbedBuilder()
.setTitle("Error Report Received")
.setColor(Color.RED)
.setDescription(errorReport)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()).queue()
event.message.channel.sendMessage(
EmbedBuilder()
.setTitle("Error Report Received")
.setColor(Color.RED)
.setDescription(errorReport)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.build()
).queue()
}
"help" -> {
event.message.channel.sendMessage(EmbedBuilder()
.setTitle("Help")
.setColor(Color.YELLOW)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.setDescription(
"""
event.message.channel.sendMessage(
EmbedBuilder()
.setTitle("Help")
.setColor(Color.YELLOW)
.setAuthor(Admin.admin?.name, Admin.admin?.avatarUrl, Admin.admin?.avatarUrl)
.setDescription(
"""
**${prefix}add**
Add this channel to the notified list
**${prefix}remove**
Expand All @@ -117,14 +166,20 @@ class OwnerCli : ListenerAdapter() {
[Community|Partner|Verified only] Auto publish the message if in an announcement channel
**${prefix}ping [none|everyone|roleName]**
What role to ping
**${prefix}setMessage [message]**
Set a custom message to show
**${prefix}resetMessage**
Reset the message
**${prefix}info**
Show an overview about all channels registered on this server
**${prefix}report**
Report an issue to the Bot Admin (this will share your user name so they can contact you)
**${prefix}help**
Show this message
""".trimIndent())
.build()).queue()
""".trimIndent()
)
.build()
).queue()
}
}
}
Expand Down

0 comments on commit 490a5dc

Please sign in to comment.