Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Discord.Net根据用户输入提供角色_Vb.net_Discord_Discord.net - Fatal编程技术网

Vb.net Discord.Net根据用户输入提供角色

Vb.net Discord.Net根据用户输入提供角色,vb.net,discord,discord.net,Vb.net,Discord,Discord.net,因此,我创建了一个role命令,但我使用该命令根据用户输入来指定角色。我现在没有太多的工作,但这就是我目前拥有的 Imports Discord Imports Discord.Commands Imports Discord.WebSocket <Group("giverole")> Public Class giverole Inherits ModuleBase(Of ICommandContext) ' Private user = Co

因此,我创建了一个role命令,但我使用该命令根据用户输入来指定角色。我现在没有太多的工作,但这就是我目前拥有的

Imports Discord
Imports Discord.Commands
Imports Discord.WebSocket

<Group("giverole")>
Public Class giverole
    Inherits ModuleBase(Of ICommandContext)
    ' Private user = Context.User
    ' Private role As IRole
    '<Remainder> ByVal r As String
    'Dim author = Context.Message.Author

    <Command("weebers")>
    Private Async Function giveRole() As Task
        'Await DirectCast(author, SocketGuildUser).AddRoleAsync(role)
        'Await CType(Context.User, SocketGuildUser).AddRoleAsync(role)
        'Dim role = Context.Guild.Roles.FirstOrDefault(Function(x) x.Name.ToString() = "WEEBERS")
        'Await user.AddRoleAsync(role)
        'Dim user = Context.User
        'Dim role = Context.Guild.Roles.FirstOrDefault(Function(x) x.Name = "WEEBERS")

        Dim roleid As ULong = 486599202093269012
        Dim role = DirectCast(Context.Message.Channel, IGuildChannel).Guild.GetRole(roleid)
        Await DirectCast(Context.User, SocketGuildUser).AddRoleAsync(role)
        Await ReplyAsync(role.ToString)

    End Function

End Class
导入不一致
导入不一致命令
导入Discord.WebSocket
公共类角色
继承ModuleBase(ICommandContext的)
'Private user=Context.user
“作为IRole的私人角色
'ByVal r作为字符串
'Dim author=Context.Message.author
专用异步函数giveRole()作为任务
'Await DirectCast(作者,SocketGuildUser)。AddRoleAsync(角色)
'Await CType(Context.User,SocketGuildUser).AddRoleAsync(角色)
'Dim role=Context.Guild.Roles.FirstOrDefault(函数(x)x.Name.ToString()=“WEEBERS”)
'等待用户.addRoleSync(角色)
'Dim user=Context.user
'Dim role=Context.Guild.Roles.FirstOrDefault(函数(x)x.Name=“WEEBERS”)
尺寸roleid为ULong=486599202093269012
Dim role=DirectCast(Context.Message.Channel、IGuildChannel.Guild.GetRole(roleid)
等待DirectCast(Context.User,SocketGuildUser).AddRoleAsync(角色)
等待回复同步(role.ToString)
端函数
末级

即使这可以编译,它也没有给我角色,即使只有命令角色。昨晚我有这个工作,但现在当我醒来时它坏了,给我一个对象没有被引用的错误。不知何故,我设法摆脱了这个错误,但现在这个角色将无法分配。现在,我真正想做的是根据用户的输入为其提供角色。你可以从我的评论中看到。我已经尝试了一段时间如何让它工作。

这应该会帮助您现在您只需添加检查以检查开始发送的命令,并根据

Private Async Function OnMessage(rawmsg As SocketUserMessage) As Task Handles Discord.MessageReceived
    If rawmsg.Author.IsBot = True Then
        'ignoring msgs from bots
    Else
        Dim context As New SocketCommandContext(Discord, rawmsg)
        If rawmsg.Content.StartsWith("-giverole") Then 'adding the command
            Dim role As String = rawmsg.Content.Replace("-giverole ", "") 'removing -giverole from it

            If role = "Admin" OrElse role = "bots" Then 'blacklist
                Await rawmsg.Channel.SendMessageAsync(rawmsg.Author.Mention & " nice try")
            Else
                For Each r In context.Guild.Roles
                    If r.ToString = role Then 'role exists
                        Dim role2 = context.Guild.Roles.FirstOrDefault(Function(x) x.Name = r.ToString)
                        Await DirectCast(context.User, SocketGuildUser).AddRoleAsync(role2)
                    End If
                Next
            End If
        End If
    End If
End Function

首先,我建议使用
SocketCommandContext
而不是
ICommandContext

第二,我相信你的命令是为了被发现而公开的

就代码而言,如果使用
SocketCommandContext

继承ModuleBase(SocketCommandContext的)
公共异步函数giveRole()作为任务
尺寸roleid为ULong=486599202093269012
Dim role=Context.Guild.GetRole(roleid)
等待DirectCast(Context.User,SocketGuildUser).AddRole(role)
等待回复同步(role.Name)
端函数
这段代码实际上将角色赋予执行命令的用户。我不确定这是不是你想要的

注意:您应该能够将用户和角色作为参数传递给命令,因此理论上您可以为任何用户提供任何角色(某些限制适用于指定场景)


这和C有什么关系?看起来您有一个VB.NET问题,正在寻找一个VB.NET解决方案。抱歉,请编辑掉C#。您好!我不知道怎么检查?我在考虑一个if语句,但我不确定如何使它起作用。这是我试图做的,但不确定这是否是应该的。我没有出错,但什么也没发生。猜测它没有任何作用,因为if语句没有返回true。另外,我不确定,但我必须添加另一个变量“Private with events Discord As DiscordSocketClient”你能给我解释一下你的命令是如何工作的吗?基本上,如果有人键入-giverole rolename,有两种方法可以实现这一点,一种是基于白名单的函数,另一种是基于黑名单的白名单,你必须列出每个可用的角色。黑名单你只会列出那些你不想得到的,你想要哪一个?你好!你说得对,我的职能必须公开才能被认可。起初,它不想工作,但在我删除(SocketCommandContext)后,它工作了。谢谢。我已经试着让它工作了大约3天了,但我无法用手指去触摸它。