Hawxy/Discord.Addons.Hosting

Discord.Addons.Hosting.DiscordHostConfiguration can't be validated after running build

PijusVose opened this issue · 4 comments

For some reason, when I build my Discord bot with command dotnet build -c Release -r linux-x64 --no-self-contained and try to run it with dotnet bot.dll, I get an error: Microsoft.Extensions.Options.OptionsValidationException: A validation error has occurred.

Exception data:

OptionsValidationException.OptionsType = Discord.Addons.Hosting.DiscordHostConfiguration;
OptionsValidationException.Failures = System.Collections.Generic.List`1[System.String]

Discord host configuration code:

.ConfigureDiscordHost((context, config) => {
      config.SocketConfig = new DiscordSocketConfig
      {
          LogLevel = LogSeverity.Verbose,
          AlwaysDownloadUsers = true,
          UseInteractionSnowflakeDate = false,
          MessageCacheSize = 200
      };
      
      config.Token = context.Configuration.GetConnectionString(someString) ?? "";
      
      config.LogFormat = (message, exception) => $"{message.Source}: {message.Message}";
})

Package information:

<PackageReference Include="Discord.Addons.Hosting" Version="5.2.0" />
<PackageReference Include="Discord.net" Version="3.8.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
Hawxy commented

Check your configuration, your token isn't valid.

But why does the bot work when I use dotnet run?

Hawxy commented

dotnet run will run the project itself, dotnet bot.dll will run the output DLL within the folder. You're likely missing an appsettings.json/environment variable etc that holds the token. See this article.

Thanks, it works now. I forgot to set appsettings.json to CopyToOutputDirectory = Always.