gpt4 book ai didi

c# - 给定已连接且就绪的 DiscordSocketClient 和 Discord Channel Id,如何向该 channel 发送消息?

转载 作者:行者123 更新时间:2023-11-30 23:10:19 27 4
gpt4 key购买 nike

我正在尝试设置自动消息。

当我设置我的客户端时,我使用:

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"
}
}
}

我试过这样获取 channel :

var channel = client.GetChannel((ulong) rotation.ChannelId);

它给了我一个 SocketChannel 并且也是这样的:

var channel = client.Guilds
.SelectMany(g => g.Channels)
.SingleOrDefault(c => c.Id == rotation.ChannelId);

这给了我一个 SocketGuildChannel。这些都没有给我直接向 channel 发送消息的选项。我已经尝试研究如何做到这一点,但没有找到任何东西......文档似乎没有任何例子......

这似乎是一件简单的事情,但我对此束手无策。有人知道怎么做吗?

最佳答案

这是因为 SocketGuildChannelSocketChannel 都可以是语音或文本 channel 。

相反,您需要 ISocketMessageChannelIMessageChannelSocketTextChannel

要得到这个,你可以简单地转换你得到的 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");

关于c# - 给定已连接且就绪的 DiscordSocketClient 和 Discord Channel Id,如何向该 channel 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45404178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com