命令权限Discord.net

时间:2019-05-08 06:51:49

标签: discord.net

用户类型:“测试”, 如果用户的角色为“ 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

2 个答案:

答案 0 :(得分:0)

首先,对于一般的命令创建,您应该考虑使用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

答案 1 :(得分:-1)

一个简单的技巧,如果您的VIP会员可以计为不多的会员,则可以选择他们的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