0

Okay, I am new to c#, so I don't have a great understanding of how some stuff works, but I am running a discord bot and I am trying to take user input from the console without stopping the other tasks in the code. When I use something like console.Readline() it will wait for user input before running the rest of the code. Is there any way to have the rest of the code running while also checking for user input? I'm sorry if any of this doesn't make sense but feel free to ask me any questions I would appreciate any help.

Here is the working code:

class Program
{
    public static void Main(string[] args)
    => new Program().MainAsync().GetAwaiter().GetResult();


    private DiscordSocketClient _client;
    public async Task MainAsync()
    {
        _client = new DiscordSocketClient();
        _client.MessageReceived += CommandHandler;
        _client.Log += Log;

        
        var token = File.ReadAllText("token.txt");


        await _client.LoginAsync(TokenType.Bot, token);
        await _client.StartAsync();

        // Block this task until the program is closed.
        await Task.Delay(-1);
    }

    private Task Log(LogMessage msg)
    {
        Console.WriteLine(msg.ToString());
        return Task.CompletedTask;
    }


        public Task CommandHandler(SocketMessage message)
    {
        //variables
        string command = "";
        int lengthOfCommand = message.Content.Length;
        Random rnd = new Random();

        command = message.Content.Substring(0, lengthOfCommand).ToLower();

        string[] greetings = { "hello", "hey", "hi", "sup", };
        string[] greetingResponse = { $"Hello {message.Author.Mention}", $"Hey {message.Author.Mention}", $"Hi {message.Author.Mention}" };

        int greetingsIndex = rnd.Next(0, greetingResponse.Length);

        for (int i = 0; i < greetings.Length; i++) // greetings
        {
            if (command.Equals(greetings[i]))
            {
                message.Channel.SendMessageAsync($@"{greetingResponse[greetingsIndex]}");
            }
            else if (command.Contains($"{greetings[i]} "))
            {
                message.Channel.SendMessageAsync($@"{greetingResponse[greetingsIndex]}"); // goes through the string array and chooses a random response
            }
        }
}
3
  • You are giongg in the right direction - multithreading/asynchrony is the way to do it :) But you have some major mistakes: entry point of a program must have signature public static void Main(string[] args) (or you just run this async method from your main method which is OK). File.ReadAllText returns string, which doesn't expose Token property. Also I do not see declarationo of TokenType - is this a global variable?? Please return with working code and explicitly say what you have problem with. Now you are asking to write code for you. Commented Jan 30, 2021 at 6:21
  • 1
    @MichałTurczyn Main can be async. Commented Jan 30, 2021 at 6:27
  • Thanks for the responses! I added the running code to the post. Commented Jan 30, 2021 at 6:40

1 Answer 1

1

what you're looking for is threading

read more here

read more here

try this:

new Thread(() => {
    // things you want to do async
}).Start();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.