Skip to content

Commit

Permalink
Add self-configuration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Theaninova committed Aug 17, 2020
1 parent e58f4dc commit 2dfe3f1
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 34 deletions.
59 changes: 41 additions & 18 deletions .idea/workspace.xml

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

12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# BlogShot
A bot that automatically polls the newest blogpost from [Hytale News Tab](https://www.hytale.com/news) and posts a message into servers if there is a new one.
## Setup
Okay, this isn't really meant for you to setup, if you want it though it first is easier to just dm me on Twitter [@tale_talk](https://twitter.com/tale_talk) so I can add you to the server list.
If you *really* want to set it up yourself, fine.
## Add to your server
Click [this](https://discord.com/api/oauth2/authorize?client_id=743447329901641799&permissions=150528&scope=bot) link to invite
the bot to your server. Please note that only people with *Administrator* permission will be able to
configure it.

You can type `%!info` to get an overview over all available commands.

## Self Hosting
Okay, this isn't really meant for you to setup, but if you *really* want to set it up yourself, fine.
* first go to the release tab, download the jar, and put it in a folder
* Add two files in the root of the repo, an `admin.json` and a `servers.json`.
Add your Discord ID (not name), Bot token, and update frequency to the `admin.json`:
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/de/wulkanat/Admin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Admin {
kotlin.io.println("Connected to ${admin!!.name}. No further errors will be printed here.")
}
}
private var admin: User? = null
var admin: User? = null

fun println(msg: String) {
sendDevMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.dv8tion.jda.api.events.ExceptionEvent
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent
import kotlin.system.exitProcess

class Cli : ListenerAdapter() {
class AdminCli : ListenerAdapter() {
override fun onPrivateMessageReceived(event: PrivateMessageReceivedEvent) {
val msg = event.message.contentRaw
if (event.author.idLong != Admin.userId ||
Expand Down
17 changes: 11 additions & 6 deletions src/main/kotlin/de/wulkanat/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ object Channels {
).toMutableList()
}

fun getServerNames(): List<String> {
fun getServerNames(server: Long? = null): List<String> {
if (jda == null)
return listOf()

return channels.map {
return channels.filter { server != null && (jda!!.getTextChannelById(it.id)?.guild?.idLong == server) }.map {
val channel = jda!!.getTextChannelById(it.id)
if (channel == null) {
Admin.warning("Channel ${it.id} is no longer active!")
Expand All @@ -80,7 +80,7 @@ object Channels {
val role = when (it.mentionedRole) {
null -> ""
"everyone" -> " @everyone"
else -> " @${channel.guild.getRoleById(it.mentionedRole)?.name}"
else -> " @${channel.guild.getRoleById(it.mentionedRole ?: "")?.name}"
}
"**${channel.guild.name}**\n#${channel.name}${role}"
}
Expand All @@ -90,12 +90,17 @@ object Channels {
return jda?.getTextChannelById(id)
}

fun addChannel(id: Long, role: String?) {
channels.add(DiscordChannel(id, role))
fun addChannel(id: Long, role: String?): DiscordChannel? {
if (channels.find { it.id == id } != null) {
return null
}
val out = DiscordChannel(id, role)
channels.add(out)
saveChannels()
return out
}

private fun saveChannels() {
fun saveChannels() {
SERVERS_FILE.writeText(
json.stringify(
DiscordChannel.serializer().list,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/de/wulkanat/DataIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import java.io.File
@Serializable
data class DiscordChannel(
val id: Long,
val mentionedRole: String? = null,
val autoPublish: Boolean = false
var mentionedRole: String? = null,
var autoPublish: Boolean = false
)

@Serializable
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/de/wulkanat/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ fun main() {
.setActivity(Activity.watching("for new Blogposts"))
.build()

builder.addEventListener(Cli())
builder.addEventListener(AdminCli())
builder.addEventListener(ErrorHandler())
builder.addEventListener(OwnerCli())
builder.awaitReady()

Channels.jda = builder
Expand Down
119 changes: 119 additions & 0 deletions src/main/kotlin/de/wulkanat/OwnerCli.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package de.wulkanat

import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import java.awt.Color

class OwnerCli : ListenerAdapter() {
private val prefix = "%!"

override fun onMessageReceived(event: MessageReceivedEvent) {
val msg = event.message.contentRaw
// Only accept admin requests
if (event.message.member?.hasPermission(Permission.ADMINISTRATOR) != true || !msg.startsWith(prefix)) {
return
}

val command = msg.removePrefix(prefix).split(Regex("\\s+"))
val channelId = event.message.channel.idLong

when (command.first()) {
"add" -> {
val result = Channels.addChannel(channelId, null)
if (result == null) {
event.message.channel.sendMessage("Already added.").queue()
} else {
event.message.channel.sendMessage("Added.").queue()
Admin.info()
}
}
"remove" -> {
val result = Channels.channels.removeAll { it.id == channelId }
Channels.saveChannels()
if (result) {
event.message.channel.sendMessage("Removed.").queue()
} else {
event.message.channel.sendMessage("This channel is not registered.").queue()
}
}
"publish" -> {
val result = Channels.channels.find { it.id == channelId }
if (result != null) {
if (command.size > 1 && listOf("on", "off").contains(command[1])) {
result.autoPublish = command[1] == "on"
Channels.saveChannels()

event.message.channel.sendMessage("Auto publish is now on").queue()
} else {
event.message.channel.sendMessage("Usage: `${prefix}publish [on|off]`")
}
} else {
event.message.channel.sendMessage("Added.").queue()
}
}
"ping" -> {
val result = Channels.channels.find { it.id == channelId }
if (result != null) {
if (command.size > 1) {
val roles = event.message.guild.getRolesByName(command[1], false)
result.mentionedRole = when {
command[1] == "everyone" -> {
event.message.channel.sendMessage("Now pinging everyone.").queue()
"everyone"
}
command[1] == "none" -> {
event.message.channel.sendMessage("Now pinging none.").queue()
null
}
roles.firstOrNull() != null -> {
event.message.channel.sendMessage("Now pinging ${roles.first().name}").queue()
roles.first().id
}
else -> {
event.message.channel.sendMessage("Unknown role.").queue()
result.mentionedRole
}
}
Channels.saveChannels()
} else {
event.message.channel.sendMessage("Usage: `${prefix}ping [everyone|none|roleName]`")
}
} else {
event.message.channel.sendMessage("Channel is 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()
}
"help" -> {
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**
Remove this channel to the notified list
**${prefix}publish [on|off]**
[Community|Partner|Verified only] Auto publish the message if in an announcement channel
**${prefix}ping [none|everyone|roleName]**
What role to ping
**${prefix}info**
Show an overview about all channels registered on this server
**${prefix}help**
Show this message
""".trimIndent())
.build()).queue()
}
}
}
}
2 changes: 0 additions & 2 deletions src/main/kotlin/de/wulkanat/web/SiteWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ object SiteWatcher {
var newestBlog: BlogPostPreview? = null

fun hasNewBlogPost(): Boolean {
Admin.silent("Updating...")

try {
val doc = Jsoup.connect(BLOG_INDEX_URL).get()
val newBlog = BlogPostParser.getFistBlog(doc)
Expand Down

0 comments on commit 2dfe3f1

Please sign in to comment.