【问题标题】:Discord.Net 2.0 send message to specific channelDiscord.Net 2.0 向特定频道发送消息
【发布时间】:2021-01-26 12:58:40
【问题描述】:
Public Async Function Kick(ByVal user As IGuildUser, <Remainder> ByVal reason As String) As Task
        Dim guild = Context.Guild
        Dim bot = Context.Client
        Dim message = Context.Message
        Dim u = Context.User
'Channel Info
        Dim _client As New DiscordSocketClient
        Dim id As ULong = 1235234987 'Random numbers not a channel id
        Dim chnl As IMessageChannel = _client.GetChannel(id)

        If u.Id = "id" Or "id" Or "id" Then

            Dim embed As New EmbedBuilder With {
                .Author = New EmbedAuthorBuilder With {
                .IconUrl = u.GetAvatarUrl,
                .Name = u.Username
            },
            .Title = $"{user.Username}#{user.Discriminator}'s Kick Information",
            .ImageUrl = "https://i.imgur.com/vc241Ku.jpeg",
            .Description = reason,
            .Color = New Color(masterClass.randomEmbedColor),
            .ThumbnailUrl = user.GetAvatarUrl,
            .Timestamp = Context.Message.Timestamp,
            .Footer = New EmbedFooterBuilder With {
                    .Text = "Kick Data",
                    .IconUrl = guild.IconUrl
                }
            }

            Await chnl.SendMessageAsync("", False, embed.Build())
            Await user.SendMessageAsync("", False, embed.Build())
            Await user.KickAsync(reason)
        Else

            Await Context.Channel.SendMessageAsync("You do not match the IDs that are required for this. Bye.")
        End If



    End Function

这是我的 kick 命令,我将如何制作它以便将嵌入发送到特定频道?我已经设置了它,所以它会向用户发送嵌入,但我希望它现在将该嵌入发送到服务器中的某个通道。也是一个旁注。有谁知道我可以在哪里在线托管机器人而不是自我托管或关于我可以在哪里托管它的好教程?

【问题讨论】:

  • Guild 获取你想要的频道,然后在上面调用SendMessage。公会有可用的方法,例如GetTextChannel
  • 当我执行 guild.GetTextChannelAsync(id).SendMessage("message") 时。它告诉我“SendMessage 不是任务(ITextChannel)的一部分”
  • 因为它不是...... Intellisense 没有将该方法显示为 SendMessageAsync() 吗?此外,您不能在任务上调用该方法。您需要在实际的 TextChannel 上调用它。如果您使用了 SocketGuild 实例,则不需要 Async 方法 GetTextchannelAsync...。相反,您可以使用 GetTextChannel
  • 当我执行 guild.GetText 时,唯一显示的是 GetTextChannelAsync。当在那些没有说什么消息的时候。当我做 Context.Guild.Get 时,结果是一样的。
  • 那么你的公会不是 SocketGuild 类型,可能是因为你的上下文不是 SocketCommandContext。如果您使用的是 Async 方法,您需要等待它,就像您应该使用任何 Async 任务一样

标签: vb.net discord.net


【解决方案1】:
Public Async Function Kick(ByVal user As IGuildUser, <Remainder> ByVal reason As String) As Task
    Dim guild = Context.Guild
    Dim chnl = guild.GetTextChannel(123456789)
    Dim embed As New EmbedBuilder With {
        .Author = New EmbedAuthorBuilder With {
            .IconUrl = user.GetAvatarUrl,
            .Name = user.Username
        },
        .Title = $"{user.toString()}'s Kick Information",
        .ImageUrl = "https://i.imgur.com/vc241Ku.jpeg",
        .Description = reason,
        .Color = New Color(masterClass.randomEmbedColor),
        .ThumbnailUrl = user.GetAvatarUrl,
        .Timestamp = Context.Message.Timestamp,
        .Footer = New EmbedFooterBuilder With {
                        .Text = "Kick Data",
                        .IconUrl = guild.IconUrl
                    }
        }

    Await chnl.SendMessageAsync("", False, embed.Build())
    Await user.SendMessageAsync("", False, embed.Build())
    Await user.KickAsync(reason)
End Function

【讨论】:

  • hastebin.com/hejagiwaxa.properties 这是整个班级。当我尝试执行 guild.GetTextChannel(id) 时,我对 SendMessage 一无所知。所以在函数参数中添加了 g 作为 SocketGuild,然后为我提供了 GetTextChannel 的正确属性。但是当我尝试执行命令时没有任何反应。我正在使用 discord.net v2.2.0
  • 就像我在之前的评论中所说,您的上下文不是 SocketCommandContext 的上下文。你应该使用Inherits ModuleBase(Of SocketCommandContext)。您不能只在命令方法中添加随机参数。您添加的任何内容都需要包含在调用该命令的消息中。
  • 好的,已将(SocketCommandContext 的)添加到 ModuleBase。并创建变量 chnl = Context.Guild.GetTextChannel(id),然后执行 Await chnl.SendMessageAsync()。但我得到相同的结果,没有执行任何操作。
  • 所以此时您需要调试代码并查看发生了什么。此外,请确保您正确记录错误。如果您还没有这样做,则应该从您的方法签名中删除不是 SocketGuild 参数的一面
  • 好的...问题正在发送到所需的频道。如果我想将消息发送到执行命令的位置并向用户发送嵌入的 dm,则它可以正常工作。只是在添加 (Of SocketCommandContext) 时它不想执行。如果机器人有任何错误,不确定如何记录任何错误。是的,我已经删除了 SocketGuild 参数。感谢您尝试帮助我做一些看似非常简单的事情..
猜你喜欢
  • 2020-07-02
  • 2021-08-21
  • 2020-04-04
  • 2018-12-09
  • 2022-01-08
  • 2021-01-01
  • 2020-10-01
相关资源
最近更新 更多