Discord.Net 根据用户输入赋予角色
Discord.Net give role based on user input
所以我创建了一个角色命令,但我的命令是根据用户输入赋予角色。我现在没有太多工作,但这就是我目前的工作。
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
尽管这可以编译,但即使仅使用命令 -giverole 也不能给我角色。我昨晚有这个工作,但现在当我醒来时它坏了,给我一个对象没有被引用的错误。不知何故我设法摆脱了那个错误,但现在角色不会被分配。现在我真正想做的是根据用户的输入赋予用户角色。正如您在我的评论中看到的那样。我已经尝试了一段时间如何让它工作。
这应该可以帮助您,现在您只需添加检查开始发送的命令并根据
分配角色
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
。
其次,我相信您的命令应该是 public 以便被发现。
就代码而言,如果使用 SocketCommandContext
Inherits ModuleBase(Of SocketCommandContext)
<Command("weebers")>
Public Async Function giveRole() As Task
Dim roleid As ULong = 486599202093269012
Dim role = Context.Guild.GetRole(roleid)
Await DirectCast(Context.User, SocketGuildUser).AddRole(role)
Await ReplyAsync(role.Name)
End Function
这段代码实际上将角色赋予了执行命令的用户。不确定这是否是您想要的。
NOTE: You should be able to pass both the user and the role as parameters to the command so in theory you can give any user any role (some whatever restriction applied to specified scenarios)
所以我创建了一个角色命令,但我的命令是根据用户输入赋予角色。我现在没有太多工作,但这就是我目前的工作。
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
尽管这可以编译,但即使仅使用命令 -giverole 也不能给我角色。我昨晚有这个工作,但现在当我醒来时它坏了,给我一个对象没有被引用的错误。不知何故我设法摆脱了那个错误,但现在角色不会被分配。现在我真正想做的是根据用户的输入赋予用户角色。正如您在我的评论中看到的那样。我已经尝试了一段时间如何让它工作。
这应该可以帮助您,现在您只需添加检查开始发送的命令并根据
分配角色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
。
其次,我相信您的命令应该是 public 以便被发现。
就代码而言,如果使用 SocketCommandContext
Inherits ModuleBase(Of SocketCommandContext)
<Command("weebers")>
Public Async Function giveRole() As Task
Dim roleid As ULong = 486599202093269012
Dim role = Context.Guild.GetRole(roleid)
Await DirectCast(Context.User, SocketGuildUser).AddRole(role)
Await ReplyAsync(role.Name)
End Function
这段代码实际上将角色赋予了执行命令的用户。不确定这是否是您想要的。
NOTE: You should be able to pass both the user and the role as parameters to the command so in theory you can give any user any role (some whatever restriction applied to specified scenarios)