.net-core - 如何在 Discord.NET 中通过公会 ID 和频道 ID 发送消息
问题描述
美好的一天,我被我的不和谐机器人困住了。我正在使用 Discord.NET 库,我的任务是在直接消息中获取命令并将其发送到特定不和谐公会中的特定频道。我知道频道 ID 和公会 ID,但我不知道如何使用它们。
[Command("resource")]
public async Task SendResource(string url = null, [Remainder]string description = null)
{
if (url == null)
{
await ReplyAsync("...");
return;
}
if (description == null)
{
await ReplyAsync("...");
return;
}
string text = $"**New resource:** {url}\n**Description:** {description}";
await Context.Guild.GetTextChannel(863427166662557696).SendMessageAsync(text);
}
await Context.Guild.GetTextChannel(863427166662557696).SendMessageAsync(text);
将在同一个公会中找到频道,但如果命令是在直接消息中执行的,它将不起作用
我做了一些研究,发现 GetGuild(id) 方法将公会作为一个对象,但调用此方法属于在 CommandHandler 中声明的 DiscordSocketClient
如何从直接消息中执行的命令将消息发送到某个不和谐的内疚频道?谢谢!
解决方案
由于我遵循 discord.net 指南模板,因此我只是在我的类构造函数中添加了 DiscordSocketClient 客户端,它就可以工作了。
推荐阅读
- android - 类型不匹配。要求:观察员
成立:? - git - 它是存储库中推送目标文件夹的好习惯吗?
- android - 具有自动连接 true 的 Android connectGatt 从未调用过`onConnectionStateChange`
- c# - 如何从表中获取所有链接的记录
- csv - 如何在 DataStage 中插入带引号的值?
- php - 如何使用嵌套的 if / else 语句(表单检查)
- swiftui - SwiftUI GeometryReader 不会在中心布局自定义子视图
- c# - UWP 程序与 Winforms 的功能不同
- angular - 如何在尤里卡服务器上注册角度应用程序
- python - Cython 无法导入和执行函数:存储临时 Python 参考的不安全 C 派生