Can't edit last message.
minneelyyyy opened this issue · 24 comments
after using discord_create_message, how do i get the messages ID so i can edit it?
You can find documentation for the message
structure here.
Each message structure has the id
field. You just need to scroll down the collaboration chart. First field.
it doesn't return a message type though. can you not get the message from the call to discord_create_message?
void on_message(struct discord *client, const struct discord_message *msg)
{
if (strcmp(msg->content, "ping") != 0)
return;
struct discord_create_message params = { .content = "pong" };
struct discord_ret_message message;
discord_create_message(client, msg->channel_id, ¶ms, &message);
log_info("%lu\n", message.id);
}
something similar to this?
All Concord functions return a status code. It might be a bit easy to miss, but you have the message
structure, with the type discord_ret_message
. That is the created message that Discord sends back. You can access id of the message by doing:
message.id
Hope this helps.
it says src/main.c:24:30: error: ‘struct discord_ret_message’ has no member named ‘id’
My bad. The discord_ret_message
type will hold a reference to the created message. You can access it through this code:
message.sync->id
then i just get segmentation fault. :/
Strange. Can you post your current code?
#include <string.h>
#include <concord/discord.h>
#include "secret.h" // stores BOT_TOKEN
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Logged in as %s!", bot->username);
}
void on_message(struct discord *client, const struct discord_message *msg)
{
if (strcmp(msg->content, "ping") != 0)
return;
struct discord_create_message params = { .content = "pong" };
struct discord_ret_message message;
discord_create_message(client, msg->channel_id, ¶ms, &message);
log_info("%lu\n", message.sync->id);
}
int main(void)
{
struct discord *client = discord_init(BOT_TOKEN);
discord_set_on_ready(client, &on_ready);
discord_set_on_message_create(client, &on_message);
discord_run(client);
}
I don't have a config.json i just have main.c and secret.h is that a problem? I'm sorry i just don't fully understand the library.
Also any access to message causes a seg fault, i tried log_info("%p\n", message)
and it also caused a segfault
or is config.json just for the examples
Realistically that should not be a problem. Concord specifically supports use of the library without a config.json
.
Both are intended to work for the user, even outside of examples.
Without having you throw this through gdb or Valgrind, my first suspicion is that message.sync
is NULL
. Before I
have you do that, I would like to verify this. So could you replace:
log_info("%lu\n", message.sync->id);
with
printf("message.sync is NULL: %i\n", message.sync == NULL);
Remember to include stdio.h
for this modification.
it caused a segfault
Oops, my bad again. Sorry. Please run the edited code. (Refresh this page.)
still causes a segfault
Could you post your current code please?
#include <stdio.h>
#include <string.h>
#include <concord/discord.h>
#include <time.h>
#include "secret.h" // stores BOT_TOKEN
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Logged in as %s!", bot->username);
}
void on_message(struct discord *client, const struct discord_message *msg)
{
if (strcmp(msg->content, "ping") != 0)
return;
struct discord_create_message params = { .content = "pong" };
struct discord_ret_message message;
discord_create_message(client, msg->channel_id, ¶ms, &message);
printf("message.sync is NULL: %i\n", message.sync == NULL);
}
int main(void)
{
struct discord *client = discord_init(BOT_TOKEN);
discord_set_on_ready(client, &on_ready);
discord_set_on_message_create(client, &on_message);
discord_run(client);
}
does this code work for you if you don't mind testing it?
Okay. I will test it.
Found the issue. Your message
structure is uninitialized. Please replace
struct discord_ret_message message;
with
struct discord_ret_message message = {0};
I still can't get the message id though it still segfaults
Can I see your current code?
message.sync is null
#include <stdio.h>
#include <string.h>
#include <concord/discord.h>
#include <time.h>
#include "secret.h" // stores BOT_TOKEN
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("Logged in as %s!", bot->username);
}
void on_message(struct discord *client, const struct discord_message *msg)
{
if (strcmp(msg->content, "ping") != 0)
return;
struct discord_create_message params = { .content = "pong" };
struct discord_ret_message message = { 0 };
discord_create_message(client, msg->channel_id, ¶ms, &message);
printf("message: %p\n", message);
printf("message sync: %p\n", message.sync);
}
int main(void)
{
struct discord *client = discord_init(BOT_TOKEN);
discord_set_on_ready(client, &on_ready);
discord_set_on_message_create(client, &on_message);
discord_run(client);
}
Odd. This seems to happen to me as well. This very well may be a library bug.
If you need to communicate with us and other Concord users, you may also want to join our Discord server.
@lcsmuller Think you could take a look at this when you have the time?
You're using the wrong method. If you want to do it synchronously, which will block the event loop:
void on_message(struct discord *client, const struct discord_message *msg)
{
if (strcmp(msg->content, "ping") != 0)
return;
struct discord_create_message params = { .content = "pong" };
struct discord_message sent;
struct discord_ret_message message = { .sync = &sent };
discord_create_message(client, msg->channel_id, ¶ms, &message);
struct discord_edit_message params_edit = { .content = "edited message" };
discord_edit_message(client, sent.channel_id, sent.id, ¶ms_edit, &message);
}
otherwise, you need to employ the on_done
callback in struct discord_ret_message
It's just not possible to know the id of the message until you get the result
She fixed the problem with I assume your code. Thanks Anotra. Closing issue.