Example #1
0
        private async Task MessageRecieved(SocketMessage messageParam)
        {
            try
            {
                //TO BE REMOVED- SAVE TOWNS EVERY 5-10 MINS INSTEAD. CREATE SAFE SHUTDOWN METHOD THAT SAVES TOWNS/USERS ETC.
                TownHandler.SaveTowns();

                //Don't process the command if it was a system message
                var message = messageParam as SocketUserMessage;
                if (message == null)
                {
                    return;
                }

                //Create a number to track where the prefix ends and the command begins
                int argPos = 0;

                //If the user who sent that message is expecting input, parse the message for inputs.
                var user = UserHandler.GetUser(message.Author.Id);
                if (user.ExpectedInput != -1)
                {
                    var con = new SocketCommandContext(_client, message);
                    await MessageHandler.ParseExpectedInput(message, user, con);
                }

                // Determine if the message is a command based on the prefix and make sure no bots trigger commands
                if (!(message.HasCharPrefix('!', ref argPos) ||
                      message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
                    message.Author.IsBot)
                {
                    return;
                }

                // Create a WebSocket-based command context based on the message
                var context = new SocketCommandContext(_client, message);

                //Update user's info
                UserHandler.UpdateUserInfo(context.User.Id, context.User.GetOrCreateDMChannelAsync().Result.Id, context.User.Username, context.User.Mention, context.User.GetAvatarUrl());

                // Execute the command with the command context we just
                // created, along with the service provider for precondition checks.
                await _commands.ExecuteAsync(context, argPos, services : null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
        }