Discord.Net How to list all commands in a group

767 Views Asked by At
    <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

How would I go about creating a command lets say called 'list' which would then send an embed with the list of commands automatically without having to write the embed and fill it in automatically? If it can't be done within an embed using a regular image is fine. I'm pretty sure it's going to use a for loop for that but after that, I don't know how to take it on.

1

There are 1 best solutions below

1
On BEST ANSWER

Somewhere in your program you would probably have something like

Dim collection As New ServiceCollection()
collection.AddSingleton(DiscordSocketClient())

or however you add your client to your IserviceProvider
You ServiceProvider is what is used for dependency injection. To inject the command service you would need to add it to the ServiceCollection

collection.AddSingleton(New CommandService())

To inject it into your command module, simply add an argument to your constructor

Public Class YourCommandModule
    Inherits ModuleBase(Of SocketCommandContext)

    Private ReadOnly Property Commands As CommandService

    Public Sub New(commands As CommandService)
        Me.Commands = commands
    End Sub

Then you can create your help command within that class, which now has access to the 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

You can look at the different things you have access to via ModuleInfo and CommandInfo