#Token not being read from JSON file when I am using code from the website?

1 messages · Page 1 of 1 (latest)

zealous narwhal
#

I am trying to get my bot to run and it isn't running.
I keep getting this:

12:28:11 Discord     Discord.Net v3.9.0 (API v10)
12:28:11 Discord     A supplied token was invalid.:
System.ArgumentNullException: A token cannot be null, empty, or contain only whitespace. (Parameter 'token')
   at Discord.TokenUtils.ValidateToken(TokenType tokenType, String token)
   at Discord.Rest.BaseDiscordClient.LoginInternalAsync(TokenType tokenType, String token, Boolean validateToken)
12:28:11 Gateway     Connecting
12:28:13 Gateway     Discord.Net.HttpException: The server responded with error 401: 401: Unauthorized
   at Discord.Net.Queue.RequestBucket.SendAsync(RestRequest request)
   at Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request)
   at Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request)
   at Discord.API.DiscordRestApiClient.SendAsync[TResponse](String method, String endpoint, BucketId bucketId, ClientBucketType clientBucket, RequestOptions options)
   at Discord.API.DiscordRestApiClient.GetBotGatewayAsync(RequestOptions options)
   at Discord.API.DiscordSocketApiClient.ConnectInternalAsync()
   at Discord.API.DiscordSocketApiClient.ConnectInternalAsync()
   at Discord.API.DiscordSocketApiClient.ConnectAsync()
   at Discord.WebSocket.DiscordSocketClient.OnConnectingAsync()
   at Discord.ConnectionManager.ConnectAsync(CancellationTokenSource reconnectCancelToken)
   at Discord.ConnectionManager.<>c__DisplayClass29_0.<<StartAsync>b__0>d.MoveNext()

This is the configuration part of the code:

            _configuration = new ConfigurationBuilder()
                .AddEnvironmentVariables(prefix: "!")
                .AddJsonFile("bot_info.json", optional: true)
                .Build();

Entire codebase: https://github.com/DudeThatsErin/csharpbot

GitHub

Contribute to DudeThatsErin/csharpbot development by creating an account on GitHub.

rigid grail
#

await client.LoginAsync(TokenType.Bot, _configuration["token"]);
do you have your bot token in the config file under the "token" key?

zealous narwhal
rigid grail
#

what is the structure of you json file?

#

is the file being copied to the output directory?

zealous narwhal
# rigid grail is the file being copied to the output directory?

Structure is just this:

{
"token": "BOT_TOKEN_HERE"
}

Output directory? What is that? The website doesn't define what is going on here.

I just have the bot_info.json file and I thought this was reading it... I was just trying to be able to run this bot and copy the code to github without discord freaking out cause my token was shared via GitHub

rigid grail
#

right click on the file in solution explorer (assuming ur using VS) and select properties, there should be a copy to output directory setting

zealous narwhal
#

Perfect, that worked. Thank you.

Now I just have:

12:45:24 Gateway     A Ready handler has thrown an unhandled exception.:
Discord.Net.HttpException: The server responded with error 50001: Missing Access
   at Discord.Net.Queue.RequestBucket.SendAsync(RestRequest request)
   at Discord.Net.Queue.RequestQueue.SendAsync(RestRequest request)
   at Discord.API.DiscordRestApiClient.SendInternalAsync(String method, String endpoint, RestRequest request)
   at Discord.API.DiscordRestApiClient.SendJsonAsync[TResponse](String method, String endpoint, Object payload, BucketId bucketId, ClientBucketType clientBucket, RequestOptions options)
   at Discord.API.DiscordRestApiClient.BulkOverwriteGuildApplicationCommandsAsync(UInt64 guildId, CreateApplicationCommandParams[] commands, RequestOptions options)
   at Discord.Rest.InteractionHelper.BulkOverwriteGuildCommandsAsync(BaseDiscordClient client, UInt64 guildId, ApplicationCommandProperties[] args, RequestOptions options)
   at Discord.Rest.ClientHelper.BulkOverwriteGuildApplicationCommandAsync(BaseDiscordClient client, UInt64 guildId, ApplicationCommandProperties[] properties, RequestOptions options)
   at Discord.Interactions.InteractionService.RegisterCommandsToGuildAsync(UInt64 guildId, Boolean deleteMissing)
   at csharpbot.InteractionHandler.ReadyAsync() in C:\Users\morri\Desktop\SAVE\csharp-bot\InteractionHandler.cs:line 51
   at Discord.EventExtensions.InvokeAsync(AsyncEvent`1 eventHandler)
   at Discord.WebSocket.DiscordSocketClient.TimeoutWrap(String name, Func`1 action)

I used this link to add the bot like 4x so I know it has perms + it is an admin on my testing server: https://discord.com/api/oauth2/authorize?client_id=819997306732019742&permissions=8&scope=applications.commands bot

Not sure what other access it could be referring to.

rigid grail
#

¯_(ツ)_/¯

#

debug, see where it errors, figure it out from there based on what it was trying to do

zealous narwhal
# rigid grail debug, see where it errors, figure it out from there based on what it was trying...

I'm coming from DJS and using JS for my bots.

Where IS the ready event in this?

This is the only thing with ready I see so I'm assuming this is it?

private async Task ReadyAsync()
        {
            // Context & Slash commands can be automatically registered, but this process needs to happen after the client enters the READY state.
            // Since Global Commands take around 1 hour to register, we should use a test guild to instantly update and test our commands.
            if (Program.IsDebug())
                await _handler.RegisterCommandsToGuildAsync(_configuration.GetValue<ulong>("testGuild"), true);
            else
                await _handler.RegisterCommandsGloballyAsync(true);
        }
rigid grail
#

yes

zealous narwhal
#

Perfect. Last question I have these errors which don't seem to effect the bot running but still bother me.

Severity    Code    Description    Project    File    Line    Suppression State
Warning    NU1701    Package 'Microsoft.Configuration.ConfigurationBuilders.Base 2.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.    csharpbot    C:\Users\morri\Desktop\SAVE\csharp-bot\csharpbot.csproj    1    

Severity    Code    Description    Project    File    Line    Suppression State
Warning    NU1701    Package 'Microsoft.Configuration.ConfigurationBuilders.UserSecrets 2.0.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.    csharpbot    C:\Users\morri\Desktop\SAVE\csharp-bot\csharpbot.csproj    1