0
有事件可用DiscordClient.UserJoined
,DiscordClient.UserLeft
和DiscordClient.UserUpdated
。虽然前两个我可以访问e.User.Name
以获取加入或离开服务器的用户名,但第三个事件没有这个。只有e.Server.CurrentUser
指的是机器人本身或e.Server.Users
,它给我所有用户的列表。但我不知道事件触发了哪一个,因此不知道哪一个访问。如何处理Discord bot的用户状态更改?
假设我想在用户上线时创建通知(将他/她的状态从脱机状态更改为在线状态或反之亦然)。对于UserJoined
事件中,我可以这样做:
client.UserJoined += async (s, e) =>
{
var channel = e.Server.FindChannels("abc", ChannelType.Text);
await channel.SendMessage(e.User.Name + " has joined the server!");
}
但我怎么能做到这一点的UserUpdated
时,我有没有User
?
client.UserUpdated += async (s, e) =>
{
var channel = e.Server.FindChannels("abc", ChannelType.Text);
if(???.Status == "online")
{
await channel.SendMessage(???.Name + " has come online!");
}
}
用户将是在e.Before和e.After,所以访问它使用e.After.Name –
这有点令人困惑,但现在我明白了结构。你是对的。如果你把它写成答案,我会将它标记为正确的。 – Andrew