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 5969a2f
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 52 deletions.
62 changes: 40 additions & 22 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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'de.wulkanat'
version '1.1'
version '1.2'

repositories {
mavenCentral()
Expand Down
11 changes: 9 additions & 2 deletions 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 All @@ -71,12 +71,19 @@ object Admin {
)
}

fun error(msg: String, error: String) {
fun error(msg: String, error: String, author: User? = null) {
sendDevMessage(
EmbedBuilder()
.setTitle(msg)
.setDescription(error)
.setColor(Color.RED)
.run {
if (author == null) {
this
} else {
this.setAuthor(author.asTag, author.avatarUrl, author.avatarUrl)
}
}
.build()
, "$msg\n\n${error}"
)
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
46 changes: 28 additions & 18 deletions src/main/kotlin/de/wulkanat/Channels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.MessageEmbed
import net.dv8tion.jda.api.entities.TextChannel
import net.dv8tion.jda.api.exceptions.ErrorResponseException

object Channels {
var jda: JDA? = null
Expand All @@ -23,20 +24,24 @@ object Channels {
return

for (channel_pair in channels) {
val channel = jda!!.getTextChannelById(channel_pair.id) ?: continue

if (channel_pair.mentionedRole != null) {
val message = if (channel_pair.mentionedRole == "everyone") {
"New Blogpost @everyone"
} else {
"New Blogpost <@&${channel_pair.mentionedRole}>"
try {
val channel = jda!!.getTextChannelById(channel_pair.id) ?: continue

if (channel_pair.mentionedRole != null) {
val message = if (channel_pair.mentionedRole == "everyone") {
"New Blogpost @everyone"
} else {
"New Blogpost <@&${channel_pair.mentionedRole}>"
}
channel.sendMessage(message).queue()
}
channel.sendMessage(message).queue()
}
channel.sendMessage(messageEmbed).queue {
if (channel_pair.autoPublish) {
it.crosspost().queue()
channel.sendMessage(messageEmbed).queue {
if (channel_pair.autoPublish) {
it.crosspost().queue()
}
}
} catch (e: ErrorResponseException) {
Admin.error("Error in server", e.message ?: e.localizedMessage)
}
}
}
Expand Down Expand Up @@ -66,11 +71,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 +85,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 +95,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
Loading

0 comments on commit 5969a2f

Please sign in to comment.