kordlib / kord

Idiomatic Kotlin Wrapper for The Discord API
MIT License
911 stars 80 forks source link

Looking for examples of sending messages without events #466

Closed thunderbiscuit closed 2 years ago

thunderbiscuit commented 2 years ago

Hi there, and thanks for the awesome library!

I was able to build the basic ping pong example within an hour, and everything works out of the gate. Great stuff. One thing that is stumping me, however, is that I am so far unable to just "send" a message without a particular event happening (not relying on the kord.on<> { } extension function.

I'm building a bot that is basically an external sentinel to a service I built, and I'd like the bot to be able to send an alert in a particular channel for devs whenever my service is not available, or when certain thresholds internal to my service are met. In that way, I don't want to bot to respond to any particular message, I need it to be able to send its own messages whenever it wants.

I have the proper snowflake for the guild and channel and I'm trying to play with the Channel and MessageBehavior objects, but so far I haven't been able to make it work.

I'm wondering if you have simple examples of that somewhere, or if you could maybe add one to the wiki. Cheers!

HopeBaron commented 2 years ago

Hello @thunderbiscuit,

You are not obliqued to send messages in events only. However, you need to get a MessageChannelBehavior at least. use our getChannelOf<MessageChannelBehavior> or getChannelOrNull<MessageChannelBehavior> on the Kord instance

same applies for a MessageBehavior you can use the channelOf function to switch the type of channel.

TL;DR: You need MessageChannelBehavior at least to send a message.

Note: depending on the version channelOf for Channels may be called of in older versions

thunderbiscuit commented 2 years ago

Thanks! I'm now able to build the message (I think?), but not clear on how to fire it.

val messageChannelBehaviour: MessageChannelBehavior? = discordClient.getChannelOf(id = sentinelChannelSnowflake)
val message = messageChannelBehaviour?.createMessage {
    content = "Hello World!"
}

I tried the publish() method on the message object but that's not right. I think part of my problem is that my mental model for the API and the library are not quite well formed. How would you go about sending just a "Hello World!" message in a given channel of a given server?

HopeBaron commented 2 years ago

Functions in Kord don't use call backs, when you call createMessage, the message is created and returned to you. if you seek to make a pre-built message to send over and over again, you could store a builder (but make sure not to mutate builders as they are not thread-safe) and provide it as the parameter

For example:

val builder = MessageCreateBuilder()
builder.content = "A message"
behavior.createMessage(builder)
thunderbiscuit commented 2 years ago

I think I'm following you, but for some reason it's not quite clicking yet. I'm also not trying to pre-build anything, I mostly just want to fire up the bot once an hour and for it to post in our channel if something is wrong.

You say

the message is created and returned to you

Do you mean by that that the message is posted in the channel (and then stored in the variable?). If that is the case the error is on my configuration, as the bot runs but doesn't post anything at the moment, using the following:

// say my channel's snowflake is 1234
val discordClient = Kord(discordToken)
discordClient.login()
val sentinelChannelSnowflake: Snowflake = Snowflake(1234)
val messageChannelBehaviour: MessageChannelBehavior? = discordClient.getChannelOf(id = sentinelChannelSnowflake)

messageChannelBehaviour?.createMessage {
    content = "Hello World!"
}
discordClient.logout()

Any idea where I might be going wrong?

EDIT: My problem was the login(). Got it working now. Cheers!

thunderbiscuit commented 2 years ago

Thank you for your help! I understand better how the MessageChannelBehaviour works now. Will keep building/working on the bot. For now all basic functionalities are working!