我正在尝试弄清楚当用户更改状态时如何将消息发送到不一致的频道。我正在尝试通过调用位于单独文件中的方法来做到这一点。
在我的Program.cs
public async Task StartAsync()
{
// Create instance of the class with the method I want to call
Games statusChange = new Games();
// call the method when a status changes
_client.GuildMemberUpdated += statusChange.UpdateGameBeingPlayed;
}
我要调用的类(在单独的文件中):
public class Games : ModuleBase<SocketCommandContext>
public async Task UpdateGameBeingPlayed(SocketGuildUser user, SocketUser userAgain)
{
// get channel id
string strGuildId = user.Guild.Id.ToString();
ulong ulongId = Convert.ToUInt64(strGuildId);
// get the channel I want to update
var channel = Context.Guild.GetChannel(ulongId) as SocketTextChannel; // Context here is undefined :(
await channel.SendMessageAsync("a user has changed status!");
}
}
在我尝试在Games类中使用Context
之前,该实现一直有效。上下文是不确定的(我想是因为不确定要获取的上下文)。
因此,我认为解决方案可能是从Program.cs文件中导入_client
并将其用于向我要更新的频道发送消息。但是,我不确定该怎么做。
所以我的问题如下:
谢谢!
答案 0 :(得分:1)
此处的海报:
答案是我毕竟不需要上下文来获取频道,您可以从SocketGuildUser获取频道。
工作代码:
public async Task UpdateGameBeingPlayed(SocketGuildUser user, SocketUser userAgain)
{
string strGuildId = user.Guild.Id.ToString();
ulong ulongId = Convert.ToUInt64(strGuildId);
var channel = user.Guild.GetChannel(ulongId) as SocketTextChannel;
await channel.SendMessageAsync("a user has changed status!");
}