W
W
WHOAMI2018-03-03 09:27:15
C++ / C#
WHOAMI, 2018-03-03 09:27:15

Discord bot keeps connecting and then disconnects, what to do?

Hello Toster! I don’t know if they will answer me here or not, but I will try to ask a question here, because I did not find the answer to my question on foreign forums.
I started developing my own Discord Bot in C# using the packages: Discord.Net and Newtonsoft.Json . I chose JSON as a good alternative to other persistent data stores. Having created a template for a bot based on various tutorials, I decided to try connecting it to my test server in Discord. Having launched a console application without debugging, the bot, for some unknown reason, tried to connect and later disconnected, and so on endlessly. (I work with the Discord.Net library for the first time)
5a9a3dfc929c2076956633.png
- - - - - - - - - - - - - - - - - - - - - - - - - -

Main script Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.WebSocket;
using Discord;

namespace DiscordPetyaBot
{
    class Program
    {
        DiscordSocketClient _client;
        CommandHandler _handler;

        static void Main(string[] args)
        => new Program().StartAsync().GetAwaiter().GetResult();

        public async Task StartAsync()
        {
            if (Config.bot.token == "" || Config.bot.token == null) return;
            _client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Verbose});
            _client.Log += Log;
            await _client.LoginAsync(TokenType.Bot, Config.bot.token);
            await _client.StartAsync();
            _handler = new CommandHandler();
            await _handler.InitializeAsync(_client);
            await Task.Delay(-1);
        }

        private async Task Log (LogMessage msg)
        {
            Console.WriteLine(msg.Message);
        }
    }
}
CommandHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;

namespace DiscordPetyaBot
{
    class CommandHandler
    {
        DiscordSocketClient _client;
        CommandService _service;

        public async Task InitializeAsync(DiscordSocketClient client)
        {
            _client = client;
            _service = new CommandService();
            await _service.AddModulesAsync(Assembly.GetEntryAssembly());
            _client.MessageReceived += HandleCommandAsync;
        }

        private async Task HandleCommandAsync(SocketMessage s)
        {
            var msg = s as SocketUserMessage;
            if (msg == null) return;
            var context = new SocketCommandContext(_client, msg);
            int argPos = 0;

            if (msg.HasStringPrefix(Config.bot.cmdPrefix, ref argPos) || msg.HasMentionPrefix(_client.CurrentUser, ref argPos))
            {
                var result = await _service.ExecuteAsync(context, argPos);
                if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
                {
                    Console.WriteLine(result.ErrorReason);
                }
            }
        }
    }
}

What could be wrong? I use the latest versions of the libraries, the bot token is 100% correct...
Maybe I'm not the only one who faced this problem?)
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WHOAMI, 2018-03-03
@1010111100100010011111000

5a9aa18218d55860029384.png
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
After 6 hours, I was finally able to solve the problem myself!
The issue was the lack of a web host provider (WebSocket) in Windows 7 used by DiscordSocketClient. The solution to this problem was to load the library into the solution. It is called like this: Discord.Net.Providers.WS4Net . Then you need to change the constructor of your client:

_client = new DiscordSocketClient(new DiscordSocketConfig { WebSocketProvider = Discord.Net.Providers.WS4Net.WS4NetProvider.Instance });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question