微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Discord.NET 中通过公会 ID 和频道 ID 发送消息

如何解决如何在 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 客户端并且它起作用了。

,
[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.Client.GetGuild(123456789).GetTextChannel(863427166662557696).SendMessageAsync(text);
}

注意
的变化 Context.Guild.GetTextChannel(channel_id).SendMessageAsync(text);

Context.Client.GetGuild(guild_id).GetTextChannel(channel_id).SendMessageAsync(text);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。