命令权限 Discord.net

Command permissions Discord.net

用户类型:"test", 如果用户具有角色“vip”,则消息显示“you have permission

其他“you dont have permission

我的代码:

  Public Async Function onMsg(message As SocketMessage) As Task

   If message.Source = MessageSource.Bot Then
   Else 
   If message.Content.Contains("test") Then

            Dim userName As SocketGuildUser
            Dim user = TryCast(message.Author, SocketGuildUser)
            Dim role = (TryCast(user, IGuildUser)).Guild.Roles.FirstOrDefault(Function(x) x.Name = "vip")

            If Not userName.Roles.Contains(role) Then

                Await message.Channel.SendMessageAsync("you have permission")
            Else
                Await message.Channel.SendMessageAsync("you dont have permission")
            End If


        End If

   End Function

一个简单的技巧,如果你的vip会员可以算作不多的会员,你可以select他们的id让他们获得许可。

If message.Content.Contains("test") Then
If message.Author.Mention = "<@!ID>" Then  'vip's permission |OrElse statement
Await message.Channel.SendMessageAsync("you have permission")
Else
Await message.Channel.SendMessageAsync("you dont have permission")
End If

首先,对于创建一般命令,您应该考虑使用 Discord.Net 中提供的 Command Service,而不是 If/Else检查您收到的消息事件处理程序。

您的代码也有错误。你声明了一个 userName 变量,但实际上从未给它赋值。然而你试图做 userName.Roles

解决方案:

If message.Source = MessageSource.Bot Then
    Return
ElseIf message.Content.Contains("test") Then

    Dim user = TryCast(message.Author, SocketGuildUser)

    'If casting the user to a SocketGuildUser was not successful, then exit
    If user Is Nothing Then Return 'This can occur if a user message the bot via Direct Messages

    'Using "Any" returns true if a match is found, false otherwise
    If user.Roles.Any(Function(role) role.Name.Equals("VIP", StringComparison.OrdinalIgnoreCase)) Then
        Await message.Channel.SendMessageAsync("you have permission")
    Else
        Await message.Channel.SendMessageAsync("you dont have permission")
    End If
End If