Skip to content

Commit

Permalink
Merge pull request #15 from caffeine-moe/dev
Browse files Browse the repository at this point in the history
v2.2.0
  • Loading branch information
caffeine01 authored Jun 7, 2022
2 parents 63e3598 + 6a7928b commit 65a20b9
Show file tree
Hide file tree
Showing 114 changed files with 1,829 additions and 973 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Logo](https://caffeine.moe/CHAOS/logo-min.jpg)](https://caffeine.moe/CHAOS/)
# CHAOS - Like a utility knife for discord.

[![Build](https://github.com/caffeine-moe/CHAOS/workflows/Build/badge.svg)](https://github.com/caffeine-moe/CHAOS/actions/workflows/ci.yml) [![Issues](https://img.shields.io/github/issues/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/issues) [![Pull Requests](https://img.shields.io/github/issues-pr/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/pulls) [![Languages](https://img.shields.io/github/languages/top/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/search?l=kotlin&type=code) [![License: MIT](https://img.shields.io/github/license/caffeine-moe/CHAOS)](https://mit-license.org/) [![Lines](https://img.shields.io/tokei/lines/github/caffeine-moe/CHAOS)]() [![Fuck Discord](https://img.shields.io/badge/Fuck-Discord-critical)](https://discord.com/developers/docs/topics/oauth2#bot-vs-user-accounts)
[![Build](https://github.com/caffeine-moe/CHAOS/workflows/Build/badge.svg)](https://github.com/caffeine-moe/CHAOS/actions/workflows/ci.yml) [![Issues](https://img.shields.io/github/issues/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/issues) [![Pull Requests](https://img.shields.io/github/issues-pr/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/pulls) [![Languages](https://img.shields.io/github/languages/top/caffeine-moe/CHAOS)](https://github.com/caffeine-moe/CHAOS/search?l=kotlin&type=code) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/20fac2c6452940e592ffc9e06f21972a)](https://www.codacy.com/gh/caffeine-moe/CHAOS/dashboard?utm_source=github.com&utm_medium=referral&utm_content=caffeine-moe/CHAOS&utm_campaign=Badge_Grade) [![License: MIT](https://img.shields.io/github/license/caffeine-moe/CHAOS)](https://mit-license.org/) [![Lines](https://img.shields.io/tokei/lines/github/caffeine-moe/CHAOS)]() [![Fuck Discord](https://img.shields.io/badge/Fuck-Discord-critical)](https://discord.com/developers/docs/topics/oauth2#bot-vs-user-accounts)


Currently under development.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
application
kotlin("jvm") version "1.5.21"
kotlin("plugin.serialization") version "1.5.21"
kotlin("jvm") version "1.6.21"
kotlin("plugin.serialization") version "1.6.21"
id("com.github.johnrengelman.shadow") version "7.0.0"
id("java")
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/caffeine/chaos/AntiScam.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import kotlin.math.absoluteValue

@kotlinx.serialization.Serializable
data class AntiScamResponse(
val domains: List<String>,
val domains : List<String>,
)

suspend fun antiScam(client: Client, event: MessageCreateEvent) {
suspend fun antiScam(client : Client, event : MessageCreateEvent) {
if (event.message.author.id == client.user.id) return
val start = System.currentTimeMillis()
val url = event.message.content.split(" ").find { link -> link.matches("http://.*..*|https://.*..*".toRegex()) }
Expand Down
39 changes: 29 additions & 10 deletions src/main/kotlin/org/caffeine/chaos/AutoBump.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.caffeine.chaos.api.client.Client
import org.caffeine.chaos.api.client.message.MessageBuilder
import org.caffeine.chaos.api.client.message.MessageCreateEvent
import kotlin.math.absoluteValue

class AutoBump : Command(arrayOf("bump", "autobump", "sbump")) {
override suspend fun onCalled(client: Client, event: MessageCreateEvent, args: MutableList<String>, cmd: String) =
class AutoBump : Command(arrayOf("bump", "autobump", "sbump"), CommandInfo("AutoBump", "bump", "Autobumps.")) {
override suspend fun onCalled(
client : Client,
event : MessageCreateEvent,
args : MutableList<String>,
cmd : String,
) =
coroutineScope {
val pre = "AUTO BUMP:"
var err = ""
val logging = client.config.logger.auto_bump
if (cmd != "sbump") {

val channel = event.channel

var guildId = ""

if (event.channel.getGuild() != null) {
guildId = event.channel.getGuild()!!.id
} else {
err = "Channel ${channel.id} is not in a guild."
}

if (bumping.isNotEmpty() && bumping.any { it.id == event.channel.id }) {
err = "Already bumping in this channel."
}
Expand All @@ -30,29 +46,32 @@ class AutoBump : Command(arrayOf("bump", "autobump", "sbump")) {

if (err.isNotBlank()) {
if (logging) {
log(err, "AUTO BUMP:")
log(err, pre)
}
return@coroutineScope
}
bumping.add(event.channel)
bumping.add(channel)
if (logging) {
log("Started bumping in channel ${channel.id}", pre)
}
while (!autoBumpCock) {
val nonce = ((f..l).random()) * 60000
withContext(Dispatchers.IO) {
Thread.sleep(interval + nonce)
}
event.channel.sendMessage(MessageBuilder()
.append("d!bump")
.build()
).thenAccept { b ->
channel.sendInteraction("bump", guildId).thenAccept {
if (logging) {
this.launch {
log("Bump! in channel ${b.channel_id}", "AUTO BUMP:")
log("Bump! in channel ${channel.id}", pre)
}
}
}
}
return@coroutineScope
}
if (logging) {
log("Stopped bumping.", pre)
}
autoBumpCock = true
bumping = mutableListOf()
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/kotlin/org/caffeine/chaos/Command.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package org.caffeine.chaos

import org.caffeine.chaos.api.client.Client
import org.caffeine.chaos.api.client.message.Message
import org.caffeine.chaos.api.client.message.MessageBuilder
import org.caffeine.chaos.api.client.message.MessageCreateEvent

abstract class Command(commandNames: Array<String>) {
data class CommandInfo(
val name : String,
val usage : String,
val description : String,
)

abstract class Command(val commandNames : Array<String>, val commandInfo : CommandInfo) {
init {
for (name in commandNames) {
this.also { commandList[name] = it }
}
}

open suspend fun onCalled(client: Client, event: MessageCreateEvent, args: MutableList<String>, cmd: String) {}
open suspend fun onCalled(client : Client, event : MessageCreateEvent, args : MutableList<String>, cmd : String) {}

open suspend fun error(client : Client, event : MessageCreateEvent, error : String, info : CommandInfo) : Message {
return MessageBuilder()
.appendLine("**Incorrect usage** '${event.message.content}'")
.appendLine("**Error:** $error")
.appendLine("**Correct usage:** `${client.config.prefix}${info.usage}`")
.build()
}
}
60 changes: 28 additions & 32 deletions src/main/kotlin/org/caffeine/chaos/ConfigWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,36 @@ import java.nio.file.Paths
import java.nio.file.StandardWatchEventKinds

//checks if anything happens to the config file, if something does, then reload
suspend fun configWatcher(client: Client) {
try {
val watchService = withContext(Dispatchers.IO) {
FileSystems.getDefault().newWatchService()
}
val path = Paths.get("").toAbsolutePath()
val watchKey = withContext(Dispatchers.IO) {
path.register(
watchService,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE
)
}
while (true) {
for (event in watchKey.pollEvents()) {
val kind = event.kind()
if (kind == StandardWatchEventKinds.ENTRY_DELETE && event.context().toString()
.contains("config.json")
) {
client.logout()
main()
}
if (kind == StandardWatchEventKinds.ENTRY_MODIFY && event.context().toString()
.contains("config.json")
) {
client.logout()
main()
}
suspend fun configWatcher(client : Client) {
val watchService = withContext(Dispatchers.IO) {
FileSystems.getDefault().newWatchService()
}
val path = Paths.get("").toAbsolutePath()
val watchKey = withContext(Dispatchers.IO) {
path.register(
watchService,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE
)
}
while (true) {
for (event in watchKey.pollEvents()) {
val kind = event.kind()
if (kind == StandardWatchEventKinds.ENTRY_DELETE && event.context().toString()
.contains("config.json")
) {
client.logout()
main()
}
val valid = watchKey.reset()
if (!valid) {
break
if (kind == StandardWatchEventKinds.ENTRY_MODIFY && event.context().toString()
.contains("config.json")
) {
client.logout()
main()
}
}
} catch (e: Exception) {
e.printStackTrace()
val valid = watchKey.reset()
if (!valid) {
break
}
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/org/caffeine/chaos/ConsoleUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ fun clear() {
}

//logger utility
fun log(text: String, prefix: String = "") {
fun log(text : String, prefix : String = "") {
//gets current date and time
var message: String
var message : String
val time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss"))

//if the prefix is blank set the message with the default prefix
Expand All @@ -37,7 +37,7 @@ fun printLogo() {
" ░ ▒ ▒ ░▒░ ░ ▒ ▒▒ ░ ░ ▒ ▒░ ░ ░▒ ░ ░\n" +
"░ ░ ░░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ \n" +
"░ ░ ░ ░ ░ ░ ░ ░ ░ ░ \n" +
""
" "
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/caffeine/chaos/LoginPrompt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package org.caffeine.chaos

import org.caffeine.chaos.api.client.Client


//executed whenever the client is logged in successfully
suspend fun loginPrompt(client: Client) {
suspend fun loginPrompt(client : Client) {
//gets friend and guild count
val friends = client.user.relationships.friends.getAmount()
val guilds = client.user.guilds.getAmount()
//clears and prints edgy hackerman logo
clear()
printLogo()
//prints version number
println("\u001B[38;5;255mVersion: \u001B[38;5;33m${version}")
println("\u001B[38;5;255mVersion: \u001B[38;5;33m${versionString}")
//prints the prefix
println("\u001B[38;5;255mPrefix: \u001B[38;5;33m${client.config.prefix}")
//prints the name and discriminator of the currently logged-in user
Expand Down
22 changes: 14 additions & 8 deletions src/main/kotlin/org/caffeine/chaos/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,37 @@ import java.io.File
import kotlin.system.exitProcess

//version lmao
const val version: Float = 2.1F
const val versionString : String = "2.2.0"
const val versionDouble : Double = 2.20

//gets unix time in ms when program starts
val programStartedTime = System.currentTimeMillis()

//main function
suspend fun main(): Unit = coroutineScope {
suspend fun main() : Unit = coroutineScope {
//init
clear()
printLogo()
printSeparator()
log("\u001B[38;5;33mCHAOS is starting...")
//checks if config exists, if not, create one and exit
if (!File("config.json").exists()) {
val cfgName = "config.json"
if (!File(cfgName).exists()) {
val default = javaClass.classLoader.getResource("defaultconfig.json")
withContext(Dispatchers.IO) {
File("config.json").createNewFile()
File(cfgName).createNewFile()
}
File("config.json").writeText(default!!.readText())
File(cfgName).writeText(default!!.readText())
log(
"Config not found, we have generated one for you at ${File("config.json").absolutePath}",
"Config not found, we have generated one for you at ${File(cfgName).absolutePath}",
"\u001B[38;5;197mERROR:"
)
log("\u001B[38;5;33mPlease change the file accordingly. Documentation: https://caffeine.moe/CHAOS/")
exitProcess(0)
}
try {
//tries to read config
val config = Json.decodeFromString<Config>(File("config.json").readText())
val config : Config = Json.decodeFromString(File(cfgName).readText())
//gets antiscam links
if (config.anti_scam.enabled) {
scamLinks =
Expand All @@ -56,8 +58,12 @@ suspend fun main(): Unit = coroutineScope {
//web ui benched for now
/* val ui = WebUI()
launch { ui.init(client) } */
//checks if client is up to date
if (client.config.updater.enabled) {
update(client)
}
client.login(config)
} catch (e: Exception) {
} catch (e : Exception) {
//if it cant read the config then it logs that its invalid
if (e.toString().contains("JsonDecodingException")) {
log(
Expand Down
14 changes: 10 additions & 4 deletions src/main/kotlin/org/caffeine/chaos/MessageHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.caffeine.chaos.api.client.message.MessageCreateEvent
import org.caffeine.chaos.commands.*

//HashMap of commands
var commandList: HashMap<String, Command> = HashMap()
var commandList : HashMap<String, Command> = HashMap()

//spam stopper, if true, stops the message spammer
var spamCock = false
Expand Down Expand Up @@ -48,10 +48,16 @@ fun registerCommands() {
SysFetch()
UserInfo()
AutoBump()
MuteAllServers()
Theme()
Pin()
HypeSquad()
Dice()
IdToDate()
}

//executed whenever a message event is received by the client
suspend fun handleMessage(event: MessageCreateEvent, client: Client) {
suspend fun handleMessage(event : MessageCreateEvent, client : Client) {
//if nitro sniper is enabled and email is verified, pass the message to the nitro sniper
if (client.config.nitro_sniper.enabled && client.user.verified) {
nitroSniper(event, client)
Expand All @@ -62,12 +68,12 @@ suspend fun handleMessage(event: MessageCreateEvent, client: Client) {
//if the message starts with the configured prefix and isn't just the prefix
//then remove the prefix and set the first item in the message (the command) as a value
if (event.message.content.startsWith(client.config.prefix) && event.message.content != client.config.prefix) {
val commandName: String =
val commandName : String =
event.message.content.lowercase().replaceFirst(client.config.prefix, "").split(" ").first()
//creates a new command object and
//checks if the first item in the message (commandName) is a command and matches it to the command in the HashMap commandList.
//if it can't, return
val command: Command = commandList.get(commandName) ?: return
val command : Command = commandList[commandName] ?: return

//if the command logger is enabled then log the command
if (client.config.logger.commands) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/caffeine/chaos/NitroSniper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.caffeine.chaos.api.client.ClientUserRedeemedCodeStatus
import org.caffeine.chaos.api.client.message.MessageCreateEvent

//executed whenever a message is received
suspend fun nitroSniper(event: MessageCreateEvent, client: Client) = coroutineScope {
suspend fun nitroSniper(event : MessageCreateEvent, client : Client) = coroutineScope {
//regex for a discord gift (nitro) link
val rg = ("https://discord.gift/" + ".{16,24}".toRegex()).toRegex()
//if the message content matches the regex (contains a nitro link) then do stuff
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/org/caffeine/chaos/Ready.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.caffeine.chaos

import org.caffeine.chaos.api.client.Client

suspend fun ready(client : Client) {
log("\u001B[38;5;33mWelcome to CHAOS!")
clear()
registerCommands()
loginPrompt(client)
configWatcher(client)
}
Loading

0 comments on commit 65a20b9

Please sign in to comment.