如何使用 getMessageAsync((ulong)id) 将 Discord.IMessage 转换为字符串?
How do i convert Discord.IMessage to string, using getMessageAsync((ulong)id)?
我正在尝试将不和谐频道中发送的最后一条消息用作 windows 表单应用程序中的字符串,但我似乎无法将 IMessage
转换为 string
.我也无法获取上次发送的消息 ID
。
使用 GetMessagesAsync(10).First().ID
returns 3 位而不是 18 位?不过,使用来自 discord 的实际消息 ID 是可行的。
非常感谢任何帮助。
try
{
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();
}
catch
{
MessageBox.Show("Unable to connect.");
return;
}
Client.Ready += getMessage;
private async Task getMessage()
{
ulong msgid = (ulong)Client.GetGuild(525039283917291520).GetTextChannel(532107460807491584).GetMessagesAsync(10).FirstOrDefault().Id;
var channel = Client.GetChannel(532107460807491584) as SocketTextChannel;
var msg = channel.GetMessageAsync(msgid); // id is 3 digits instead of 18? not sure why.
Console.WriteLine("MESSAGE: " + msg);
}
private Task Client_Log(LogMessage arg)
{
Invoke((Action)delegate
{
Console.WriteLine(arg);
});
return null;
}
您需要 await
您 GetMessageAsync
方法,因为它是 Task<IMessage>
。然后将每个消息内容存储在 Content
属性 中。此外,您之前已经获取了消息对象,无需检索同一消息的另一个实例。
理想情况下,就绪处理程序应如下所示:
private async Task OnReadyAsync()
{
// attempt to get the channel
var channel = _client.GetChannel(_channelId);
// pattern match to SocketMessageChannel, the standard type required for a channel to be a messageable one
if (!(channel is ISocketMessageChannel msgChannel)) return;
// await the GetMessageAsync task to get the result of this task
var msg = await msgChannel.GetMessageAsync(_msgId);
// print Content if msg exists
if (msg != null) Console.WriteLine(msg.Content);
}
参考文献:
多亏了 Still H 的代码,它才开始工作。我只是修改它以获取发送的最后一条消息的 ID :) 这是供稍后查看此信息的任何人使用的代码;
private async Task clientReady()
{
Channel = Client.GetChannel(525085451598692364) as SocketTextChannel;
if (!(Channel is ISocketMessageChannel msgChannel)) return;
var msgId = (await Channel.GetMessagesAsync(1).FlattenAsync()).FirstOrDefault().Id;
var msg = await msgChannel.GetMessageAsync(msgId);
if (msg != null) Console.WriteLine(msg.Content + " ID: ");
}
我正在尝试将不和谐频道中发送的最后一条消息用作 windows 表单应用程序中的字符串,但我似乎无法将 IMessage
转换为 string
.我也无法获取上次发送的消息 ID
。
使用 GetMessagesAsync(10).First().ID
returns 3 位而不是 18 位?不过,使用来自 discord 的实际消息 ID 是可行的。
非常感谢任何帮助。
try
{
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();
}
catch
{
MessageBox.Show("Unable to connect.");
return;
}
Client.Ready += getMessage;
private async Task getMessage()
{
ulong msgid = (ulong)Client.GetGuild(525039283917291520).GetTextChannel(532107460807491584).GetMessagesAsync(10).FirstOrDefault().Id;
var channel = Client.GetChannel(532107460807491584) as SocketTextChannel;
var msg = channel.GetMessageAsync(msgid); // id is 3 digits instead of 18? not sure why.
Console.WriteLine("MESSAGE: " + msg);
}
private Task Client_Log(LogMessage arg)
{
Invoke((Action)delegate
{
Console.WriteLine(arg);
});
return null;
}
您需要 await
您 GetMessageAsync
方法,因为它是 Task<IMessage>
。然后将每个消息内容存储在 Content
属性 中。此外,您之前已经获取了消息对象,无需检索同一消息的另一个实例。
理想情况下,就绪处理程序应如下所示:
private async Task OnReadyAsync()
{
// attempt to get the channel
var channel = _client.GetChannel(_channelId);
// pattern match to SocketMessageChannel, the standard type required for a channel to be a messageable one
if (!(channel is ISocketMessageChannel msgChannel)) return;
// await the GetMessageAsync task to get the result of this task
var msg = await msgChannel.GetMessageAsync(_msgId);
// print Content if msg exists
if (msg != null) Console.WriteLine(msg.Content);
}
参考文献:
多亏了 Still H 的代码,它才开始工作。我只是修改它以获取发送的最后一条消息的 ID :) 这是供稍后查看此信息的任何人使用的代码;
private async Task clientReady()
{
Channel = Client.GetChannel(525085451598692364) as SocketTextChannel;
if (!(Channel is ISocketMessageChannel msgChannel)) return;
var msgId = (await Channel.GetMessagesAsync(1).FlattenAsync()).FirstOrDefault().Id;
var msg = await msgChannel.GetMessageAsync(msgId);
if (msg != null) Console.WriteLine(msg.Content + " ID: ");
}