poshbotio / PoshBot

Powershell-based bot framework
MIT License
536 stars 108 forks source link

CommandCount = 0? #169

Closed gnurph69 closed 4 years ago

gnurph69 commented 5 years ago

Hi, everybody.

I'm not sure where to go with this problem - it's me (i'm fairly sure) and it's because I don't have any real experience with modules, although I've been writing PowerShell code for 7+ years. I have the bot up and running, and it replies to the built-in commands, and I wrote a plugin / module with a single simple function (Hello, world-style). The plugin is installed, and enabled, but when I get-plugin [plugin name], it shows a CommandCount of 0, which I believe means that while it loaded the plugin - it didn't discern (export?) any commands.

I can include my psm1/psd1 files if that's helpful. I'm just unsure if I've missed a step or something here? Is there a "troubleshooting" section of the docs that I've not seen?

Thank you for any help you can provide.

mgeorgebrown89 commented 5 years ago

@gnurph69, It would be helpful to see the actual files, yes. Maybe link your repo?

But it sounds like you just need to explicitly export your commands. I ran into this issue a few times. You need something like this at the end of your psm1.

Export-ModuleMember -function 'Function-Name'

or

$commandsToExport = @()
$commandsToExport += 'one-Function'
$commandsToExport += 'another-Function'
Export-ModuleMember -function $commandsToExport
devblackops commented 5 years ago

Hey @gnurph69. I @mgeorgebrown89 said, this is most likely because your functions are not being exported from the module.

In your module manifest (psd1), make sure you are explicitly listing the functions to export like so:

FunctionsToExport = @(
    'Get-Foo1'
    'Get-Foo2'
)

Sometimes you'll see this like below but it's best practice (and better performance) to list them explicitly.

FunctionsToExport = @('*')

It's also best practice to use Export-ModuleMember like @mgeorgebrown89 shows above as well. This way, you can control exactly what commands, aliases, variables, etc are exported from the module.

Hope that helps.

gnurph69 commented 5 years ago

I did an export...and an import...to no avail that I can tell.

Poshbot.zip The repo isn't on github, so it was easier to zip up the files and go from there.

Here's my PSM1, my PSD1, my PoshBotConfig (called PoshBotConfigHelp and I removed the Slack token) and my plugins.psd1 file.

There is only 1 function in the PSM1 file, and all it does it perform a write-output. The PSD1 file is barebones, and you can see that I tried experimenting...to no avail. The config file is standard; I've got a PoshBot directory under Dropbox that everything lives in.
The plugins file has a single entry, pointing to the PSD1 file.

To start the bot, I run Import-Module c:\users\lcorrell\Dropbox\PoshBot\ACGI.psm1 Stop-Poshbot -id (get-job).id -Force $myBotConfig = Get-PoshBotConfiguration -Path $shared_directory\PoshBot\PoshBotConfig.psd1 Start-PoshBot -Configuration $myBotConfig -AsJob

Which kills any existing bot and then starts a new bot. It tries to load a (nonexistent) help file, I get crankiness about unapproved verbs, it's unable to parse help for my command, gripes about the Builtin plugin not being defined in plugins.psd1, says a bunch of things are already loaded, and then says the schedules.psd1 isn't found.

I've got to have overlooked something else basic here. Thank you for your help. None of which, I think, is relevant, but I mention it just in case i'm incorrect.

mgeorgebrown89 commented 5 years ago

@gnurph69 , I looked at the files, and I don't see anything obvious, but it has been awhile since I've worked on my own PoshBot.

From within Slack, have you used Update-Plugin or Uninstall-Plugin and then Install-Plugin?

I ran into this early in my testing where I would change the plugin/module, but the changes weren't reflected in Slack, so I needed to uninstall and reinstall the plugins (Update-Plugin didn't always work correctly for me).

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

devblackops commented 5 years ago

Hey @gnurph69 Sorry it took so long to get back to this. I looked at the module you provided. When you use the PoshBot-provided custom attributes like [PoshBot.BotCommand()] or [PoshBot.FromConfig()], you need to add PoshBot to the RequiredModules section of your manifest.

Try that out and let us know if it solved your issue.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.