给定已连接且就绪的 DiscordSocketClient 和 Discord Channel Id,如何向该频道发送消息?
Given a connected and ready DiscordSocketClient and a Discord Channel Id, How does one send a message to that channel?
我正在尝试设置自动消息。
当我设置我的 client
时,我使用:
client.Ready += OnClientReady;
从那里开始我的 Scheduler
class:
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
class:
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
。这些都没有给我直接向频道发送消息的选项。我已经尝试研究如何做到这一点,但没有找到任何东西......文档似乎没有任何例子......
这似乎是一件简单的事情,但我束手无策。有人知道怎么做吗?
这是因为 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");
我正在尝试设置自动消息。
当我设置我的 client
时,我使用:
client.Ready += OnClientReady;
从那里开始我的 Scheduler
class:
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
class:
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
。这些都没有给我直接向频道发送消息的选项。我已经尝试研究如何做到这一点,但没有找到任何东西......文档似乎没有任何例子......
这似乎是一件简单的事情,但我束手无策。有人知道怎么做吗?
这是因为 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");