Hello guys, My program calls a function inside a c...
# coroutines
o
Hello guys, My program calls a function inside a coroutine, which calls a plugin. Now this plugin requires to wait for a specified period of time. I tried java.util.Timer and it didnt work(i put 60 secs but even after that it didnt work). Now i think i'll try this https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/java.util.-timer/schedule.html . But what Im unsure about is that if a plugin can have a coroutine call inside it(also, im not sure if Schedule is a coroutine thingy). Thanks.
m
Timer.schedule
is not a coroutine-construct. What kind of "plugin" are you talking about?
o
its a simple plugin which processes text and returns a value
the text can hav a string like "60s" or "450D"
i process tht string into seconds, then passed it to Timer.schedule()
m
Do you mean a library? Can you give some example code?
o
well, its a messy bit of code
m
I think I just need to see the API of that plugin
m
So "onCommand" is the function you wait for?
o
no, onCommand is a general function tht always returns a string. That command takes a string as parameter and processes it and then returns some other string
currently tht part of the code isnt updated to the repos but heres a snippet
else if(strList.get(1) == "ban") { var found = false if(size <= 3) { retData = "Usage: ${specialChar}chan ban #channel prefix or " + "${specialChar}chan ban #channel prefix except or " + "${specialChar}chan ban #channel prefix time or " + "${specialChar}chan ban #channel prefix except time." + " Time can be in the following format: 6D or 6d(day), 10M or 10m(minute)." + " Following are some common literals for time: Second - s/S, Minute - m/M, Hour - h/H, Day - d,D. " + "Others need not be implemented." type = 1 } else if(size > 3) { for(i in channelList) { if(i.retName() == strList.get(2)) { if(size == 4) { // 1st if(isPrefix(strList.get(3))) { retData = SendBan(strList.get(2),strList.get(3)) type = 2 } } else if(size == 5) { // 2nd n 3rd if(isPrefix(strList.get(3))) { if(isPrefix(strList.get(4))) { // 2nd retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4)) type = 2 } else if(parseTimeInput(strList.get(4)).third) { // 3rd val data = parseTimeInput(strList.get(4)) val timeSecs = setTimeInNumbers(data.second) Timer().schedule(timeSecs) { retData = SendBan(strList.get(2),strList.get(3)) } } } } else if(size == 6) { if(isPrefix(strList.get(3)) && isPrefix(strList.get(4)) && parseTimeInput(strList.get(5)).third) { val data = parseTimeInput(strList.get(4)) val timeSecs = setTimeInNumbers(data.second) Timer().schedule(timeSecs) { retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4)) } } } } } } }
m
Put the code inside triple backticks: ```
o
Copy code
else if(strList.get(1) == "ban") {
                       var found = false
                       if(size <= 3) {
                           retData = "Usage: ${specialChar}chan ban #channel prefix or " +
                                   "${specialChar}chan ban #channel prefix except or " +
                                   "${specialChar}chan ban #channel prefix time or " +
                                   "${specialChar}chan ban #channel prefix except time." +
                                   " Time can be in the following format: 6D or 6d(day), 10M or 10m(minute)." +
                                   " Following are some common literals for time: Second - s/S, Minute - m/M, Hour - h/H, Day - d,D. " +
                                   "Others need not be implemented."
                           type = 1
                       } else if(size > 3) {
                           for(i in channelList) {
                               if(i.retName() == strList.get(2)) {
                                   if(size == 4) { // 1st
                                       if(isPrefix(strList.get(3))) {
                                           retData = SendBan(strList.get(2),strList.get(3))
                                           type = 2
                                       }
                                   } else if(size == 5) { // 2nd n 3rd
                                       if(isPrefix(strList.get(3))) {
                                           if(isPrefix(strList.get(4))) { // 2nd
                                               retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4))
                                               type = 2
                                           } else if(parseTimeInput(strList.get(4)).third) { // 3rd
                                               val data = parseTimeInput(strList.get(4))
                                               val timeSecs = setTimeInNumbers(data.second)
                                               Timer().schedule(timeSecs) {
                                                   retData = SendBan(strList.get(2),strList.get(3))
                                               }
                                           }
                                       }
                                   } else if(size == 6) {
                                       if(isPrefix(strList.get(3)) && isPrefix(strList.get(4)) &&
                                               parseTimeInput(strList.get(5)).third) {
                                           val data = parseTimeInput(strList.get(4))
                                           val timeSecs = setTimeInNumbers(data.second)
                                           Timer().schedule(timeSecs) {
                                               retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4))
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
m
You can edit your original comment. 😉
o
Copy code
Timer().schedule(timeSecs) {
                                                   retData = SendBan(strList.get(2),strList.get(3))
                                               }
this part of the code needs to execute SendBan and wait for timeSecs seconds
idk, i dont remember what schedule did
like how do you put a function call and store data in a variable at the same time
m
You're kinda prone to race conditions when you assign stuff to
retData
like that.
o
ok
so should i declare a val somethingFoo = SendBan(...) and then at the end of the onCommand assign tht variable to retData
or is there a better way ?
m
Also, the timer will probably fire long after you have returned from your function, so at that time the
retData
variable no longer exists.
Assuming
retData
is defined inside your function, like it is in
onCommand
.
o
basically the idea is this the if statement checks ban, calls SendBan and waits timesecs, then sends a SendUnban
m
Ok. Instead of assigning the
SendBan
to a variable, I think you should send it as a message on a channel.
o
like how?
m
The component which is reading from
retData
should instead subscribe to the channel, and act on the
SendBan
message.
o
ok
so how do i create this channell, is it a coroutine or something ?
I guess you could use an actor.
You can read about both channels and actors there.
o
ok
so can you tell me what basically happens here >
m
Do you mean with
Timer.schedule
?
o
no this channel and actor
m
That's too much info to write down in a chat-thread 😉 I think you will find everything you need in that coroutines-guide.
o
okies
thnx
m
No problem
o
also one more thing, will using a coroutine inside a plugin do any harm ?
g
What is “plugin”? What kind harm? Coroutines is just an abstraction and library for asyncronous programming
If you use coroutines I don’t understand why do you need Timer.schedule if you can just use
delay
from kotlinx.coroutines
👍 2