K
Kord•6d ago
chommsi

Calling Kord constructor throws IOException

I have just setup a new Kotlin project in Intellij I implemented the code as shown in the "Making a simple ping-pong bot" section of the github wiki, adding my own token
suspend fun main() {
val kord = Kord("MY TOKEN")
kord.on<MessageCreateEvent> { // runs every time a message is created that our bot can read

// ignore other bots, even ourselves. We only serve humans here!
if (message.author?.isBot != false) return@on

// check if our command is being invoked
if (message.content != "!ping") return@on

// all clear, give them the pong!
message.channel.createMessage("pong!")
}

kord.login {
// we need to specify this to receive the content of messages
@OptIn(PrivilegedIntent::class)
intents += Intent.MessageContent
}
}
suspend fun main() {
val kord = Kord("MY TOKEN")
kord.on<MessageCreateEvent> { // runs every time a message is created that our bot can read

// ignore other bots, even ourselves. We only serve humans here!
if (message.author?.isBot != false) return@on

// check if our command is being invoked
if (message.content != "!ping") return@on

// all clear, give them the pong!
message.channel.createMessage("pong!")
}

kord.login {
// we need to specify this to receive the content of messages
@OptIn(PrivilegedIntent::class)
intents += Intent.MessageContent
}
}
this throws the following exception: https://pastebin.com/n9FESr6k my pom.xml: https://pastebin.com/F5EEAXQ6 Version: 0.15.0 JDK: 24 I feel like something with my setup is probably wrong but I'm absolutely stumped as to what it could be at this point
Pastebin
SLF4J(W): No SLF4J providers were found.SLF4J(W): Defaulting to no-...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
56 Replies
gdude
gdude•6d ago
looking into it does maven not support api-scoped dependencies? nah it must do you have kord core, so you don't need to add common, rest or gateway unless maven doesn't add them to your compile classpath for some reason the root cause of the exception is that the kotlinx.io on your classpath is the wrong version, or missing entirely you're going to have a very difficult time finding anyone with any deep experience with maven in the kotlin community though
chommsi
chommsiOP•6d ago
ah, I originally only had core but wanted to make sure it's not that I'm missing something so I tried adding the others
gdude
gdude•6d ago
if you go down the traceback you can see the root cause
Caused by: java.lang.ClassNotFoundException: kotlinx.io.unsafe.UnsafeBufferOperations
chommsi
chommsiOP•6d ago
so my best bet is probably to make a gradle project instead?
gdude
gdude•6d ago
I would generally recommend using Gradle yeah are you familiar?
chommsi
chommsiOP•6d ago
kinda, I've used it here and there
gdude
gdude•6d ago
well before we get to that
chommsi
chommsiOP•6d ago
I mostly wanted to go with maven because the service I wrote is already in maven
gdude
gdude•6d ago
how do you plan to distribute your bot? or well, the correct term I should use there is package single jar? distribution zip? docker container?
chommsi
chommsiOP•6d ago
it's been too long since I did anything with discord bots 😅 whatever runs on a raspberry will be fine
gdude
gdude•6d ago
what model?
chommsi
chommsiOP•6d ago
i think 3b
gdude
gdude•6d ago
a 3b or 3b+?
chommsi
chommsiOP•6d ago
I'm not sure
gdude
gdude•6d ago
actually it doesn't seem to matter they both only have about a gig of ram you're probably not going to manage to run a jvm bot on that the good news is that kord isn't jvm-only
chommsi
chommsiOP•6d ago
tbh, I'm still quite far from that, first I wanna get the bot running locally
gdude
gdude•6d ago
alright, fair well, the root of your current issue is a missing/incorrect kotlinx.io
gdude
gdude•6d ago
GitHub
GitHub - Kotlin/kotlinx-io: Kotlin multiplatform I/O library
Kotlin multiplatform I/O library. Contribute to Kotlin/kotlinx-io development by creating an account on GitHub.
gdude
gdude•6d ago
however you should think about how you want to package it your pom doesn't show any signs of having thought of that, so it might be the case that you're not done configuring your project if you're trying to just run the bot's jar then this configuration as written likely won't work, because the jar won't contain any of the dependencies
chommsi
chommsiOP•6d ago
you're very much right, what would you recommend I use to package it to a jar?
gdude
gdude•6d ago
in gradle I'd generally recommend the distribution plugin with maven, I'm not sure the distribution plugin packages your project along with its dependencies in a .zip or .tar archive, including some start scripts you'd just unpack the archive and run the script
chommsi
chommsiOP•6d ago
hmm, I think I might just have to convert the service I have to gradle and make a new gradle project for the bot
gdude
gdude•6d ago
you don't need to convert any other projects unless they're in the same build unit, I guess sorry, it's just that gradle is the standard build tool for kotlin, so that's what most people will know
chommsi
chommsiOP•6d ago
ah, no worries, we're just using maven with kotlin primarily at work, so I thought I might just go with that haha didn't know we were actually the odd ones out I wanted to add the service as a dependency to the bot, so I think I'll have to make it a gradle project
gdude
gdude•6d ago
if it's published in a maven repo, there's no need
chommsi
chommsiOP•6d ago
it's all local for now
gdude
gdude•6d ago
You can use mavenLocal() if you installed it to your local maven repo I do recommend trying gradle before converting other projects
chommsi
chommsiOP•6d ago
alright, I added a new module with gradle now and replaced the main with the code from above and it works without problem now
gdude
gdude•6d ago
okay, interesting so then maybe it is the case that maven isn't pulling in all the dependencies
chommsi
chommsiOP•6d ago
seems like it
gdude
gdude•6d ago
okay, so before you get stuck into this I implore you to be careful about how you write this code given you need to run this on 1 gig of ram and a 1.2GHz/1.4GHz CPU there are additional frameworks that build on top of Kord, I maintain one of them (KordEx) I very much do not recommend using one make this as lean as you possibly can
chommsi
chommsiOP•6d ago
current plan is two slash commands for private use on a single discord server you think a raspberry could handle that?
gdude
gdude•6d ago
it should however do you know javascript?
chommsi
chommsiOP•6d ago
also, eureka! i simply added the dependency for kotlinx to the pom.xml, now the maven project works too i've used it a bit, yeah
gdude
gdude•6d ago
okay so yeah that is what it was lmao do you actually need the discord gateway? or could you get away with a REST-only bot?
chommsi
chommsiOP•6d ago
that I had in the pom.xml?
gdude
gdude•6d ago
no, not exactly okay, so Discord bots ("apps") can take two forms the one you're familiar with, a traditional bot, is a process that connects to Discord's gateway, which is a websocket, and processes events and data in realtime as it's given them however, if you're just doing slash commands, and you don't need the usual set of events, you probably don't actually need that
chommsi
chommsiOP•6d ago
oh, so there's like a lightweight version for that?
gdude
gdude•6d ago
the other form is a REST-only bot, where discord will essentially send data to a webhook when one of your commands are run you can technically do that with Kord and ktor, but since you seem to be limited on capacity here, you might be able to get away with using a cloudflare worker on the free tier feel free to continue on with kord if you like, it will probably work fine, but that's also an option if you feel like looking into it
gdude
gdude•6d ago
GitHub
GitHub - discord/cloudflare-sample-app: Example discord bot using C...
Example discord bot using Cloudflare Workers. Contribute to discord/cloudflare-sample-app development by creating an account on GitHub.
chommsi
chommsiOP•6d ago
oh cool, that sounds like it might be a good idea i'll check out the sample project
gdude
gdude•6d ago
it'll likely be considerably more reliable than running on a pi that old that's my thinking
chommsi
chommsiOP•6d ago
completely fair, I'm not even sure where it is haha
gdude
gdude•6d ago
I have used pis in production ftr those things are basically disposable they die so I do recommend something like this over that
chommsi
chommsiOP•6d ago
it does seem like the easier version considering I wouldn't need to go unpack a bunch of boxes in the hope of finding the pi 😅
gdude
gdude•6d ago
haha well then, best of luck with it I will say I haven't done it myself, but I do know people that have
chommsi
chommsiOP•6d ago
thanks a lot for the quick help and great suggestions! appreciate it a lot :tabbylove:
gdude
gdude•6d ago
you're welcome even though I am personally invested in kord's ecosystem it just isn't really the right option here imo that's how it goes sometimes
chommsi
chommsiOP•6d ago
definitely a type of bot I haven't made so far, they were all "traditional" bots as you put it
gdude
gdude•6d ago
REST-only bots are a much newer concept so yeah a lot of people don't know about them
chommsi
chommsiOP•6d ago
yeah, haven't made bots in a few years, so I wasn't really aware
gdude
gdude•6d ago
ah yeah, you might've even stopped before they came in they are very convenient once you get used to the workflow, or so I'm told there are likely better libraries as well since discord made that example
chommsi
chommsiOP•6d ago
probably, I've been at it for almost 8 hours at this point so I think I'll go and continue this tomorrow 😅
gdude
gdude•6d ago
haha, good call, rest is important
chommsi
chommsiOP•6d ago
well then, thanks again, have a great day/night/wherever you are 😄
gdude
gdude•6d ago
you're welcome, good luck with it :>

Did you find this page helpful?