我正在尝试设置自动消息。
当我设置client
时,我使用:
client.Ready += OnClientReady;
从那里开始我的Scheduler
课程:
private Task OnClientReady()
{
var scheduler = new Scheduler(client);
scheduler.Start();
return Task.CompletedTask;
}
看起来像这样:
public class Scheduler
{
private readonly DiscordSocketClient _client;
private static Timer _timer;
public void Start(object state = null)
{
Sender.Send(_client);
_timer = new Timer(Start, null, (int)Duration.FromMinutes(1).TotalMilliseconds, 0);
}
public Scheduler(DiscordSocketClient client)
{
_client = client;
}
}
当计时器滴答时,它会调出并将client
传递给下面的Sender
课程:
public static class Sender
{
public static void Send(DiscordSocketClient client)
{
var currentLocalDateTime = SystemClock.Instance.InTzdbSystemDefaultZone().GetCurrentLocalDateTime();
var elapsedRotations = new List<Rotations>();
using (var db = new GOPContext())
{
elapsedRotations = db.Rotations
.Include(r => r.RotationUsers)
.Where(r => r.LastNotification == null ||
Period.Between(r.LastNotification.Value.ToLocalDateTime(),
currentLocalDateTime).Hours >= 23)
.ToList();
}
foreach (var rotation in elapsedRotations)
{
var zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(rotation.Timezone);
var zonedDateTime = SystemClock.Instance.InZone(zone).GetCurrentZonedDateTime();
if (zonedDateTime.Hour != 17)
continue;
//I need to send a message to the channel here.
//I have access to the connected / ready client,
//and the channel Id which is "rotation.ChannelId"
}
}
}
我尝试过这样的频道:
var channel = client.GetChannel((ulong) rotation.ChannelId);
给了我一个SocketChannel
,也是这样的:
var channel = client.Guilds
.SelectMany(g => g.Channels)
.SingleOrDefault(c => c.Id == rotation.ChannelId);
给了我一个SocketGuildChannel
。这些都不能让我选择直接向频道发送消息。我试过研究如何做到这一点,但没有找到任何东西......文档似乎没有任何这方面的例子......
这似乎是一件很简单的事情,但是我的智慧结束了。有谁知道怎么做?
答案 0 :(得分:2)
这是因为SocketGuildChannel
和SocketChannel
都可以是语音或文字频道。
相反,您需要ISocketMessageChannel
,IMessageChannel
或SocketTextChannel
要获得此功能,您只需投射SocketChannel
即将获得
var channel = client.GetChannel((ulong) rotation.ChannelId);
var textChannel = channel as IMessageChannel;
if(textChannel == null)
// this was not a text channel, but a voice channel
else
textChannel.SendMessageAsync("This is a text channel");