discord - Discord.Net 如何列出组中的所有命令
问题描述
<Group("math")>
Public Class cmd_math
Inherits ModuleBase
#Region "Add"
<Command("add")>
Public Async Function cmdAdd(ByVal num1 As Integer, <Remainder> ByVal num2 As Integer) As Task
Dim sum = num1 + num2
Dim user = Context.User
Dim channel = Context.Channel
Await channel.SendMessageAsync($"{user.Mention} the sum of the two specified numbers are {sum}")
End Function
#End Region
#Region "Subtract"
<Command("sub")>
Public Async Function cmdSub(ByVal num1 As Integer, <Remainder> ByVal num2 As Integer) As Task
Dim sum = num1 - num2
Dim user = Context.User
Dim channel = Context.Channel
Await channel.SendMessageAsync($"{user.Mention} the sum of the two specified numbers are {sum}")
End Function
#End Region
#Region "Multiply"
<Command("multi")>
Public Async Function cmdMulti(ByVal num1 As Integer, <Remainder> ByVal num2 As Integer) As Task
Dim sum = num1 * num2
Dim user = Context.User
Dim channel = Context.Channel
Await channel.SendMessageAsync($"{user.Mention} the sum of the two specified numbers are {sum}")
End Function
#End Region
#Region "Divide"
<Command("divide")>
Public Async Function cmdDivide(ByVal num1 As Integer, <Remainder> ByVal num2 As Integer) As Task
Dim sum = num1 / num2
Dim user = Context.User
Dim channel = Context.Channel
Await channel.SendMessageAsync($"{user.Mention} the sum of the two specified numbers are {sum}")
End Function
#End Region
End Class
我将如何创建一个命令,比如说“列表”,然后它会自动发送一个带有命令列表的嵌入,而无需编写嵌入并自动填充它?如果不能使用常规图像在嵌入中完成,则很好。我很确定它会为此使用 for 循环,但在那之后,我不知道如何接受它。
解决方案
在你的程序中的某个地方你可能会有类似的东西
Dim collection As New ServiceCollection()
collection.AddSingleton(DiscordSocketClient())
或者您将客户端添加到您的IserviceProvider
YouServiceProvider
是用于依赖注入的。要注入命令服务,您需要将其添加到ServiceCollection
collection.AddSingleton(New CommandService())
要将其注入您的命令模块,只需向您的构造函数添加一个参数
Public Class YourCommandModule
Inherits ModuleBase(Of SocketCommandContext)
Private ReadOnly Property Commands As CommandService
Public Sub New(commands As CommandService)
Me.Commands = commands
End Sub
然后你可以在那个类中创建你的帮助命令,它现在可以访问CommandService
<Command("Help")>
Public Async Function Help() As Task
'Create your embed builder
'...
'You can access all module classes using `CommandService.Modules`
For Each moduleData As ModuleInfo In Commands.Modules
Dim cmds As List(CommandInfo) = moduleData.Commands 'this gives a list of all commands in this class
'you can now do something with that list of commands
'add each one to a embed field for example
Next
End Function
您可以通过 ModuleInfo 和 CommandInfo 查看您可以访问的不同内容
推荐阅读
- core-data - 在 SwiftUI 中使用 @FetchRequests 的性能成本
- javascript - 如何修复 React(功能组件)中的错误。“注册错误:TypeError:无法读取 null 的属性(读取'协议')”
- firebase - Firebase 功能云消息通知未收到
- python - 如何在熊猫列中应用 Geopy?
- database - 如何将 DOCUVALUE 表链接到相关的业务元数据
- nginx - Nginx 在代理通道上解码“%2F”
- javascript - Wordpress 和 JavaScript:当产品没有 sku 时尝试隐藏按钮
- python - 通过 Django QuerySet 水平迭代
- python - Python 升级和需求兼容性
- linux - 在linux中销毁对象后RSS(驻留集大小)没有减少